Learn WCF (4)--学会使用配置文件

本文介绍了WCF服务配置的核心要素,包括服务定义、绑定配置及行为设置等内容,并详细解析了配置文件的各部分功能。

无论是Web应用程序还是Win应用程序,我们都会经常用到配置文件。WCF作为分布式开发的基础框架,在定义服务以及定义消费服务的客户端时,都使用了配置文件的方法。配置文件的重要性和实用性是大家所熟知的,它可以给我们WCF开发的灵活性上带来很大的提高。下面说说我学习使用配置文件的所得。

WCF的配置使用.NET FrameworkSystem.Configuration配置系统。在Visual Studio中配置一个WCF服务时,如果是自托管宿主或Windows Services宿主
,则配置文件为App.confing,如果是IIS宿主,则配置文件为Web.config

先来看看简单的system.ServiceModel结构

ContractedBlock.gifExpandedBlockStart.gifCode
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><system.ServiceModel>

<services>
<service>
<endpoint/>
</service>
</services>

<bindings>
<!—定义一个或多个系统提供的binding元素,例如<basicHttpBinding>-->
<!—也可以是自定义的binding元素,如<customBinding>.-->
<binding>
<!—例如<BasicHttpBinding>元素.-->
</binding>
</bindings>

<behaviors>
<!—一个或多个系统提供的behavior元素.-->
<behavior>
<!—例如<throttling>元素.-->
</behavior>
</behaviors>

</system.ServiceModel>

(1)<services>

服务是在配置文件的 services 节中定义的。每个服务都有自己的 service 配置节。在Service中定义特定服务的address,binding,contract(也就是传说中重要的ABC)。

ContractedBlock.gifExpandedBlockStart.gifCode
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><services>
<servicename="WCFStudent.WCFStudentText"behaviorConfiguration="ServiceBehavior">
<endpointaddress=""binding="wsHttpBinding"contract="WCFStudent.IStuServiceContract"></endpoint>
<endpointaddress="mex"binding="mexHttpBinding"contract="IMetadataExchange"/>
</service>
</services>

<!--一个Service中可以有多个endpoint,这样就可以同时定制多个服务。如果address值为空,那么endpoint的地址就是默认的基地址(BaseAddress)。那么则需要在host中声明BaseAddress-->

(2)<bindings><!---->

<!-- Content type: Devdiv1. Transform: orcas2mtps.xslt. -->

此节包含标准绑定和自定义绑定的集合。每一项都是一个可由其唯一 name 进行标识的 binding 元素。服务通过用 name 与绑定进行链接来使用绑定。

个人感觉比较常用的是<basicHttpBinding>,<basicHttpBinding>等。由于属性比较多,在这里就不一一说明了。

ContractedBlock.gifExpandedBlockStart.gifCode
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><bindings>
<basicHttpBinding>
<bindingname="IStuServiceContract"closeTimeout="00:01:00"
openTimeout
="00:01:00"receiveTimeout="00:10:00"sendTimeout="00:01:00"
bypassProxyOnLocal
="false"transactionFlow="false"hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize
="524288"maxReceivedMessageSize="65536"
messageEncoding
="Text"textEncoding="utf-8"useDefaultWebProxy="true"
allowCookies
="false">
</basicHttpBinding>
</bindings>
<!--相应属性的设置大家可以参看MSDN.
http://msdn.microsoft.com/zh-cn/library/ms731361.aspx
-->

(3)<behaviors>

此元素定义名为 endpointBehaviorsserviceBehaviors 的两个子集合。每个集合分别定义终结点和服务所使用的行为元素。每个行为元素由其唯一的 name 属性标识。如果需要指定服务在执行方面的相关特性时,就必须定义服务的behavior。在WCF中,定义behavior就可以设置服务的运行时属性,甚至于通过自定义behavior插入一些自定义类型。

ContractedBlock.gifExpandedBlockStart.gifCode
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><behaviors>
<serviceBehaviors>
<behaviorname="ServiceBehavior">
<!--为避免泄漏元数据信息,请在部署前将以下值设置为false并删除上面的元数据终结点-->
<serviceMetadatahttpGetEnabled="true"/>
<!--要接收故障异常详细信息以进行调试,请将以下值设置为true。在部署前设置为false以避免泄漏异常信息-->
<serviceDebugincludeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>

