日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > asp.net >内容正文

asp.net

Spring.NET学习笔记22——整合WCF(应用篇) Level 200

發(fā)布時(shí)間:2025/3/17 asp.net 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring.NET学习笔记22——整合WCF(应用篇) Level 200 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

  Spring.NET對(duì)WCF(Windows Communication Foundation)有很好的支持,Spring.Services程序集下封裝了創(chuàng)建和調(diào)用WCF的方法。以往,我們使用svc文件來(lái)承載WCF;使用自動(dòng)生產(chǎn)的代理來(lái)調(diào)用服務(wù)。這樣便對(duì)產(chǎn)生了諸多依賴。而使用Spring.NET則會(huì)令應(yīng)用程序得到良好的改善。

?

  讓我們從實(shí)例中學(xué)習(xí)今天的內(nèi)容:

  

  首先創(chuàng)建服務(wù)契約和其實(shí)現(xiàn)

Contract ????[ServiceContract]
????
public?interface?IWcfContract
????{
????????[OperationContract]
????????
string?GetData(int?value);
????}

????
public?class?ImplementService?:?IWcfContract
????{
????????
public?string?GetData(int?value)
????????{
????????????
return?"你輸入的是:"?+?value;
????????}
????}

?

?

  把服務(wù)契約的實(shí)現(xiàn)類加入Spring.NET中管理

?

<object?id="WcfServer"?type="WcfSevice.ImplementService,WcfSevice"/>

?

?

  創(chuàng)建一個(gè)WCF的WCF項(xiàng)目,并引用Common.Logging.dll、Spring.Core.dll、Spring.Data.dll、Spring.Web.dll、Spring.Services.dll(在Spring.NET庫(kù)下的3.0文件夾里)

  Spring.ServiceModel.Activation.ServiceHostFactory類繼承自System.ServiceModel.Activation.ServiceHostFactory,用于在BS架構(gòu)中承載WCF

?

  建立svc文件

<%@?ServiceHost?Language="C#"?Debug="true"?Service="WcfServer"?Factory="Spring.ServiceModel.Activation.ServiceHostFactory"%>

?

指定Service屬性為服務(wù)契約的實(shí)現(xiàn)類(WcfServer)

?

  配置web.config文件

?

代碼 <system.serviceModel>
????????
<services>
????????????
<service?name="WcfServer"?behaviorConfiguration="WcfServerBehavior">
????????????????
<!--?Service?Endpoints?-->
????????????????
<endpoint?address=""?binding="wsHttpBinding"?contract="IContract.IWcfContract"/>
????????????????
<endpoint?address="mex"?binding="mexHttpBinding"?contract="IMetadataExchange"/>
????????????
</service>
????????
</services>
????????
<behaviors>
????????????
<serviceBehaviors>
????????????????
<behavior?name="WcfServerBehavior">
????????????????????
<!--?為避免泄漏元數(shù)據(jù)信息,請(qǐng)?jiān)诓渴鹎皩⒁韵轮翟O(shè)置為?false?并刪除上面的元數(shù)據(jù)終結(jié)點(diǎn)-->
????????????????????
<serviceMetadata?httpGetEnabled="true"/>
????????????????????
<!--?要接收故障異常詳細(xì)信息以進(jìn)行調(diào)試,請(qǐng)將以下值設(shè)置為?true。在部署前設(shè)置為?false?以避免泄漏異常信息-->
????????????????????
<serviceDebug?includeExceptionDetailInFaults="false"/>
????????????????
</behavior>
????????????
</serviceBehaviors>
????????
</behaviors>
????
</system.serviceModel>

?

?

指定Service節(jié)點(diǎn)的name屬性為剛才配置的Spring.NET對(duì)象(WcfServer)。

在Global的Application_Start方法中實(shí)例化Spring.NET對(duì)象(Spring.Context.Support.ContextRegistry.GetContext();)

?

這樣WEB宿主的WCF就搭建成功了。

?

而在winform或者控制臺(tái)等程序中無(wú)法使用svc文件來(lái)承載WCF,但這一點(diǎn)被Spring.NET開(kāi)發(fā)團(tuán)隊(duì)考慮到了。Spring.Services程序集下的Spring.ServiceModel.Activation.ServiceHostFactoryObject則是用于非WEB環(huán)境的WCF創(chuàng)建。

?

我們以控制臺(tái)程序?yàn)槔?#xff0c;來(lái)講解在非WEB環(huán)境的WCF創(chuàng)建:

在控制臺(tái)程序中創(chuàng)建一項(xiàng)Spring.NET對(duì)象的配置

?

??<object?id="WcfServerHost"?type="Spring.ServiceModel.Activation.ServiceHostFactoryObject,?Spring.Services">
????
<property?name="TargetName"?value="WcfServer"?/>
??
</object>

?

指明TargetName屬性為實(shí)現(xiàn)服務(wù)契約的Spring.NET對(duì)象(WcfServer)

配置app.config文件,這里為了區(qū)別WEB環(huán)境,我使用netTcpBinding的綁定方式。

?

代碼 <system.serviceModel>

