您的位置:首页 > 其它

[WCF 学习笔记] 11. 配置文件

2012-06-21 14:17 405 查看
http://topic.csdn.net/u/20070503/12/5ec1cc79-dafe-4f2a-b7e4-3909c5c6d967.html

/article/8388875.html

http://www.cnblogs.com/kimtop/articles/1328026.html

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>

Client App.config

<?xml version="1.0" encoding="utf-8"?>

<configuration>

<system.serviceModel>

<client>

<endpoint address="http://localhost:8080/MyService" binding="basicHttpBinding"

contract="ConsoleApplication1.localhost.ICalculate"

name="BasicHttpBinding_ICalculate" />

</client>

</system.serviceModel>

</configuration>

使用 VS2005 的 "Add Service Reference..." 功能时,会自动创建客户端代理文件和配置文件。





--------------

Service Configuration Editor 使用方法 图文演示

1. 打开编辑器,创建新的配置文件。





2. 创建新的服务设置。





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





4. 确认契约是否正确。





5. 选择通讯方式。





6. 选择操作方式。





7. 将服务端点地址清空,不要理会提示。我们在后面使用 host 配置,这样便于日后调整路径。





8. 完成服务设置。





9. 查看服务设置。





10. 添加服务行为设置。





11. 添加 serviceMetadata,以便发布元数据。





12. 将 HttpGetEnabled 置为 True。





13. 修改服务行为设置。





14. 在 Host 上添加一个 Base Address,这样其它相关地址可以全部使用局部路径,便于日后维护更改。





15. 添加一个新的服务端点,用于配置 WS-MetadataExchange。(可选)





16. 注意 MetadataExchange 的相关参数设置。





17. 保存配置文件。





18. 运行服务器,在浏览器中输入 Host Base Address,我们就可以看到服务页面了。





19. 单击链接,查看 WSDL 信息。





客户端的配置方式比较简单,不再演示。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: