如何使用C#WCF RESTful(即Web)服务发送CSV文件?

如何使用C#WCF RESTful(即Web)服务发送CSV文件?,c#,wcf,web-services,rest,csv,C#,Wcf,Web Services,Rest,Csv,我的任务是使用C#WCF RESTful(即Web)服务以CSV格式发送数据。目前,我有以JSON格式发送数据的代码设置 如何以CSV格式发送数据 注意:这实际上不是我正在使用的文件集。这只是一个示例,展示了我如何构建我的服务,并帮助修改它以获得CSV输出 IService1.cs: using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; usin

我的任务是使用C#WCF RESTful(即Web)服务以CSV格式发送数据。目前,我有以JSON格式发送数据的代码设置

如何以CSV格式发送数据

注意:这实际上不是我正在使用的文件集。这只是一个示例,展示了我如何构建我的服务,并帮助修改它以获得CSV输出

IService1.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService4
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebInvoke(
            Method = "GET", 
            UriTemplate = "employees",
            RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Bare)]
        List<Employee> GetEmployees();
    }

    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class Employee
    {
        [DataMember]
        public string FirstName { get; set; }

        [DataMember]
        public string LastName { get; set; }

        [DataMember]
        public int Age { get; set; }

        public Employee(string firstName, string lastName, int age)
        {
            this.FirstName = firstName;
            this.LastName = lastName;
            this.Age = age;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Net;

namespace WcfService4
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {
        public List<Employee> GetEmployees()
        {
            // In reality, I'm calling the data from an external datasource, returning data to the client that exceeds 10 MB and can reach an upper limit of at least 30 MB.               

            List<Employee> employee = new List<Employee>();
            employee.Add(new Employee("John", "Smith", 28));
            employee.Add(new Employee("Jane", "Fonda", 42));
            employee.Add(new Employee("Brett", "Hume", 56));

            return employee;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Runtime.Serialization;
使用System.ServiceModel;
使用System.ServiceModel.Web;
使用系统文本;
命名空间WcfService4
{
//注意:您可以使用“重构”菜单上的“重命名”命令同时更改代码和配置文件中的接口名称“IService1”。
[服务合同]
公共接口IService1
{
[经营合同]
[WebInvoke(
Method=“GET”,
UriTemplate=“employees”,
RequestFormat=WebMessageFormat.Json,
ResponseFormat=WebMessageFormat.Json,
BodyStyle=WebMessageBodyStyle.Bare)]
列出GetEmployees();
}
//如下面的示例所示,使用数据协定将复合类型添加到服务操作中。
[数据合同]
公营雇员
{
[数据成员]
公共字符串名{get;set;}
[数据成员]
公共字符串LastName{get;set;}
[数据成员]
公共整数{get;set;}
公共雇员(字符串firstName、字符串lastName、整数年龄)
{
this.FirstName=FirstName;
this.LastName=LastName;
这个。年龄=年龄;
}
}
}
Service1.svc.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService4
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebInvoke(
            Method = "GET", 
            UriTemplate = "employees",
            RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Bare)]
        List<Employee> GetEmployees();
    }

    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class Employee
    {
        [DataMember]
        public string FirstName { get; set; }

        [DataMember]
        public string LastName { get; set; }

        [DataMember]
        public int Age { get; set; }

        public Employee(string firstName, string lastName, int age)
        {
            this.FirstName = firstName;
            this.LastName = lastName;
            this.Age = age;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Net;

namespace WcfService4
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {
        public List<Employee> GetEmployees()
        {
            // In reality, I'm calling the data from an external datasource, returning data to the client that exceeds 10 MB and can reach an upper limit of at least 30 MB.               

            List<Employee> employee = new List<Employee>();
            employee.Add(new Employee("John", "Smith", 28));
            employee.Add(new Employee("Jane", "Fonda", 42));
            employee.Add(new Employee("Brett", "Hume", 56));

            return employee;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Runtime.Serialization;
使用System.ServiceModel;
使用System.ServiceModel.Web;
使用系统文本;
Net系统;
命名空间WcfService4
{
//注意:您可以使用“重构”菜单上的“重命名”命令来同时更改代码、svc和配置文件中的类名“Service1”。
公共类服务1:IService1
{
公开名单
{
//实际上,我从外部数据源调用数据,将超过10 MB的数据返回到客户机,并且可以达到至少30 MB的上限。
List employee=新列表();
添加(新员工(“John”,“Smith”,28));
添加(新员工(“Jane”,“Fonda”,42));
添加(新员工(“Brett”,“Hume”,56));
返回员工;
}
}
}

有两种方法可供选择。第一个是编写
IDispatchMessageFormatter
的新实现,它知道如何理解CSV文件并将其“反序列化”为适当的类型。您可以在上找到有关它的更多信息


另一个更简单的方法是使用“原始编程模型”,在该模型中,您的操作返回类型声明为
,并且您的操作可以返回CSV格式的数据。有关该模式的更多信息,请访问。

是否可以将其作为字符串发送?基本上,这就是CSV,只需将返回类型更改为String,对吗?或者,我可以将员工列表序列化为字符串吗?非常感谢。这两个都是很好的选择。我的问题是为什么我不能将方法类型设置为String并返回String类型?或者,该服务仍然会将其序列化为XML或JSON?我想补充的是,我确实找到了一个链接,可以将JSON字符串转换为CSV(www.codeproject.com/Tips/565920/Create CSV from JSON in Csharp),但这可能只在数据序列化为JSON后才有用。但是,我不认为将其转换为JSON只是为了将其转换回CSV有任何意义。如果返回类型为
string
,它将被序列化为JSON(或XML),这不是您想要的。您可以通过MTOM发送,是的。您所需要做的就是将其作为
字节[]
发送,并将其作为附件发送(如果大小大于某个阈值,约为768字节IIRC)。只要客户端理解MTOM,他们就能够正确读取它。