WCF WebServiceHostFactory MaxReceivedMessageSize 配置 [英] WCF WebServiceHostFactory MaxReceivedMessageSize configuration

查看:22
本文介绍了WCF WebServiceHostFactory MaxReceivedMessageSize 配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Palladium"的RESTful WCF Web 服务作为我 VS2008 解决方案中的一个项目.它通过名为Palladium.svc"的页面使用 WebServiceHostFactory 实现托管在 ASP.Net 3.5 Web 应用程序中.

I have a RESTful WCF web service called "Palladium" as a project in my VS2008 solution. It is hosted in an ASP.Net 3.5 Web Application using the WebServiceHostFactory implementation via a page called "Palladium.svc".

我的服务的工作方式与解释的类似 此处,由此服务可以接收 POST 以及定义的其他参数在 URITemplate 中.

My service works in a manner similar to the one explained here, whereby POSTs can be received by the service along with other parameters as defined in the URITemplate.

服务很好,我可以接收发布的信息并使用它.
当发布数据超过 65k 时出现我的问题,并且出现以下错误(在 web.config 和 Microsoft Service Trace Viewer 中使用 system.diagnostics 获得).

The service works well, and I can receive posted information and work with it.
My problem occurs when the post data exceeds 65k and I get the following error (obtained using system.diagnostics in the web.config and Microsoft Service Trace Viewer).

已超出传入邮件的最大邮件大小配额 (65536).要增加配额,请在适当的绑定元素上使用 MaxReceivedMessageSize 属性.

The maximum message size quota for incoming messages (65536) has been exceeded. To increase the quota, use the MaxReceivedMessageSize property on the appropriate binding element.

因为服务是通过 WebServiceHostFactory 实现托管的,所以服务有工厂为其设置的默认绑定.我试图通过在 web.config 文件中提供绑定设置和端点来覆盖这些绑定.但是,当我这样做时,我收到一条错误消息:

Because the service is hosted via the WebServiceHostFactory implementation, the service has default bindings set up for it by the factory. I attempted to override these bindings by providing binding settings and endpoints in the web.config file. When I did this however, I got an error message saying:

System.InvalidOperationException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

System.InvalidOperationException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

要使操作 LogStartingDetails 中的请求成为流,该操作必须有一个类型为 Stream 的参数.

For request in operation LogStartingDetails to be a stream the operation must have a single parameter whose type is Stream.

LogStartingDetails 是我在 RESTful 服务中调用的方法.

LogStartingDetails is the method I'm calling in my RESTful service.

显然,LogStartingDetails 方法不需要具有类型为 Stream 的单个参数,因为当工厂为我创建绑定时,服务响应良好(或者更重要的是,当工厂为我做这项工作时,它不需要只有一个参数).

Obviously the LogStartingDetails method doesn't need to have a single parameter whose type is Stream, as when the factory was creating the bindings for me the service was responding well (or more to the point, it didn't need to have a single parameter when the factory was doing the work for me).

经过大量研究和碰壁后,我决定创建自己的类,该类继承自 WebServiceHostFactory 并覆盖一些实现以指定 MaxReceivedMessageSize 属性在绑定上.
当我通过调试逐步在我的工厂类中创建服务时,我可以看到传输接收新的 MaxReceivedMessageSizeMaxBufferSize 值,但它们似乎没有做任何事情并且我仍然得到相同的 传入消息的最大消息大小配额 (65536). 抛出异常.

After much research and hitting several brick walls, I decided to create my own class that inherits from WebServiceHostFactory and overrides some of the implementation in order to specify the MaxReceivedMessageSize property on the binding.
When I step through the service creation in my factory class via debug, I can see the transport receiving the new MaxReceivedMessageSize and MaxBufferSize values, but they don't appear to do anything and I still end up getting the same The maximum message size quota for incoming messages (65536) has been exceeded. exception thrown.

以下是我的服务代码示例.如果有人能帮我弄清楚我在这里做错了什么,我将不胜感激.

Below are examples of my service code. If someone could help me figure out what I'm doing wrong here, it would be much appreciated.

Palladium.svc(托管在 ASP.Net Web 应用程序中)

Palladium.svc (hosted in the ASP.Net Web Application)

<%@ ServiceHost Language="C#" Debug="true" Service="CDS.PalladiumService.Palladium" Factory="CDS.PalladiumService.MyWebServiceHostFactory" %>

MyWebServiceHostFactory.cs(在 CDS.PalladiumService 项目中)

