2013-06-19 68 views
2

我的任务是使用C#WCF RESTful(即Web)服务以CSV格式发送数据。目前,我已经设置了以JSON发送数据的代码。如何使用C#WCF RESTful(即Web)服务发送CSV文件?

如何以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; 
     } 
    } 
} 
+1

你能把它作为一个字符串发送吗?这是一个CSV是 – Jonesopolis

+0

基本上,只是将返回类型更改为字符串,正确的?或者,我可以将员工列表并序列化为一个字符串吗? – user717236

回答

4

有两个备选方案,以做到这一点。首先是编写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找到关于该模式的更多信息。

+0

非常感谢。这些都是很好的选择。我的问题是为什么我不能只将方法类型设置为String并返回类型String?或者,该服务仍然会以XML或JSON序列化它? – user717236

+0

我想补充一点,我确实找到了一个将JSON字符串转换为CSV的链接(www.codeproject.com/Tips/565920/Create-CSV-from-JSON-in-Csharp),但这可能仅在数据已被序列化为JSON。但是,我没有看到将它转换为JSON的任何意义,只是为了将其转换回CSV。 – user717236

+2

如果返回类型是'string',它将被序列化为JSON(或XML),这不是你想要的。 – carlosfigueira