Silverlight+WCF异常:期望应用程序/soap+xml,收到文本/xml

发布于 2024-07-13 10:49:01 字数 260 浏览 10 评论 0原文

我有一个 Silverlight 应用程序,我想在其中调用 WCF 服务。 调用该服务时,我收到来自服务器的以下响应:

415 无法处理消息,因为内容类型为“text/xml;” charset=utf-8' 不是预期的类型 'application/soap+xml; 字符集=utf-8

有人遇到过这个问题吗? 有谁知道我需要调整哪些配置设置? 任何有关如何解决此问题的信息将不胜感激。

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:

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦里兽 2024-07-20 10:49:01

那么,您可以尝试在VS2008中使用“Silverlight-enabled WCF Service”模板,并比较差异? 我希望您需要使用 basicHttpBinding 并使用更奇特的东西。

有关信息,以下是默认 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>

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 basicHttpBinding and are using something more exotic.

For info, here is the web.config section for a default Silverlight/WCF service:

 <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>
雨巷深深 2024-07-20 10:49:01

我在尝试将 Silverlight 应用程序连接到 WCF 服务时遇到此错误。

根本原因是 WCF 服务是使用 wsHttpBinding 绑定的,而 Silverlight 仅支持 basicHttpBinding。

因此,请检查 web.config 中的 元素,并确保您的服务的绑定信息位于 元素中,并且 <服务定义的 endpoint> 元素使用 basicHttpBinding。

I encountered this error trying to connect a Silverlight application to a WCF service.

The root cause turned out to be that the WCF Service was bound using wsHttpBinding whereas Silverlight only supports 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.

半边脸i 2024-07-20 10:49:01

可能该服务正在抛出异常。 异常消息不是服务调用期望的格式,因此出现“不是期望的类型”消息。

如果调用的方法没有在内部引发异常,请根据 Marc Gravell 的有用答案检查服务或其他配置项的安全设置。

有几种方法可以检查异常:详细查看异常消息,或跟踪 WCF 服务调用。

  1. 要查看异常消息,请在服务调用周围放置一个 try-catch 并在 catch 块中设置断点。 这将允许您检查异常内容。 您可能希望暂时将服务配置为在错误消息中包含异常详细信息

  2. 您可以通过启用服务的消息日志记录来轻松跟踪 WCF 消息。 您可以在配置文件中执行此操作(请参阅配置消息日志记录)或使用 WCF 服务配置编辑器(可在 VS 2008 工具菜单下或右键单击配置文件使用)。 然后使用服务跟踪查看器浏览日志文件。 查看器是 SDK 的一部分,可以在此处找到:“C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\SvcTraceViewer.exe”

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.

There are a couple of ways to examine the exception: Looking at the exception message in detail, or tracing the WCF service calls.

  1. 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 temporarily to include exception details in the fault message.

  2. 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".

没有你我更好 2024-07-20 10:49:01

您可以使用 WebOperationContext 类

作为示例,以下代码展示了如何使用此类将内容类型设置为 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);
    }
}

如果您感兴趣,此示例旨在启用跨域调用自托管 WCF Web 服务中的 Silverlight 客户端,请查看 此处了解更多信息,我有一个代码下载 附加到这篇文章

在您的情况下,对于 WCF 服务的响应,您可以将内容类型设置为 application/soap+xml 并使用 UTF-8。

WebOperationContext 类位于 System.ServiceModel.Web 程序集中,并且是 .NET Framework 3.5 的一部分。

希望这可以帮助。

You can change the content type of the response in any method on your WCF web service using the WebOperationContext class.

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:

[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 here for more and I have a code download attached to this post.

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.

The WebOperationContext class is in the System.ServiceModel.Web assembly and is part of .NET Framework 3.5.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文