REST服务400错误的请求

REST服务400错误的请求

本文介绍了WCF REST服务400错误的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have WCF RESTful service and Android client.

服务器400答复,当我做更大的请求。看来,我有65K的限制问题,像
在或在相同问题的其他百万的发帖。

Server replies with 400 when I do bigger request. It seems that I have 65k limit issue likein here or in other million posts on same problem.

不过,我似乎无法能够解决它。这里是我的web.config的外观

However, I can't seem to be able to fix it. Here is how my web.config looks

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

下面是服务功能的code例如:

Here is code example of service function:

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

下面是code它承载WCF(Global.asax.cs中)

Here is code inside my Web project which hosts 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消息拦截任何指针?

From what I understand Java HttpClient doesn't impose any limits so it's on WCF side. Any pointers on how to solve this issue or how to intercept message in WCF?

编辑2:
这是跟踪显示。当我modigy standardEndpoint它并不能帮助...

EDIT 2:This is what trace shows. And when I modigy standardEndpoint it doesn't help...

推荐答案

您不必使用这种情况下,流 - 所有你需要做的是提高标准webHttpEndpoint的maxReceivedMessageSize配额:

You don't need to use streaming in this case - all you need to do is to increase the maxReceivedMessageSize quota on the standard webHttpEndpoint:

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

更新:如果配置的变化并没有工作(我不知道为什么),你可以尝试在code将其增加。通过使用自定义服务主机工厂,你到端点对象的引用,你可以增加配额那里。下面的code显示了这样一个工厂(你需要更新 RegisterRoute code使用这个新厂):

Update: if the config change didn't work (I don't know why), you can try increasing it in code. By using a custom service host factory, you get a reference to the endpoint object and you can increase the quota there. The code below shows one such a factory (you'll need to update the RegisterRoute code to use this new factory):

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

这篇关于WCF REST服务400错误的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 21:52