资料来源:网络整理
时间:2023/2/14 0:14:14 共计:3624 浏览
一、概述
配置也是WCF编程中的主要组成部分。在
以往的.net应用程序中,我们会把DBConn和一些动态加载类及变量写在配置文件里。但WCF有所不同。他指定向客户端公开的服务,包括服务的地址、
服务用于发送和接收消息的传输和消息编码,以及服务需要的安全类型等。使用配置文件后,我们无需编译即可修改WCF的变化的信息,提高了程序的灵活性。
如果在代码里写了配置,那么配置文件将不起作用。
Web程序在Web.config中配置,应用程序中在App.config中配置。
二、服务配置的主要部分
在Config中配置服务的结点为:<system.serviceModel></system.serviceModel>,在这个节点中主要有三个平级的部分。如下代码所示:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<!--配置服务和终结点开始-->
<services>
<service>
<endpoint></endpoint>
</service>
</services>
<!--配置服务和终结点结束-->
<!--配置绑定开始-->
<bindings>
<netTcpBinding>
<binding>
</binding>
</netTcpBinding>
</bindings>
<!--配置绑定结束-->
<!--配置行为开始-->
<behaviors>
<serviceBehaviors>
<behavior>
</behavior>
</serviceBehaviors>
</behaviors>
<!--配置行为结束-->
</system.serviceModel>
</configuration>
Service配置节[必须有]:配置服务、接口和终结点。每个Service都会有以下两个属性。name:名称空间.类名[服务的具体实现类]。behaviorConfiguration:一个在behaviors节点中找到的名称。
Binding配置节[可有可无]:配置绑定,如http,tcp等。
Behavior配置节[可有可无]:配置行为,如认证等。
三、实例
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<!--服务-->
<services>
<!--name:名称空间.类型名-->
<!--behaviorConfiguration:behavior的名称,请看behavior配置节的名称-->
<service name="WCFLibrary.User" behaviorConfiguration="MyBehavior">
<host>
<baseAddresses>
<!-- 每种传输协议的baseAddress,用于跟使用同样传输协议Endpoint定义的相对地址组成完整的地址,
每种传输协议只能定义一个baseAddress。HTTP的baseAddress同时是service对外发布服务说明页面的URL -->
<add baseAddress="http://localhost:8732/Design_Time_Addresses/WCFLibrary/Service/"/>
</baseAddresses>
</host>
<!-- 除非完全限定,否则地址将与上面提供的基址相关,每个服务可以有多个Endpoint -->
<!-- Address:指定这个Endpoint对外的URI,这个URI可以是个绝对地址,也可以是个相对于baseAddress的
相对地址。如果此属性为空,则这个Endpoint的地址就是baseAddress-->
<!--bindingConfiguration:binding的名称,请看binding配置节的名称-->
<endpoint address="" binding="wsHttpBinding" contract="WCFLibrary.IUser" bindingConfiguration="myHttpBinding">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<!-- 此终结点不使用安全绑定,应在部署前确保其安全或将其删除-->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<!--绑定-->
<bindings>
<wsHttpBinding>
<binding name="myHttpBinding">
<security mode="None">
<message clientCredentialType="Windows" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<!--行为-->
<behaviors>
<serviceBehaviors>
<behavior name="MyBehavior">
<!-- httpGetEnabled - bool类型的值,表示是否允许通过HTTP的get方法获取sevice的WSDL元数据 -->
<serviceMetadata httpGetEnabled="True"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>

版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。