天天看点

Silverlight调用WCF服务,在添加WCF服务引用时,报错如下:

[align=left][align=left]HTML 文档不包含 Web 服务发现信息。

元数据包含无法解析的引用:“http://localhost:6158/WCF-Service/Service.svc”。

服务 http://localhost:6158/WCF-Service/Service.svc 不支持内容类型 application/soap+xml; charset=utf-8。客户端和服务绑定可能不匹配。

远程服务器返回错误: (415) Unsupported Media Type。

如果该服务已在当前解决方案中定义,请尝试生成该解决方案,然后再次添加服务引用。

===========================

解决办法

Web.config文件中要有如下配置:

<system.serviceModel>

<behaviors>

<serviceBehaviors>

//关键部分 Start

<behavior name="ServiceBehavior">

<serviceMetadata httpGetEnabled="true" />

<serviceDebug includeExceptionDetailInFaults="false" />

</behavior>

<behavior name="">

<serviceMetadata httpGetEnabled="true" />

<serviceDebug includeExceptionDetailInFaults="false" />

</behavior>

//关键部分 END

</serviceBehaviors>

</behaviors>

<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

<!-- WCF服务新增节点-->

<services>

<service behaviorConfiguration="ServiceBehavior" name="Service">

<endpoint address="" binding="basicHttpBinding" contract="IService">

<identity>

<dns value="localhost"/>

</identity>

</endpoint>

<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>

</service>

</services>

</system.serviceModel>

注意关键部分不可少,如果少了,在添加服务引用时就会出现上面的错误提示。

[/align][/align]