MyWebServiceHostFactory.cs (in the CDS.PalladiumService project)

using System;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Channels;
using System.ServiceModel.Web;

namespace CDS.PalladiumService
{
    public class MyServiceHost : WebServiceHost
    {
        public MyServiceHost()
        {
        }

        public MyServiceHost(object singletonInstance, params Uri[] baseAddresses)
            : base(singletonInstance, baseAddresses)
        {
        }

        public MyServiceHost(Type serviceType, params Uri[] baseAddresses)
            : base(serviceType, baseAddresses)
        {
        }


        protected override void OnOpening()
        {
            base.OnOpening();

            if (base.Description != null)
            {
                foreach (var endpoint in base.Description.Endpoints)
                {
                    var transport = endpoint.Binding.CreateBindingElements().Find<TransportBindingElement>();
                    if (transport != null)
                    {
                        transport.MaxReceivedMessageSize = 5242880;
                        transport.MaxBufferPoolSize = 5242880;
                    }
                }
            }
        }
    }
    
    class MyWebServiceHostFactory : WebServiceHostFactory
    {
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            return new MyServiceHost(serviceType, baseAddresses);
        }
    }
}

IPalladium.cs(在 CDS.PalladiumService 项目中)

IPalladium.cs (in the CDS.PalladiumService project)

using System.IO;
using System.ServiceModel;
using System.ServiceModel.Web;

namespace CDS.PalladiumService
{
    // NOTE: If you change the interface name "IPalladium" here, you must also update the reference to "IPalladium" in Web.config.
    [ServiceContract]
    public interface IPalladium
    {
        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = UriTemplate.LogStartingDetails)]
        void LogStartingDetails(string truckId, string palladiumId, Stream postData);
    }
}

Palladium.cs(在 CDS.PalladiumService 项目中)

Palladium.cs (in the CDS.PalladiumService project)

using System.IO;
using System.ServiceModel.Activation;

namespace CDS.PalladiumService
{
    // NOTE: If you change the class name "Palladium" here, you must also update the reference to "Palladium" in Web.config.
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Palladium : IPalladium
    {
        public void LogStartingDetails(string truckId, string palladiumId, Stream postData)
        {
            string contents = string.Empty;
            using (var reader = new StreamReader(postData))
            {
                contents = reader.ReadToEnd();
            }

            StreamWriter sw1 =
                File.AppendText(@"C:log.txt");
            sw1.WriteLine(contents);
            sw1.WriteLine("");
            sw1.Close();

            return;
        }
    }
}

URITemplate.cs(在 CDS.PalladiumService 项目中)

URITemplate.cs (in the CDS.PalladiumService project)

namespace CDS.PalladiumService
{
    public static class UriTemplate
    {
        public const string LogStartingDetails = "/log-starting/{truckId}/{palladiumId}";
    }
}

推荐答案

好的,对于那些正在寻找上述问题的解决方案的人 - 我只是对我的 web.config 文件进行了一些更改并将服务托管在我的 asp.net 网站.

Ok, for those of you that are looking for a solution to the above problem - I simply made some changes to my web.config file and hosted the service in my asp.net website.

我将 Web 配置的 部分更改为以下内容 - 这允许大消息绑定",其中帖子的最大长度在配置文件.

I changed the <system.serviceModel> section of my web config to the following - this allows for 'Large Message Binding' where the max length of the post is specified in the appropriate section of the config file.

<system.serviceModel>
<bindings>
  <webHttpBinding>
    <binding name="largeMessageBinding" maxBufferPoolSize="5242880" maxReceivedMessageSize="5242880" transferMode="Buffered">
      <readerQuotas maxStringContentLength="5242880" maxArrayLength="5242880" maxBytesPerRead="5242880" />
    </binding>
  </webHttpBinding>
</bindings>

<behaviors>
  <endpointBehaviors>
    <behavior name="largePostEndpointBehavior">
      <webHttp />
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="CDS.UI.Resources.Services.PalladiumBehavior">
      <serviceMetadata httpGetEnabled="false" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>

<services>
  <service behaviorConfiguration="CDS.UI.Resources.Services.PalladiumBehavior"
   name="CDS.PalladiumService.Palladium">
    <endpoint address="" binding="webHttpBinding" behaviorConfiguration="largePostEndpointBehavior"
              bindingConfiguration="largeMessageBinding" contract="CDS.PalladiumService.IPalladium" />
  </service>
</services>

</system.serviceModel>

这篇关于WCF WebServiceHostFactory MaxReceivedMessageSize 配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