【问题标题】:Windows service calling WCF service (web http configuration)Windows服务调用WCF服务(web http配置)
【发布时间】:2013-07-02 11:40:38
【问题描述】:

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

背景:

我有一个使用 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" 部分,但我已经使用包含此属性对其进行了测试。

【问题讨论】:

    标签: c# .net wcf windows-services wcf-binding


    【解决方案1】:

    在尝试了所有可能性之后,解决方案非常简单。 我在 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 绑定,因此它需要绑定的显式映射。

    至少这对我有用

    【讨论】:

      【解决方案2】:

      在你的 WCF 服务配置中

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

      试试这个:

      <endpoint address="" binding="webHttpBinding" bindingConfiguration="basicWebHttp"
          contract="Classes.IVisService" name="BasicHttpBinding_IVisService" behaviorConfiguration="WebHttp" />
      

      【讨论】:

      • 我粘贴了错误的配置。在我的配置中添加了这部分,并且错误仍然存​​在。
      • 你试过英里的回答吗?添加到 web.config :
      • 我试过了,没用。这是我尝试过的第一件事
      【解决方案3】:
      <system.web>
      <httpRuntime maxRequestLength="2147483647" /  >
      </system.web>
      

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

      【讨论】:

        【解决方案4】:

        你可以试试这样的:

        尝试在 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);
        

        【讨论】:

          【解决方案5】:

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

          然后我们进行下面的操作

          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)
          

          然后它对我有用。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-07-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-08-23
            • 2015-11-18
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多