Silverlight+WCF exception: Expecting application/soap+xml, received text/xml

前端 未结 4 927
遥遥无期
遥遥无期 2021-02-13 18:07

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:

4条回答
  •  礼貌的吻别
    2021-02-13 18:30

    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.

提交回复
热议问题