熙冠

 

Silverlight调用WCF服务引用

初学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>
,将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暂时没试过。

posted on 2012-03-18 17:06  熙冠  阅读(4629)  评论(1)    收藏  举报

导航