Silverlight+WCF exception: Expecting application/s

2020-06-09 08:49发布

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.

4条回答
▲ chillily
2楼-- · 2020-06-09 08:58

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.

查看更多
叛逆
3楼-- · 2020-06-09 09:08

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

查看更多
冷血范
4楼-- · 2020-06-09 09:15

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.

查看更多
【Aperson】
5楼-- · 2020-06-09 09:21

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>
查看更多
登录 后发表回答