????
<behaviors>
??????
<serviceBehaviors>
????????
<behavior?name="WcfServerBehavior">
??????????
<!--?為避免泄漏元數(shù)據(jù)信息,請(qǐng)?jiān)诓渴鹎皩⒁韵轮翟O(shè)置為?false?并刪除上面的元數(shù)據(jù)終結(jié)點(diǎn)-->
??????????
<serviceMetadata?httpGetEnabled="true"/>
??????????
<!--?要接收故障異常詳細(xì)信息以進(jìn)行調(diào)試,請(qǐng)將以下值設(shè)置為?true。在部署前設(shè)置為?false?以避免泄漏異常信息-->
??????????
<serviceDebug?includeExceptionDetailInFaults="false"/>
????????
</behavior>
??????
</serviceBehaviors>
????
</behaviors>

????
????
<services>

??????
<service?name="WcfServer"?behaviorConfiguration="WcfServerBehavior">
????????
<endpoint?binding="netTcpBinding"?contract="IContract.IWcfContract"
??????????????????bindingConfiguration
="ServerBinding"
??????????????????address
="net.tcp://localhost:3286/App/Server/"/>
????????
<endpoint?address="mex"?binding="mexHttpBinding"?contract="IMetadataExchange"?/>
????????
<host>
??????????
<baseAddresses>
????????????
<add?baseAddress="http://localhost:3285/App/Server/"/>
??????????
</baseAddresses>
????????
</host>
??????
</service>
????
????
</services>
???
????
<bindings>
??????
??????
<netTcpBinding>
????????
<binding?name="ServerBinding">
??????????
<security?mode="None">
????????????
<transport?clientCredentialType="None"?/>
????????????
<message?clientCredentialType="None"?/>
??????????
</security>
????????
</binding>
??????
</netTcpBinding>
??????
????
</bindings>
????
??
</system.serviceModel>

?

指明baseAddress是為了便于生成WCF的代理類。

?

?最后在控制臺(tái)程序的Main方法中實(shí)例化Spring.NET容器,這樣一個(gè)以控制臺(tái)為宿主的WCF環(huán)境便搭建成功了。

?

  然而對(duì)于調(diào)用WCF,我們通常使用“添加服務(wù)應(yīng)用”的方式來(lái)生成WCF的代理類。這樣,在整個(gè)項(xiàng)目都使用Spring.NET框架的環(huán)境下,很難管理WCF的代理類。

為了讓Spring.NET來(lái)管理WCF的代理類,我們需要配置System.ServiceModel.ChannelFactory<T>這個(gè)泛型類,再配置代理類中的factory-method屬性。
至于System.ServiceModel.ChannelFactory<T>類,是用來(lái)創(chuàng)建和管理客戶端用來(lái)將消息發(fā)送到服務(wù)終結(jié)點(diǎn)的通道,這里不細(xì)講,請(qǐng)查看MSDN。

?

Client <!--創(chuàng)建web宿主的信道工廠-->
??
<object?id="WebChannelFactory"
??????type
="System.ServiceModel.ChannelFactory&lt;IContract.IWcfContract>,?System.ServiceModel">
????
<constructor-arg?name="endpointConfigurationName"?value="WSHttpBinding_IContract"?/>
??
</object>

??
<!--調(diào)用web宿主的代理類-->
??
<object?id="WebProxy"
????????type
="IContract.IWcfContract,?IContract"
????????factory-object
="WebChannelFactory"
????????factory-method
="CreateChannel"?singleton="false"?/>

??
<!--創(chuàng)建app宿主的信道工廠-->
??
<object?id="AppChannelFactory"
????type
="System.ServiceModel.ChannelFactory&lt;IContract.IWcfContract>,?System.ServiceModel">
????
<constructor-arg?name="endpointConfigurationName"?value="NetTcpBinding_IContract"?/>
??
</object>

??
<!--調(diào)用app宿主的代理類-->
??
<object?id="AppProxy"
????????type
="IContract.IWcfContract,?IContract"
????????factory-object
="AppChannelFactory"
????????factory-method
="CreateChannel"/>

?

?

注意的是System.ServiceModel.ChannelFactory<T>的構(gòu)造函數(shù)中需要指明endpointConfigurationName屬性為endpoint的name屬性

?

?

app.config ????<client>
??????
<endpoint?address="http://localhost:3287/WebHost.svc"?binding="wsHttpBinding"
??????????bindingConfiguration
="WSHttpBinding_IContract"?contract="IContract.IWcfContract"
??????????name
="WSHttpBinding_IContract"/>

??????
<endpoint?address="net.tcp://localhost:3286/App/Server/"?binding="netTcpBinding"
????????????bindingConfiguration
="NetTcpBinding_IContract"?contract="IContract.IWcfContract"
????????????name
="NetTcpBinding_IContract"/>
????
</client>

?

?

客戶端中調(diào)用WCF代理類

Program class?Program
????{
????????
static?void?Main(string[]?args)
????????{
????????????IApplicationContext?cxt?
=?ContextRegistry.GetContext();
????????????
????????????
//調(diào)用web宿主
????????????IWcfContract?webProxy?=?(IWcfContract)cxt.GetObject("WebProxy");
????????????Console.WriteLine(webProxy.GetData(
1));


????????????
//調(diào)用app宿主
????????????IWcfContract?appProxy?=?(IWcfContract)cxt.GetObject("AppProxy");
????????????Console.WriteLine(appProxy.GetData(
2));


????????????Console.ReadLine();
????????}
????}

?

?

程序運(yùn)行的效果:

?

?

代碼下載

總結(jié)

以上是生活随笔為你收集整理的Spring.NET学习笔记22——整合WCF(应用篇) Level 200的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。