0

我有一个用 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;
        }
    }
}

服务1.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;
        }
    }
}
4

1 回答 1

2

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

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

这里,

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

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

于 2013-06-19T05:43:53.463 回答