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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Silverlight与WCF之间的通信(4)silverlight以net.tcp方式调用console上寄宿的wcf服务

發(fā)布時間:2023/12/6 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Silverlight与WCF之间的通信(4)silverlight以net.tcp方式调用console上寄宿的wcf服务 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

(由于最近是針對一個demo進行的研究,在之前公開過代碼結(jié)構(gòu),這里只是對需要改動的地方加以說明)

WCF4.0使得編寫wcf服務不再那么復雜,去掉了許多的配置信息,客戶端只需要一個服務地址,便可在系統(tǒng)生成的代理類下做開發(fā)了,在部署時也只需要更改引用配置文件的地址即可。但是今天我嘗試silverlight以net.tcp方式連接host到console上的wcf服務時,卻頗費周折,一個wcf console server 和一個console client 之間的通信很簡單,不需要任何配置,但是silverlight如果想引用這個服務,則必須為服務定義元數(shù)據(jù)才能供silverlight生成代理類,如果直接用之前的服務,會產(chǎn)生錯誤。

WCF服務端配置

這個是簡單的服務,沒有任何配置,本次我是想完全由配置文件來解決信息公開的問題,所以服務端實際上很簡單

????? host = new ServiceHost(typeof(ChatService));??
????? host.Open();?
在silverlight引用時會出現(xiàn)找不到元數(shù)據(jù)的情況,原因是沒有公開服務的描述信息,沒有元數(shù)據(jù)無法讓外界知道服務的信息,所以經(jīng)過反復的試驗后終于能夠正常訪問到服務,配置文件有兩種情況:

1,如果沒有配置基地址,則終結(jié)點的地址采用全地址??
??
?

代碼 <system.serviceModel>?
????
<services>?
??????
<service?behaviorConfiguration="Server.ChatServiceBehavior"?name="Server.ChatService">?
????????
<endpoint?address="net.tcp://localhost:4503/ChatService"?binding="netTcpBinding"?contract="Server.IChatService"></endpoint>?
????????
<endpoint?address="net.tcp://localhost:4503/ChatService/mex"?binding="mexTcpBinding"?contract="IMetadataExchange"?></endpoint>????????
??????
</service>?????
????
</services>?
????
<behaviors>?
??????
<serviceBehaviors>?
????????
<behavior?name="Server.ChatServiceBehavior">?
??????????
<serviceMetadata?httpGetEnabled="false"/>?
??????????
<serviceDebug?includeExceptionDetailInFaults="false"/>?
??????
</behavior>?
??????
</serviceBehaviors>?
????
</behaviors>?

?

2,如果配置了基地址,則終結(jié)點的地址采用相對地址

?

代碼 <system.serviceModel>?
????
<services>?
??????
<service?behaviorConfiguration="Server.ChatServiceBehavior"?name="Server.ChatService">?
????????
<endpoint?address=""?binding="netTcpBinding"?contract="Server.IChatService"></endpoint>?
????????
<endpoint?address="mex"?binding="mexTcpBinding"?contract="IMetadataExchange"?></endpoint>?
????????
<host>?
??????????
<baseAddresses>?
????????????
<add?baseAddress="net.tcp://localhost:4503/ChatService/"/>?
??????????
</baseAddresses>?
????????
</host>?
??????
</service>?
????
</services>?

?3,其實,也可以通過HTTP獲得元數(shù)據(jù),如下配置即可?

<endpoint?address="http://localhost:4503/ChatService/mex"?binding="mexHttpBinding"?contract="IMetadataExchange"?></endpoint>?

?4,一定要注意安全配置這里設(shè)置一下,否則默認會有局域網(wǎng)的賬戶驗證什么的。?

<bindings>?
??????
<netTcpBinding>?
????????
<binding?name="netTcpBindConfig">?
??????????
<security?mode="None"/>?
????????
</binding>?
??????
</netTcpBinding>?
????
</bindings>

?5,行為配置?

代碼

????<behaviors>?
??????
<serviceBehaviors>?
????????
<behavior?name="Server.ChatServiceBehavior">?
??????????
<serviceMetadata?/>?
??????????
<serviceDebug?includeExceptionDetailInFaults="false"/>?
??????
</behavior>?
??????
</serviceBehaviors>?
????
</behaviors>?
??
</system.serviceModel>

?到這里,服務已經(jīng)通過net.tcp方式建立起來了,靜候silverlight客戶端的調(diào)用了。

silverlight調(diào)用

silverlight以tcp方式訪問服務器時,只能夠訪問固定的端口,需要一個策略文件驗證,我們只需要將這個策略文件放到IIS下即可。

代碼 void?MainPage_Loaded(object?sender,?RoutedEventArgs?e)?
????????{?
????????????ChatService.ChatServiceClient?proxy?
=?new?ChatService.ChatServiceClient();?
????????????proxy.SendMessageCompleted?
+=?(o,ev)?=>?{?
????????????????
string?str?=?string.Empty;?
????????????????
if?(ev.Error?==?null)?
????????????????{?
????????????????????str?
=?"發(fā)送成功";?
????????????????}?
????????????????
else?
????????????????{?
????????????????????str?
=?"發(fā)送失敗";?
????????????????}?
????????????????MessageBox.Show(str);?
????????????};?
????????????proxy.SendMessageAsync(
new?ChatService.MessageInfo()?{?Message="hello,leon",?UserName="mac",?PartnerName="leon"});?
????????}

?

?注意,一定要將策略文件放到IIS下面。




? ? ?本文轉(zhuǎn)自wengyuli 51CTO博客,原文鏈接:http://blog.51cto.com/wengyuli/587227,如需轉(zhuǎn)載請自行聯(lián)系原作者




總結(jié)

以上是生活随笔為你收集整理的Silverlight与WCF之间的通信(4)silverlight以net.tcp方式调用console上寄宿的wcf服务的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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