2

我在这里搜索了所有可用的问题,我不知道问题是特定于我的配置还是我遗漏了什么。

背景:

我有一个使用 HTTPS 绑定在 IIS 7 上托管的 WCF 服务。该服务用于发送/接收大型对象。Windows 服务使用此 WCF 服务将这些大对象发送给它,但我收到 400 Bad Request 错误。我在服务中启用了跟踪,错误是“已超出传入消息的最大消息大小配额 (65536)”。以下是 WCF 和 Windows 服务配置以及 wcf 服务的使用方式。

WCF 服务配置。

<system.web>
    <compilation debug="true" targetFramework="4.0" />
    <globalization culture="mk-MK" fileEncoding="windows-1251" />
</system.web>
<system.serviceModel>
<bindings>
  <webHttpBinding>
    <binding name="basicWebHttp" allowCookies="true" closeTimeout="00:59:59" receiveTimeout="00:59:59"
             sendTimeout="00:59:59" maxReceivedMessageSize="2147483647" 
             maxBufferSize="2147483647" maxBufferPoolSize="2147483647" >
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647"
                    maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
      <security>
        <transport clientCredentialType="None" realm="" proxyCredentialType="None" />
      </security>
    </binding>
  </webHttpBinding>
</bindings>
<behaviors>
  <serviceBehaviors>
    <behavior>
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
      <serviceTimeouts transactionTimeout="00:10:00"/>
      <serviceThrottling maxConcurrentCalls="20" maxConcurrentSessions="20" maxConcurrentInstances="20" />
      <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="WebHttp">
      <webHttp automaticFormatSelectionEnabled="true" faultExceptionEnabled="true" />
      <dataContractSerializer maxItemsInObjectGraph="2147483647" />
    </behavior>
  </endpointBehaviors>
</behaviors>
<client>
  <endpoint address="" binding="webHttpBinding" bindingConfiguration="basicWebHttp"
    contract="Classes.IVisService" name="BasicHttpBinding_IVisService" behaviorConfiguration="WebHttp" />
</client>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<defaultDocument>
  <files>
    <remove value="default.aspx" />
    <remove value="iisstart.htm" />
    <remove value="index.html" />
    <remove value="index.htm" />
    <remove value="Default.asp" />
    <remove value="Default.htm" />
    <remove value="Default.html" />
    <add value="VisService.svc" />
  </files>
</defaultDocument>
<urlCompression doDynamicCompression="false" />
</system.webServer>
<system.diagnostics>
<sources>
  <source name="System.ServiceModel"
          switchValue="Information, ActivityTracing, Error"
          propagateActivity="true">
    <listeners>
      <add name="traceListener"
          type="System.Diagnostics.XmlWriterTraceListener"
          initializeData= "c:\log\Traces.svclog" />
    </listeners>
  </source>
</sources>
</system.diagnostics>

该服务通过具有以下配置的 Windows 服务使用:

<system.web>
<httpRuntime maxRequestLength="2147483647" />
</system.web>
<system.serviceModel>
<bindings>
  <webHttpBinding>
    <binding name="basicWebHttp" allowCookies="true" closeTimeout="00:59:59" receiveTimeout="00:59:59"
              sendTimeout="00:59:59"
              maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" maxBufferPoolSize="2147483647">
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647"
                    maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
      <security mode="Transport">
        <transport clientCredentialType="None"></transport>
      </security>
    </binding>
  </webHttpBinding>
</bindings>
<behaviors>
  <endpointBehaviors>
    <behavior name="basicWebHttpBehaviour">
      <dataContractSerializer maxItemsInObjectGraph="2147483647" />
      <webHttp />
    </behavior>
  </endpointBehaviors>
</behaviors>
<client>
  <endpoint address="" behaviorConfiguration="basicWebHttpBehaviour"
            binding="webHttpBinding" bindingConfiguration="basicWebHttp"
            contract="labis.IVisService" name="BasicHttpBinding_IVisService" />
</client>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>

该服务是这样调用的:

System.Net.ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) => { return true; };
WebChannelFactory<IVisService> factory = new WebChannelFactory<IVisService>(
                                "BasicHttpBinding_IVisService",
                                new Uri("https://some_ip/service.svc"));
IVisService service = factory.CreateChannel();
service.PostData(large_object);

我已启用跟踪,从日志中我可以看到服务器抛出异常:已超出传入消息的最大消息大小配额 (65536)

我想我已经设置了所有需要的属性,但没有运气。同样在 IIS 配置编辑器中,我将 system.webServer/security/requestFiltering - requestLimits 属性设置为最大值

有什么帮助吗?

谢谢 :)

