WCF 的配置文件并不复杂,相对于手工编写配置文件,我更倾向于使用 SDK 所附带的工具 —— "Service Configuration Editor"。使用这个工具,我们可以非常方便地创建或修改和客户端的配置文件。

WCF 配置文件基本元素:
<system.ServiceModel>
<services>
<service>
<endpoint/>
</service>
</services>
<bindings>
<!-- Specify one or more of the system-provided binding elements,
for example, <basicHttPBinding> -->
<!-- Alternatively, <customBinding> elements. -->
<binding>
<!-- For example, a <BasicHttpBinding> element. -->
</binding>
</bindings>
<behaviors>
<!-- One or more of the system-provided or custom behavior elements. -->
<behavior>
<!-- For example, a <throttling> element. -->
</behavior>
</behaviors>
</system.ServiceModel>
WCF 的配置信息必须写在 App.config 或 Web.config 中,我觉得像 Remoting 那样可以使用单独的配置文件应该更好一些。现在的项目开发中,各种各样的组件都往 App.config 中写信息,感觉非常凌乱,不便于维护。
在编写服务器端配置文件时,记得添加 serviceMetadata Behaviors,否则我们无法使用 SVCutil.exe 或 VS2005 创建客户端代理文件。
以下配置文件的演示:
Server App.config
<?XML version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<services>
<service behaviorConfiguration="NewBehavior" name="Learn.Library.WCF.MyService">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/MyService" />
</baseAddresses>
</host>
<endpoint address="" binding="basicHttPBinding"
contract="Learn.Library.WCF.IContract" />
<endpoint address="mex" binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="NewBehavior">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>

Service Configuration Editor 使用方法 图文演示
1. 打开编辑器,创建新的配置文件。
2. 创建新的服务设置。

3. 手工输入,或使用 "Browser..." 选择服务所在程序集。

4. 确认契约是否正确。

5. 选择通讯方式。
(责任编辑:admin)