无法从代码隐藏中覆盖MaxRecievedMessageSize

时间:2014-02-13 17:26:52

标签: c# wcf asp.net-mvc-4 .net-4.5

我托管一个WCF REST服务并在同一个webrole中使用它。配置在代码隐藏中完成。我已经尝试了这么多解决方案并发布了这个,

但我无法弄清楚为什么我无法覆盖MaxRecievedMessageSize。如果大小超过64kb。它抛出异常。

WebChannelFactory<X> channelFactory;

                if (!ChannelFactories.ContainsKey(endpointName.ToString(CultureInfo.InvariantCulture)))
                {
                    channelFactory = new WebChannelFactory<X>(new Uri(endpointName));

                    lock (ChannelFactories.SyncRoot)
                    {
                        ChannelFactories[endpointName.ToString(CultureInfo.InvariantCulture)] = channelFactory;
                    }
                }
                else
                {
                    channelFactory =
                        ChannelFactories[endpointName.ToString(CultureInfo.InvariantCulture)] as
                        WebChannelFactory<X>;
                }
                //Configure The Size here

                if (channelFactory != null)
                {

                    channelFactory.Endpoint.Binding = new WebHttpBinding
                        {

                            MaxReceivedMessageSize = 2147483647,
                            MaxBufferSize = 2147483647,
                            ReaderQuotas =new XmlDictionaryReaderQuotas()
                                {
                                    MaxArrayLength = 2147483647,
                                    MaxStringContentLength = 2147483647,
                                    MaxDepth = 2147483647,
                                    MaxNameTableCharCount = 2147483647,
                                    MaxBytesPerRead = 2147483647
                                },
                            MaxBufferPoolSize = 2147483647,
                            OpenTimeout = new TimeSpan(0, 0, 10, 0),
                            ReceiveTimeout = new TimeSpan(0, 0, 10, 0),
                            TransferMode = TransferMode.Streamed,
                            CloseTimeout = new TimeSpan(0, 0, 10, 0),
                            SendTimeout = new TimeSpan(0, 0, 10, 0)
                        };

和Web.Config

<system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <diagnostics>
      <messageLogging logEntireMessage="true" logMessagesAtServiceLevel="false" logMessagesAtTransportLevel="false" logMalformedMessages="true" maxMessagesToLog="5000" maxSizeOfMessageToLog="2000"></messageLogging>
    </diagnostics>
    <standardEndpoints></standardEndpoints>
  </system.serviceModel>

1 个答案:

答案 0 :(得分:0)

我不相信你可以在WebChannelFactory创建之后更改绑定(但我不是100%肯定)。创建工厂后,您可以更改的内容非常少。最有可能的是它使用WebHttpBinding的默认值,这就是为什么你得到了下限。

尝试创建绑定并将其传递到WebChannelFactory<T>构造函数重载之一,如下所示:

WebHttpBinding myBinding = new WebHttpBinding
                           {
                               MaxReceivedMessageSize = 2147483647,
                               MaxBufferSize = 2147483647,
                               ReaderQuotas =new XmlDictionaryReaderQuotas()
                               {
                                   MaxArrayLength = 2147483647,
                                   MaxStringContentLength = 2147483647,
                                   MaxDepth = 2147483647,
                                   MaxNameTableCharCount = 2147483647,
                                   MaxBytesPerRead = 2147483647
                               },
                               MaxBufferPoolSize = 2147483647,
                               OpenTimeout = new TimeSpan(0, 0, 10, 0),
                               ReceiveTimeout = new TimeSpan(0, 0, 10, 0),
                               TransferMode = TransferMode.Streamed,
                               CloseTimeout = new TimeSpan(0, 0, 10, 0),
                               SendTimeout = new TimeSpan(0, 0, 10, 0)
                           };

if (!ChannelFactories.ContainsKey(endpointName.ToString(CultureInfo.InvariantCulture)))
{

    // Pass the binding created above and the URI into the constructor
    channelFactory = new WebChannelFactory<X>(myBinding, new Uri(endpointName));

    lock (ChannelFactories.SyncRoot)
    {
        ChannelFactories[endpointName.ToString(CultureInfo.InvariantCulture)] = channelFactory;
    }
}
else
{
    channelFactory = ChannelFactories[endpointName.ToString(CultureInfo.InvariantCulture)] as WebChannelFactory<X>;
}

在上面的代码中,我使用WebFactoryChannel<T>的重载来获取绑定和URI - WebChannelFactory<TChannel> Constructor (Binding, Uri)