如何在具有互操作性的C#中实现WCF RESTful(JSON)服务的压缩?

如何在具有互操作性的C#中实现WCF RESTful(JSON)服务的压缩?,json,wcf,rest,interop,compression,Json,Wcf,Rest,Interop,Compression,我正在用C#构建一个WCF RESTful(即JSON)服务。其中一个DataContract方法可以返回非常大的响应,最小值为10 MB,最大值可能超过30 MB。它都是文本,并将其作为JSON数据返回给客户端。当我在浏览器中测试这个方法时,我看到它超时了。我知道有一种方法可以压缩WCF RESTful服务响应数据。因为互操作性对于我来说是绝对关键的,所以仍然可以压缩WCF RESTful服务响应数据吗?现在,我还在本地机器上测试这个项目。不过,我将把它部署到IIS 如果有一种方法可以利用互操

我正在用C#构建一个WCF RESTful(即JSON)服务。其中一个DataContract方法可以返回非常大的响应,最小值为10 MB,最大值可能超过30 MB。它都是文本,并将其作为JSON数据返回给客户端。当我在浏览器中测试这个方法时,我看到它超时了。我知道有一种方法可以压缩WCF RESTful服务响应数据。因为互操作性对于我来说是绝对关键的,所以仍然可以压缩WCF RESTful服务响应数据吗?现在,我还在本地机器上测试这个项目。不过,我将把它部署到IIS

如果有一种方法可以利用互操作性进行压缩,那么该如何实现呢

多谢各位

这实际上不是我正在使用的文件集,但它只是一个示例,展示了我如何构建我的服务。我意识到这个样本根本不需要压缩

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));
返回员工;
}
}
}

您可以更改web.config文件以解决此问题。 更改httpRuntime

<httpRuntime maxRequestLength="10240" executionTimeout="1000" />

这里,

maxRequestLength:表示ASP.NET支持的最大文件上载大小。此限制可用于防止用户将大型文件发布到服务器而导致的拒绝服务攻击。指定的大小以KB为单位。默认值为4096KB(4MB)


executionTimeout:表示在ASP.NET自动关闭之前,允许执行请求的最长秒数。

非常感谢。当你说在web.config文件中,你是指我的项目中的web.config文件还是IIS的web.config文件?你的项目的web.config文件