环境是这样的
在solution中添加了一个WebSite,在WebSite中添加了一个Silverlight-enabled WCF Service(添加普通的WCF Service,在Silverlight中使用service reference时会报错。但是通过修改config和契约是可以使用的,这里简化背景,只适用Silverlight-enabled WCF Service)。 再向solution中添加一个Silverlight Application,在这个application中添加刚才创建的Silverlight-enabled WCF Service的service reference是可以调用成功的。之后再向solution添加一个Silverlight Library。把这个Library引用到刚才的Silverlight application中。并向Library中添加service reference。这时在这个Library中调用WCF服务会失败。抛出Could not find default endpoint element that references contract...
在网上找啊找也没有好的解决方案,看来只能靠自己了。分析异常报告,有这样一句
This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element。
显然前半句的错误肯定不存在。ServiceReferences.ClientConfig好好的躺在那呢。
那么只能是endpoint element在config中找不到了。尝试着在ClientConfig中将契约添加命名空间,修改绑定类型等等都不起作用。
后来突然想到有Application加载Library,那么会不会需要在Applcation中的ClientConfig添加Library的endpoint呢?于是在Application的ClientConfig中尝试添加Library中用到的endpoint。重新编译,启动。成功调用。。。。。。
怎么说微软好呢。。。。。
总结解决方法,如果要在Silverlight的library中使用WCF需要在Sliverlight Applcation的config文件中添加相应的endpoint