c# - 如何使用 C# WCF RESTful(即 Web)服务发送 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;
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;
        }
    }
}

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;
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;
        }
    }
}

最佳答案

有两种方法可以做到这一点。第一种是编写 IDispatchMessageFormatter 的新实现,它知道如何理解 CSV 文件并将其“反序列化”为适当的类型。您可以在 http://blogs.msdn.com/b/carlosfigueira/archive/2011/05/03/wcf-extensibility-message-formatters.aspx 找到更多相关信息。 .

另一种更简单,是使用“原始编程模型”,其中您的操作返回类型声明为Stream,您的操作可以返回CSV格式的数据。您可以在 http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-web.aspx 找到有关该模式的更多信息。 .

关于c# - 如何使用 C# WCF RESTful(即 Web)服务发送 CSV 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17197901/

相关文章:

c# - 基本c#问题

c# - 在 WCF 服务引用之间共享类型并仍然自动生成 INotifyPropertyChanged

c# - System.StackOverflowException 未处理 - C#、.NET

c# - 从套接字读取网络流

c# - 如何仅使用一个 Controller 动态检索表?

.net - ASP.NET网站->WCF服务->WCF服务,一路冒充?

.net - 远程还是不远程?

web-services - 防止访问主域上子域的文件夹

web-services - 在 WSO2 ESB 传出请求中使用 TLS 1.1/1.2

Silverlight Web服务问题