初學Silverlight,現在使用Silverlight調用WCF服務,今天遇到了兩個異常,所以記錄下解決方案。
異常一:
在 ServiceModel 客戶端配置部分中,找不到引用協定“ServiceReference1.IService1”的默認終結點元素。這可能是因為未找到應用程序的配置文件,或者是因為客戶端元素中找不到與此協定匹配的終結點元素。
異常二:
給定關鍵字不在字典中
造成上面異常的主要原因是
在添加WCF項時,
有兩個選項,一是“WCF服務”
,二是“啟用Silverlight功能的WCF服務
”
如果選擇的是一的話,則在web.config文件中,會添加以下節點:
<service behaviorConfiguration="Test_1.Web.RESTBehavior" name="Test_1.Web.REST">
<endpoint address="" binding=" wsHttpBinding " contract="Test_1.Web.IREST">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
<endpoint address="" binding=" wsHttpBinding " contract="Test_1.Web.IREST">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
,將binding="wsHttpBinding"改為binding="basicHttpBinding"即可。在添加WCF服務引用后的Silverlight項目中自動生成的ServiceReferences.ClientConfig會自動添加節點,如果binding="wsHttpBinding"的話ServiceReferences.ClientConfig只有一個節點<configuration/>,這就是異常產生的原因。
如果選擇的是二的話,則在web.config文件中,會添加以下節點:
<service behaviorConfiguration="Test_1.Web.RESTBehavior"
name="Test_1.Web.REST">
<endpoint address="" binding="customBinding" bindingConfiguration="customBinding1"
contract="Test_1.Web.REST" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
默認binding="customBinding",所以不會拋出異常。
注:以上demo只是在Silverlight3.0+vs2008環境下,Silverlight4.0暫時沒試過。