WCF REST 服务 400 错误请求

发布于 2024-11-18 18:24:29 字数 1960 浏览 2 评论 0原文

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

当我发出更大的请求时,服务器会回复 400。看来我有 65k 限制问题 在此处或其他有关同一问题的数百万帖子中。

但是,我似乎无法修复它。这是我的 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 时它没有帮助...

在此处输入图像描述

I have WCF RESTful service and Android client.

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

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>

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

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

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?

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

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

满栀 2024-11-25 18:24:29

如果您看过此链接(类似的 StackOverflow 问题),请原谅我:

默认情况下,WCF 传输是
仅限于以 65K 发送消息。如果
您想要发送更大的邮件,您需要
启用流传输模式并且
你需要增加尺寸
MaxReceivedMessageSize,就在那里
只是作为一名警卫来阻止某人
通过上传一个来杀死你的服务器
海量文件。

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

var端点=((HttpEndpoint)host.Description.Endpoints[0]); //假设有一个端点
端点.TransferMode = TransferMode.Streamed;
端点.MaxReceivedMessageSize = 1024 * 1024 * 10; // 允许文件最大为10MB

Forgive me if you've seen this link (Similar StackOverflow Question):

By default the WCF Transport is
limited to sending messages at 65K. If
you want to send larger you need to
enable Streaming Transfer Mode and
you need to increase the size of
MaxReceivedMessageSize, which is there
just as a guard to prevent someone
killing your server by uploading a
massive file.

So, you can do this using binding
configuration or you can do it in
code. Here is one way to do it in
code:

var endpoint = ((HttpEndpoint)host.Description.Endpoints[0]); //Assuming one endpoint
endpoint.TransferMode = TransferMode.Streamed;
endpoint.MaxReceivedMessageSize = 1024 * 1024 * 10;  // Allow files up to 10MB
献世佛 2024-11-25 18:24:29

在这种情况下,您不需要使用流式传输 - 您所需要做的就是增加标准 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;
                    }
                }
            }
        }
    }
}

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>

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;
                    }
                }
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文