Silverlight与WCF之间的通信(4)silverlight以net.tcp方式调用console上寄宿的wcf服务
(由于最近是針對一個demo進行的研究,在之前公開過代碼結(jié)構(gòu),這里只是對需要改動的地方加以說明)
WCF4.0使得編寫wcf服務(wù)不再那么復(fù)雜,去掉了許多的配置信息,客戶端只需要一個服務(wù)地址,便可在系統(tǒng)生成的代理類下做開發(fā)了,在部署時也只需要更改引用配置文件的地址即可。但是今天我嘗試silverlight以net.tcp方式連接host到console上的wcf服務(wù)時,卻頗費周折,一個wcf console server 和一個console client 之間的通信很簡單,不需要任何配置,但是silverlight如果想引用這個服務(wù),則必須為服務(wù)定義元數(shù)據(jù)才能供silverlight生成代理類,如果直接用之前的服務(wù),會產(chǎn)生錯誤。
WCF服務(wù)端配置
這個是簡單的服務(wù),沒有任何配置,本次我是想完全由配置文件來解決信息公開的問題,所以服務(wù)端實際上很簡單
????? host = new ServiceHost(typeof(ChatService));??
????? host.Open();?
在silverlight引用時會出現(xiàn)找不到元數(shù)據(jù)的情況,原因是沒有公開服務(wù)的描述信息,沒有元數(shù)據(jù)無法讓外界知道服務(wù)的信息,所以經(jīng)過反復(fù)的試驗后終于能夠正常訪問到服務(wù),配置文件有兩種情況:
1,如果沒有配置基地址,則終結(jié)點的地址采用全地址??
??
?
????<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è)置一下,否則默認(rèn)會有局域網(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>
?到這里,服務(wù)已經(jīng)通過net.tcp方式建立起來了,靜候silverlight客戶端的調(diào)用了。
silverlight調(diào)用
silverlight以tcp方式訪問服務(wù)器時,只能夠訪問固定的端口,需要一個策略文件驗證,我們只需要將這個策略文件放到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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: VNX NETAPP NAS 备份恢复
- 下一篇: linux之ps命令详解