(4)<client>

有的时候我们还需要定义<client>节点,主要定义客户端可以连接的终结点的列表。

ContractedBlock.gifExpandedBlockStart.gifCode
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><client>
<endpointaddress="http://localhost:39113/WCFServiceText/WCFStudentText.svc"
binding
="wsHttpBinding"bindingConfiguration="WSHttpBinding_IStuServiceContract"
contract
="ServiceReference1.IStuServiceContract"name="WSHttpBinding_IStuServiceContract">
<identity>
<dnsvalue="localhost"/>
</identity>
</endpoint>
</client>

<!--如果宿主是IIS,WIN应用程序调用WCF时,会自动在App.config中生成<client>节的设置-->

三种宿主形式的配置文件的声明基本上一样。如果使用svcutil.exe生成客户程序,会在svcutil.exe的根目录中生成一个配置文件,值得大家一看:

ContractedBlock.gifExpandedBlockStart.gifCode
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><?xmlversion="1.0"encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<bindingname="WSHttpBinding_IStuServiceContract"closeTimeout="00:01:00"
openTimeout
="00:01:00"receiveTimeout="00:10:00"sendTimeout="00:01:00"
bypassProxyOnLocal
="false"transactionFlow="false"hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize
="524288"maxReceivedMessageSize="65536"
messageEncoding
="Text"textEncoding="utf-8"useDefaultWebProxy="true"
allowCookies
="false">
<readerQuotasmaxDepth="32"maxStringContentLength="8192"maxArrayLength="16384"
maxBytesPerRead
="4096"maxNameTableCharCount="16384"/>
<reliableSessionordered="true"inactivityTimeout="00:10:00"
enabled
="false"/>
<securitymode="Message">
<transportclientCredentialType="Windows"proxyCredentialType="None"
realm
=""/>
<messageclientCredentialType="Windows"negotiateServiceCredential="true"
algorithmSuite
="Default"establishSecurityContext="true"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpointaddress="http://localhost:39113/WCFServiceText/WCFStudentText.svc"
binding
="wsHttpBinding"bindingConfiguration="WSHttpBinding_IStuServiceContract"
contract
="IStuServiceContract"name="WSHttpBinding_IStuServiceContract">
<identity>
<dnsvalue="localhost"/>
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>
【RIS 辅助的 THz 混合场波束斜视下的信道估计与定位】在混合场波束斜视效应下,利用太赫兹超大可重构智能表面感知用户信道与位置(Matlab代码实现)内容概要:本文围绕“IS 辅助的 THz 混合场波束斜视下的信道估计与定位”展开,重点研究在太赫兹(THz)通信系统中,由于混合近场与远场共存导致的波束斜视效应下,如何利用超大可重构智能表面(RIS)实现对用户信道状态信息和位置的联合感知与精确估计。文中提出了一种基于RIS调控的信道参数估计算法,通过优化RIS相移矩阵提升信道分辨率,并结合信号到达角(AoA)、到达时间(ToA)等信息实现高精度定位。该方法在Matlab平台上进行了仿真验证,复现了SCI一区论文的核心成果,展示了其在下一代高频通信系统中的应用潜力。; 适合人群:具备通信工程、信号处理或电子信息相关背景,熟悉Matlab仿真,从事太赫兹通信、智能反射面或无线定位方向研究的研究生、科研人员及工程师。; 使用场景及目标:① 理解太赫兹通信中混合场域波束斜视问题的成因与影响;② 掌握基于RIS的信道估计与用户定位联合实现的技术路径;③ 学习并复现高水平SCI论文中的算法设计与仿真方法,支撑学术研究或工程原型开发; 阅读建议:此资源以Matlab代码实现为核心,强调理论与实践结合,建议读者在理解波束成形、信道建模和参数估计算法的基础上,动手运行和调试代码,深入掌握RIS在高频通信感知一体化中的关键技术细节。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值