.net Silverlight+WCF 异常:期望应用程序/soap+xml,收到文本/xml
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/493883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Silverlight+WCF exception: Expecting application/soap+xml, received text/xml
提问by Kevin
I have a Silverlight application in which I would like to call a WCF service. When calling the service I receive the following response from the server:
我有一个 Silverlight 应用程序,我想在其中调用 WCF 服务。调用服务时,我从服务器收到以下响应:
415 Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8
415 无法处理消息,因为内容类型为 'text/xml; charset=utf-8' 不是预期的类型 'application/soap+xml; 字符集=utf-8
Has anyone experienced this problem before? Does anyone know which configuration settings I need to adjust? Any information on how to fix this would be appreciated.
有没有人遇到过这个问题?有谁知道我需要调整哪些配置设置?任何有关如何解决此问题的信息将不胜感激。
采纳答案by Marc Gravell
Well, you could try using the "Silverlight-enabled WCF Service" template in VS2008, and comparing the differences? I expect that you need to use the basicHttpBindingand are using something more exotic.
那么,您可以尝试使用VS2008中的“启用Silverlight的WCF服务”模板,并比较差异?我希望您需要使用basicHttpBinding并且正在使用更奇特的东西。
For info, here is the web.config section for a default Silverlight/WCF service:
有关信息,这里是默认 Silverlight/WCF 服务的 web.config 部分:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="MySite.Service1Behavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<services>
<service behaviorConfiguration="MySite.Service1Behavior"
name="MySite.Service1">
<endpoint address="" binding="basicHttpBinding"
contract="MySite.Service1" />
<endpoint address="mex" binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
回答by ScottB
I encountered this error trying to connect a Silverlight application to a WCF service.
我在尝试将 Silverlight 应用程序连接到 WCF 服务时遇到此错误。
The root cause turned out to be that the WCF Service was bound using wsHttpBinding whereas Silverlight only supports basicHttpBinding.
根本原因原来是 WCF 服务是使用 wsHttpBinding 绑定的,而 Silverlight 仅支持 basicHttpBinding。
So check your <bindings>element in web.config and make sure binding information for your service is in the <basicHttpBinding>element and that the <endpoint>element of your service definition uses basicHttpBinding.
因此,请检查您<bindings>在 web.config 中的元素,并确保您的服务的绑定信息在该<basicHttpBinding>元素中,并且<endpoint>您的服务定义的元素使用 basicHttpBinding。
回答by Tom A
Probably the service is throwing an exception. The exception message is not in the format expected by the service call, hence the "not the expected type" message.
可能是服务抛出异常。异常消息的格式不是服务调用所期望的格式,因此是“非预期类型”消息。
If the method called is not throwing an exception internally, check your security settings for the service or the other configuration items, per Marc Gravell's helpful answer.
如果调用的方法没有在内部抛出异常,请根据 Marc Gravell 的有用回答检查服务或其他配置项的安全设置。
There are a couple of ways to examine the exception: Looking at the exception message in detail, or tracing the WCF service calls.
有几种方法可以检查异常:详细查看异常消息,或跟踪 WCF 服务调用。
To see the exception message put a try-catch around the service call and set a breakpoint in the catch block. This will allow you to examine the exception contents. You may want to configure the service temporarilyto include exception details in the fault message.
You can trace WCF messages easily by enabling message logging for the service. You can do this in the config file (see Configuring Message Logging) or using the WCF Service Configuration Editor (available under VS 2008 Tools menu or by right clicking the config file). Then use the Service Trace Viewer to browse the log file. The viewer is part of the SDK and may be found here:
"C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\SvcTraceViewer.exe".
要查看异常消息,请在服务调用周围放置 try-catch 并在 catch 块中设置断点。这将允许您检查异常内容。您可能希望临时配置该服务以在故障消息中包含异常详细信息。
您可以通过为服务启用消息日志记录来轻松跟踪 WCF 消息。您可以在配置文件中执行此操作(请参阅配置消息日志记录)或使用 WCF 服务配置编辑器(在 VS 2008 工具菜单下或通过右键单击配置文件可用)。然后使用服务跟踪查看器浏览日志文件。查看器是 SDK 的一部分,可以在此处找到:
"C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\SvcTraceViewer.exe"。
回答by Peter McG
You can change the content type of the response in any method on your WCF web service using the WebOperationContext class.
您可以使用WebOperationContext 类在 WCF Web 服务上的任何方法中更改响应的内容类型。
Just as an example the following code shows how to use this class to set the content-type to application/xml and return a UTF-8 encoded stream:
作为示例,以下代码显示了如何使用此类将内容类型设置为 application/xml 并返回 UTF-8 编码流:
[ServiceContract]
public interface IPolicyProvider
{
[OperationContract, WebGet(UriTemplate = "/ClientAccessPolicy.xml")]
Stream ProvidePolicy();
}
public sealed class StockService : IPolicyProvider
{
public Stream ProvidePolicy()
{
WebOperationContext.Current.OutgoingResponse.ContentType = "application/xml";
return new MemoryStream( Encoding.UTF8.GetBytes(File.ReadAllText("ClientAccessPolicy.xml")) , false);
}
}
If you're interested this example is for the purpose of enabling cross-domain calls for Silverlight clients in a self-hosted WCF web service, have a look herefor more and I have a code download attached to this post.
如果您对此示例感兴趣,该示例旨在为自托管 WCF Web 服务中的 Silverlight 客户端启用跨域调用,请查看此处了解更多信息,我在此帖子中附有代码下载。
In your situation, for the response from your WCF service you would set the content type to be application/soap+xml and use UTF-8.
在您的情况下,对于来自 WCF 服务的响应,您可以将内容类型设置为 application/soap+xml 并使用 UTF-8。
The WebOperationContext class is in the System.ServiceModel.Web assembly and is part of .NET Framework 3.5.
WebOperationContext 类位于 System.ServiceModel.Web 程序集中,是 .NET Framework 3.5 的一部分。
Hope this helps.
希望这可以帮助。

