设置 WCF 客户端和服务器时,配置文件必须如何同步?
大多数 WCF 示例都会向您展示如何配置 WCF 客户端和服务器。现在,如果它们之间的配置略有不同,会发生什么?我的意思是,谁有最终决定权?
我们以这个客户端配置为例:
<configuration>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_ISampleService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:01:00" sendTimeout="00:01:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
allowCookies="false">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Message">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" establishSecurityContext="true" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8080/SampleService" binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_ISampleService" contract="ISampleService"
name="WSHttpBinding_ISampleService">
</endpoint>
</client>
</system.serviceModel>
通常,服务器端将在其公开的服务器上配置完全相同的绑定。
但如果在服务器端定义了 openTimeout = 00:00:30,现在会发生什么。超时时间是多少?谁赢了?我对所有其他参数做了同样的问题。
整个事情看起来一团糟,你怎么知道,对于配置的每个元素(绑定、客户端、服务、行为等)及其所有细节,需要哪些参数以及哪一方(客户端或服务器)?
看来您可以在服务器端定义带有所有超时参数的整个绑定,而在客户端只需放置所需的最低配置,以便接受来自服务器的所有参数。但是现在,考虑到服务器有更深入的配置,客户端上最少需要的参数是多少?
使用 WCF 配置客户端和服务器时,关于配置的每个元素的参数(绑定、服务、客户端/端点和行为)的最佳实践是什么?
当客户端和服务器之间定义的参数冲突时,WCF 如何处理?
Most of the WCF examples out there show you how to configure WCF client and server. Now, what happens if you differ the configuration slightly between them? I mean, who has the final word?
Let's take this client configuration for example:
<configuration>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_ISampleService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:01:00" sendTimeout="00:01:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
allowCookies="false">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Message">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" establishSecurityContext="true" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8080/SampleService" binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_ISampleService" contract="ISampleService"
name="WSHttpBinding_ISampleService">
</endpoint>
</client>
</system.serviceModel>
Usually the server side will have the exact same binding configured on its exposed server.
But what happens now if on the server side is defined with openTimeout = 00:00:30. What will be the timeout? Who wins? I do the same question for all other parameters.
The whole thing seems a big mess, how can you tell, for each element of the configuration (binding, client, service, behavior, etc.) and all their details, which parameters is required and in which side (client or server)?
It seems you could define the entire binding with all timeout parameters on the server side and on the client side simply put the minimum required configuration so all parameters from the server are accepted. But now, what are the minimum required parameters on the client considering the server has a more in depth configuration?
What are the best practice when configuring client and server using WCF regarding parameters for each element of the configuration: bindings, services, client/endpoint and behavior?
When conflicting parameters are defined between client and server how WCF handles it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了解决您在对我之前的答案的最后评论中的请求,我尝试提出如何为任何给定服务创建(和修改)服务器和客户端配置的方法。这是基于我读过的理论(书籍、博客)、我在 Juval Lowy 的
WCF 大师班
中学到的东西,以及在几个大型服务实施项目中获得的大量实践经验 - 这不是'无法在一个地方找到,无论是在网络上还是在书中……所以这里是:我基本上会从头开始。首先考虑您的服务:
最简单的场景:单一服务,单一端点,basicHttpBinding,所有默认值
服务配置:
相应的客户端配置:
只有在确实必须时才更改某些内容!最重要的是:永远不要 > 让 Visual Studio(添加服务引用)或 svcutil.exe 搞砸您的配置!像保护您的掌上明珠一样保护它!
然后:如果您的数据传输花费的时间超过了默认的 1 分钟超时允许的时间,请更改服务端和客户端的一项设置。通过定义自定义绑定配置并从端点引用该配置来执行此操作 - 但仅更改该配置 - 而不是更多! 将其他所有内容保留为默认值。除非绝对必须(并且知道自己在做什么,以及为什么这样做),否则永远不要改变任何事情。
请注意:客户端上的
sendTimeout
(发送整个消息所允许的时间)将对应于服务器上的receiveTimeout
- 发送整个消息所允许的时间。进来(请参阅这篇优秀博客文章和此MSDN 论坛主题 了解更多信息)服务配置:
相应的客户端配置:
当您需要其他更改时,例如服务端的多个端点,或诸如
bypassProxyOnLocal
之类的本地设置 - 调整您的配置,请仔细执行,一步一步进行步骤,手动,并将您的配置视为整个服务中极其重要的一部分 - 照顾它,将其放入版本控制等。In order to address your request in your last comment to my previous answer, I tried to come up with my approach to how I would create (and modify) server- and client-side config's for any given service. This is based on both theory I read (books, blogs), things I've learned in Juval Lowy's
WCF Master Class
, and quite a bit of practical experience with several large service implementation projects - this isn't available in one single place, on the web or in a book.... so here it goes:I would start basically from scratch. Think about your service first:
Simplest scenario: single service, single endpoint, basicHttpBinding, all defaults
Service config:
Corresponding client config:
Then only ever change something if you really must! And most of all: NEVER EVER let Visual Studio (Add Service Reference) or
svcutil.exe
screw up your config! Protect it like the apple of your eye!Then: if e.g. your data transfer takes more time than the default timeout of 1 minute allows, change that one single setting on both the service side and the client side. Do this by defining a custom binding configuration and referencing that from your endpoints - but change only that - not more! Leave everything else as is, with default values. Don't ever change anything unless you absolutely must (and know what you're doing, and why you're doing it).
Mind you: the
sendTimeout
on the client (time allowed until the whole message has been sent) will correspond to thereceiveTimeout
on the server - the time allowed for the whole message to come in (see this excellent blog post and this MSDN forum thread for more information)Service config:
Corresponding client config:
As you need other changes, like multiple endpoints on the service side, or local settings like
bypassProxyOnLocal
- adapt your config, do it carefully, step by step, manually, and consider your config an extremely essential part of your whole service - take care of it, put it in version control etc.如果给定的超时最终在客户端和服务器上大致相同,并且两个值不匹配,则较短的超时“获胜” - 无论它是在服务器还是客户端上定义。
大多数其他内容,例如地址、绑定等。必须匹配 - 否则,将无法进行通信...
服务器设置的好处是您可以定义多个端点具有不同选项和绑定的单个服务 - 然后客户端可以“挑选”要连接到的端点。
但是,一旦为您的连接选择了一个服务端点,绑定、协议、安全性、可靠性等设置就必须完全匹配。
另外:Visual Studio
Add Service Reference
或svcutil.exe
命令行工具生成的默认配置都是灾难性的错误 - 它们包含太多的设置,而这些设置都反映了默认值,这使得很难知道什么是真正需要的,什么不是。在这方面,我建议观看 DotNet Rocks 电视节目 #122:Miguel Castro on Extreme WCF,其中 Miguel 很好地展示了手动创建这些配置是多么容易,然后您就完全明白自己在做什么!强烈推荐!
If a given timeout is about the same thing in the end, on the client and the server, and the two values don't match, the shorter timeout "wins" - no matter whether it's defined on the server or the client.
Most other things like addresses, bindings etc. must match - otherwise, no communication will be possible...
The benefit of the server setup is that you can define multiple endpoints for a single service, with different options and bindings - and the client can then "pick and choose" which endpoint to connect to.
But once one service endpoint has been chosen for your connection, the settings like bindings, protocols, security, reliability and so forth must match exactly.
Also: the default config that the Visual Studio
Add Service Reference
or thesvcutil.exe
command line tools generate are both catastrophically bad - they contain way too many settings that all reflect the default values, which make it really hard to know what's really needed and what is not.In that respect, I'd recommend watching the DotNet Rocks TV Show #122: Miguel Castro on Extreme WCF in which Miguel nicely shows just how easy it really is to create those configs manually, and then you completely understand what the heck you're doing! Highly recommended!