WCF - 在单个 APP.Config 文件中定义多个服务?
Posted
技术标签:
【中文标题】WCF - 在单个 APP.Config 文件中定义多个服务?【英文标题】:WCF - Define multiple services in a single APP.Config file? 【发布时间】:2011-02-04 19:04:36 【问题描述】:场景
我有一个 Windows 窗体应用程序。 我想使用两个完全没有连接的不同 WCF 服务。 但是,我不确定如何在我的 APP.CONFIG 文件中定义服务。 根据我所阅读的内容,可以执行我在下面所做的事情,但我不能确定语法是否正确或标签是否都存在于必要的地方,我需要澄清一下。
问题。
那么下面是在一个 APP.CONFIG 文件中设置两个服务的正确方法吗? 即:
<configuration>
<system.serviceModel>
<services>
<service>
<!--SERVICE ONE-->
<endpoint>
</endpoint>
<binding>
</binding>
</service>
<service>
<!--SERVICE TWO-->
<endpoint>
</endpoint>
<binding>
</binding>
</service>
</services>
</system.serviceModel>
</configuration>
代码
<configuration>
<system.serviceModel>
<services>
<!--SERVICE ONE-->
<service>
<endpoint
address=""
binding="netTcpBinding"
bindingConfiguration="tcpServiceEndPoint"
contract="ListenerService.IListenerService"
name="tcpServiceEndPoint"
/>
<binding
name="tcpServiceEndPoint"
closeTimeout="00:01:00"
openTimeout="00:01:00"
receiveTimeout="00:10:00"
sendTimeout="00:01:00"
transactionFlow="false"
transferMode="Buffered"
transactionProtocol="OleTransactions"
hostNameComparisonMode="StrongWildcard"
listenBacklog="10"
maxBufferPoolSize="524288"
maxBufferSize="65536"
maxConnections="10"
maxReceivedMessageSize="65536"
>
<readerQuotas
maxDepth="32"
maxStringContentLength="8192"
maxArrayLength="16384"
maxBytesPerRead="4096"
maxNameTableCharCount="16384"
/>
<reliableSession
ordered="true"
inactivityTimeout="00:05:00"
enabled="true"
/>
<security mode="None">
<transport
clientCredentialType="Windows"
protectionLevel="EncryptAndSign"
/>
<message clientCredentialType="Windows" />
</security>
</binding>
</service>
<!--SERVICE TWO-->
<service>
<endpoint
address=""
binding="netTcpBinding"
contract="UploadObjects.IResponseService"
bindingConfiguration="TransactedBinding"
name="UploadObjects.ResponseService"
/>
<binding name="TransactedBinding">
<security mode="None" />
</binding>
</service>
</services>
</system.serviceModel>
</configuration>
编辑
行为代表什么? 它们与服务定义有何关系?
编辑 2
服务名称是否需要与绑定名称相同?
【问题讨论】:
行为就是这样 - 您的服务的附加功能(行为)。服务元数据、服务调试等。 【参考方案1】:你的配置不太正确:
<configuration>
<system.serviceModel>
<behaviors>
... here you define sets of behaviors - behavior configurations
</behaviors>
<bindings>
... here you define your binding configurations (parameters for bindings)
</bindings>
<services>
<service name="Service1">
... here you define the service endpoint which includes the ABC of WCF:
... (A)ddress, (B)inding, (C)ontract
</service>
<service name="Service2">
... here you define the service endpoint which includes the ABC of WCF:
... (A)ddress, (B)inding, (C)ontract
</service>
....
</services>
</system.serviceModel>
</configuration>
通过分别指定behaviorConfiguration=
和bindingConfiguration=
设置,服务和服务端点可以引用行为配置以及绑定配置。
您绝对应该查看WCF Configuration Editor tool 以帮助您配置 WCF 服务!它应该可以从 Visual Studio 的“工具”菜单中获得:
它看起来像这样:
【讨论】:
【参考方案2】:将它们组合在一起。
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="Service1Bevhavior">
</behavior>
<behavior name="Service2Bevhavior"/>
</serviceBehaviors>
</behaviors>
<services>
<!-- SERVICE ONE -->
<service name="Service1">
<endpoint address=""
binding="netTcpBinding"
bindingConfiguration="tcpServiceEndPoint"
contract="ListenerService.IListenerService"
name="tcpServiceEndPoint" />
</service>
<!-- SERVICE TWO -->
<service name="Service2">
<endpoint address=""
binding="netTcpBinding"
contract="UploadObjects.IResponseService"
bindingConfiguration="TransactedBinding"
name="UploadObjects.ResponseService"/>
</service>
</services>
<bindings>
<netTcpBinding>
<binding name="tcpServiceEndPoint" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxBufferPoolSize="524288"
maxBufferSize="65536" maxConnections="10" maxReceivedMessageSize="65536">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:05:00"
enabled="true" />
<security mode="None">
<transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
<message clientCredentialType="Windows" />
</security>
</binding>
</netTcpBinding>
<netTcpBinding>
<binding name="TransactedBinding">
<security mode="None" />
</binding>
</netTcpBinding>
</bindings>
</system.serviceModel>
【讨论】:
【参考方案3】:服务名称是否必须是 是否与绑定名称相同?
服务名称应该是合同实现类。如果要使用绑定配置,绑定配置名称应与端点的 bindingConfiguration 设置相同。
<configuration>
<system.serviceModel>
<services>
<!--SERVICE ONE-->
<service name="ListenerService.ListenerServiceImplementation" >
<endpoint address=""
binding="netTcpBinding"
bindingConfiguration="tcpServiceEndPoint"
contract="ListenerService.IListenerService" />
<!--SERVICE TWO-->
<service name="UploadObjects.ResponseServiceImplementation" />
<endpoint address=""
binding="netTcpBinding"
bindingConfiguration="TransactedBinding"
contract="UploadObjects.IResponseService" />
</services>
<bindings>
<binding name="tcpServiceEndPoint" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxBufferPoolSize="524288"
maxBufferSize="65536" maxConnections="10" maxReceivedMessageSize="65536">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:05:00"
enabled="true" />
<security mode="None">
<transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
<message clientCredentialType="Windows" />
</security>
</binding>
<binding name="TransactedBinding">
<security mode="None" />
</binding>
</bindings>
</system.serviceModel>
</configuration>
【讨论】:
如果需要,您也可以使用 MSDN 资源:msdn.microsoft.com/en-us/library/ms735119%28v=VS.85%29.aspx【参考方案4】:是的,两个名称都应该是相同的服务名称和绑定名称
【讨论】:
以上是关于WCF - 在单个 APP.Config 文件中定义多个服务?的主要内容,如果未能解决你的问题,请参考以下文章
在 app.config 文件中配置 wcf 超时 [重复]
WCF 何时使用 app.config 或 web.config?
如何以编程方式修改 WCF app.config 端点地址设置?