编辑 1

我粘贴了错误的端点配置元素。原版缺少 behaviorConfiguration="WebHttp" 部分,但我已经使用包含此属性对其进行了测试。

4

5 回答 5

2

在尝试了所有可能性之后,解决方案非常简单。我在 WCF 服务中添加了以下配置

<protocolMapping>
  <add scheme="https" binding="webHttpBinding" bindingConfiguration="basicWebHttp" />
  <add scheme="http" binding="webHttpBinding" bindingConfiguration="basicWebHttp" />
</protocolMapping>

在此处阅读有关它的更多信息http://msdn.microsoft.com/en-us/library/ee816881.aspx

我猜这很有效,因为该服务使用 HTTPS 绑定,因此它需要绑定的显式映射。

至少这对我有用

于 2013-07-02T13:34:29.390 回答
1

在您的 WCF 服务配置中

您忘记将行为配置(您将其命名为“WebHttp”)添加到端点

尝试这个 :

<endpoint address="" binding="webHttpBinding" bindingConfiguration="basicWebHttp"
    contract="Classes.IVisService" name="BasicHttpBinding_IVisService" behaviorConfiguration="WebHttp" />
于 2013-07-02T11:47:01.080 回答
1
<system.web>
<httpRuntime maxRequestLength="2147483647" /  >
</system.web>

将上述配置放入托管服务的 web.config 中。

于 2013-07-02T11:49:28.087 回答
1

你可以尝试这样的事情:

尝试在 WebChannelFactory 的构造函数中提供端点。

但首先您必须提取配置并创建端点实例并手动提供行为。

http://weblogs.asp.net/cibrax/archive/2010/05/11/getting-wcf-bindings-and-behaviors-from-any-config-source.aspx是关于如何做到这一点的一个很好的资源。

public Binding ResolveBinding(string name)
    {
    Configuration appConfig = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

ServiceModelSectionGroup serviceModel = ServiceModelSectionGroup.GetSectionGroup(appConfig);

      BindingsSection section = serviceModel.Bindings;

      foreach (var bindingCollection in section.BindingCollections)
      {
        if (bindingCollection.ConfiguredBindings.Count > 0 

    && bindingCollection.ConfiguredBindings[0].Name == name)
        {
          var bindingElement = bindingCollection.ConfiguredBindings[0];
          var binding = (Binding)Activator.CreateInstance(bindingCollection.BindingType);
          binding.Name = bindingElement.Name;
          bindingElement.ApplyConfiguration(binding);

          return binding;
        }
      }

      return null;
    }

public List<IEndpointBehavior> ResolveEndpointBehavior(string name)
{
     Configuration appConfig = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

ServiceModelSectionGroup serviceModel = ServiceModelSectionGroup.GetSectionGroup(appConfig);

      BindingsSection section = serviceModel.Behaviors;
  List<IEndpointBehavior> endpointBehaviors = new List<IEndpointBehavior>();

  if (section.EndpointBehaviors.Count > 0 

&& section.EndpointBehaviors[0].Name == name)
  {
    var behaviorCollectionElement = section.EndpointBehaviors[0];

    foreach (BehaviorExtensionElement behaviorExtension in behaviorCollectionElement)
    {
      object extension = behaviorExtension.GetType().InvokeMember("CreateBehavior",
            BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance,
            null, behaviorExtension, null);

      endpointBehaviors.Add((IEndpointBehavior)extension);
    }

   return endpointBehaviors;
 }

 return null;
}

然后使用:

    var binding = ResolveBinding("MyBinding");
    var behaviors = ResolveEndpointBehavior("MyBehavior");
    System.ServiceModel.Description.ContractDescription contract = System.ServiceModel.Description.ContractDescription.GetContract(typeof(IVisService));
     System.ServiceModel.Description.ServiceEndpoint ep = new System.ServiceModel.Description.ServiceEndpoint(contract, binding, "https://some_ip/service.svc");
                    foreach (var behavior in behaviors)
                    {
                        ep.Behaviors.Add(behavior);
                    }

System.ServiceModel.Web.WebChannelFactory<IVisService> t = new System.ServiceModel.Web.WebChannelFactory<IVisService>(ep);
于 2013-07-02T13:53:15.583 回答
0

在我遇到同样问题之前的最后两周,即使我已经更新了服务和客户端的配置,与您的相同,但代码不起作用,我们收到了相同的超大消息。

然后我们进行了以下操作

1. Updated the webconfig file of service
2. Reset IIS of service
3. Created a proxy for service
4. updated client web.config file
5. reset the iis of client(webapplication)

然后它对我有用。

于 2013-07-02T11:48:46.553 回答