c# - Windows服务调用WCF服务(web http配置)

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

我已经在这里搜索了所有可用的问题,我不知道问题是否特定于我的配置,或者我是否遗漏了某些内容。

背景:

我有一个使用 HTTPS 绑定(bind)托管在 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”部分,但我已经使用包含此属性对其进行了测试。

最佳答案

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

至少这对我有用

关于c# - Windows服务调用WCF服务(web http配置),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17424655/

相关文章:

c# - 向此类添加委托(delegate)或事件属性?

Silverlight 网络服务调用在 Studio 中工作,但从网站运行时失败

.net - SOAP 客户端未正确处理 XML 实体;遇到 "There is an error in XML document"

c# - 覆盖 C# 中固有属性的继承

c# - LINQ - 对特定类型的类进行分组

c# - 在 CultureInfo 中获取当前语言

c# - 如何增加原始音频字节的音量/振幅

c# - 如何增加命名管道的MaxStringContentLength?

C# QuickBooks 无法添加检查

c# - 如何为 HttpClient (WinForms) 设置默认的 json 序列化设置