WCF REST服务400错误请求

时间:2011-07-06 15:51:49

标签: android wcf wcf-rest

我有WCF RESTful服务和Android客户端。

当我做更大的请求时,

服务器回复400。好像我有65k限制问题 在here或其他相同问题的其他百万篇帖子中。

但是,我似乎无法修复它。这是我的web.config看起来如何

    <system.serviceModel>
    <diagnostics>
      <messageLogging logMalformedMessages="true" logMessagesAtServiceLevel="true"
        logMessagesAtTransportLevel="true" />
    </diagnostics>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <standardEndpoints>
      <webHttpEndpoint>
        <standardEndpoint name="myEndpoint" helpEnabled="true" automaticFormatSelectionEnabled="true" maxReceivedMessageSize="1000000" />
      </webHttpEndpoint>
    </standardEndpoints>
  </system.serviceModel>

以下是服务功能的代码示例:

[WebInvoke(UriTemplate = "/trips/{TripId}/inspection", Method = "POST")]
        [Description("Used on mobile devices to submit inspections to server")]
        public void PostTripInspection(string tripId, Inspection inspection)
        {
            return;
        }

这是我的Web项目中包含WCF(Global.asax.cs)

的代码
private static void RegisterRoutes()
        {
            // Setup URL's for each customer
            using (var cmc = new CoreModelContext())
            {
                foreach (var account in cmc.Accounts.Where(aa => aa.IsActive).ToList())
                {
                    RouteTable.Routes.Add(
                        new ServiceRoute(
                            account.AccountId + "/mobile", new WebServiceHostFactory(), typeof(MobileService)));
                }
            }
        }

据我所知,Java HttpClient没有施加任何限制,所以它在WCF方面。有关如何解决此问题或如何拦截WCF中的消息的任何指针?

编辑2: 这是跟踪显示的内容。当我修改standardEndpoint它没有帮助......

enter image description here

2 个答案:

答案 0 :(得分:1)

如果你看过这个链接(Similar StackOverflow Question),请原谅我:

  

默认情况下,WCF传输是   限于以65K发送消息。如果   你想发送更大的你需要的   启用流式传输模式和   你需要增加大小    MaxReceivedMessageSize ,就在那里   就像一个防范某人的守卫   通过上传来杀死你的服务器   大量文件。

     

所以,你可以使用绑定来做到这一点   配置或你可以做到   码。这是一种方法   代码:

var endpoint = ((HttpEndpoint)host.Description.Endpoints[0]); //Assuming one endpoint
endpoint.TransferMode = TransferMode.Streamed;
endpoint.MaxReceivedMessageSize = 1024 * 1024 * 10;  // Allow files up to 10MB

答案 1 :(得分:0)

在这种情况下您不需要使用流式传输 - 您需要做的就是增加标准webHttpEndpoint上的maxReceivedMessageSize配额:

<standardEndpoints>
  <webHttpEndpoint>
    <!-- 
        Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
        via the attributes on the <standardEndpoint> element below
    -->
    <standardEndpoint name=""
                      helpEnabled="true"
                      automaticFormatSelectionEnabled="true"
                      maxReceivedMessageSize="1000000"/>
  </webHttpEndpoint>
</standardEndpoints>

更新:如果配置更改不起作用(我不知道为什么),您可以尝试在代码中增加它。通过使用自定义服务主机工厂,您可以获得对端点对象的引用,并可以在那里增加配额。下面的代码显示了一个这样的工厂(您需要更新RegisterRoute代码才能使用这个新工厂):

public class MyWebServiceHostFactory : ServiceHostFactory
{
    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
        return base.CreateServiceHost(serviceType, baseAddresses);
    }

    class MyWebServiceHost : WebServiceHost
    {
        public MyWebServiceHost(Type serviceType, Uri[] baseAddresses)
            : base(serviceType, baseAddresses)
        {
        }
        protected override void OnOpening()
        {
            base.OnOpening();
            foreach (ServiceEndpoint endpoint in this.Description.Endpoints)
            {
                if (!endpoint.IsSystemEndpoint)
                {
                    Binding binding = endpoint.Binding;
                    if (binding is WebHttpBinding)
                    {
                        ((WebHttpBinding)binding).MaxReceivedMessageSize = 1000000;
                    }
                    else
                    {
                        CustomBinding custom = binding as CustomBinding;
                        if (custom == null)
                        {
                            custom = new CustomBinding(binding);
                        }

                        custom.Elements.Find<HttpTransportBindingElement>().MaxReceivedMessageSize = 1000000;
                    }
                }
            }
        }
    }
}