2011-07-06 226 views
0

我有WCF RESTful服务和Android客户端。WCF REST服务400错误请求

服务器回复400,当我做更大的请求。似乎我在here或其他数百万个帖子中遇到了65000限制问题,如 。

但是,我似乎无法修复它。这里是我的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; 
     } 

这里是它承载WCF(Global.asax.cs中)我的Web项目

内码
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: 这是跟踪显示。当我modigy standardEndpoint它并不能帮助......

enter image description here

回答

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

这似乎是什么,对吗?对我有意义,但不起作用:(看到更新的主要帖子。我跑了跟踪和YES,这个属性似乎是问题,我想知道我需要修改什么绑定.. – katit

+0

它应该工作,但如果它不' t,你可以尝试我添加到答案的更新解决方案 – carlosfigueira

+0

我会将你的帖子标记为答案,因为你指示我回到我应该看的地方猜猜什么?!在删除“name =”“”后开始工作,来自终端Microsoft服务配置编辑器抱怨它,但代码不会注册这个配置任何名称,标签必须出... – katit

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

我会在哪放置该代码? – katit

+0

有关编程设置,请参阅http://social.technet.microsoft.com/wiki/contents/articles/streaming-large-data-files-using-webhttpbinding-wcf.aspx。或者,通过web.config,尝试将transferMode =“Streamed”添加到绑定元素。 – jglouie

+0

此链接不显示配置终端的位置。但无论如何,我认为这是不正确的方向。我的Android客户端使用普通的POST,没有涉及流媒体。它只是发布大json字符串.. – katit

相关问题