gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:59:07 29 4
gpt4 key购买 nike

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

背景:

我有一个使用 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com