简体   繁体   中英

The content type text/xml; charset=“utf-8” of the response message does not match the content type of the binding (text/xml; charset=utf-8)

Please note that this question pertains to the .NET Core implementation of WCF Connected Services.

I am porting a regular .NET WCF client over to .NET Core, but I ran into this issue:

The content type text/xml; charset="utf-8" of the response message does
not match the content type of the binding (text/xml; charset=utf-8).

If using a custom encoder, be sure that the IsContentTypeSupported method is
implemented properly. The first 1024 bytes of the response were: 
'<?xml version='1.0' encoding='UTF-8'?> [...]

The response indeed contains the quotes:

HTTP/1.1 200 Ok
content-type: text/xml; charset="utf-8"

I never did anything special to handle this in WCF proper. Is this a bug in the .NET Core version, or is it just really specific about the content type (utf-8 vs "utf-8")?

How can I change the expected content type to match the service I'm calling? (I have no control over that, but I can copy and alter the WSDL if needed).

I'm using a svcutil-generated client. (Connected Service)

It would indeed seem that the .NET Core version is more picky about this. In any case, I managed to solve it using a Custom Encoder.

I blatently stole the CustomTextMessageEncoder from Github . I added the following method:

public override bool IsContentTypeSupported(string contentType)
{
    return true;
}

And stole CustomTextMessageBindingElement and CustomTextMessageEncoderFactory from the same place.

I added them by creating a custom binding (basicBinding is the binding I had before):

var customBindingElement = new CustomTextMessageBindingElement("UTF-8", "text/xml", MessageVersion.Soap11);
var binding = new CustomBinding(basicBinding);
binding.Elements.RemoveAt(0);
binding.Elements.Insert(0, customBindingElement);
var client = (T2)Activator.CreateInstance(typeof(T), binding, address);

I use Activator as I generate my proxies dynamically. Just replace with a call to the WCF generated client.

Quite a lot of work for two misplaced quotes :D

I was getting the exact same error after converting a WCF Client to dotnet core. I have to refactor some. I replaced my binding code to this. Note WsBinding isn't native in dotnet core.

https://github.com/dotnet/wcf/issues/1370

I had the same problem also. There might be an issue with security-related or content-type format differences. I got over the problem by ignoring ServiceReference bindings. You should directly call with HttpWebRequest As a sample;

  private string GetWebService(string token, int testId, string phone)
    {
        try
        {
            var serviceUrl = "https://test.com/Soap";

            var httpWebRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);
            httpWebRequest.ContentType = "text/xml";
            httpWebRequest.Method = "POST";

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {

                var xmlData = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:doc=\"http://test.com\">" +
                "   <soapenv:Header/>" +
                "   <soapenv:Body>" +
                "      <doc:AskForStackOverFlow>" +
                "         <doc:token>" + token + "</doc:token>" +
                "         <doc:testId>" + testId+ "</doc:testId>" +
                "         <doc:phone>" + phone + "</doc:phone>" +
                "         <doc:startTime>" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "</doc:startTime>" +
                "      </doc:AskForStackOverFlow>" +
                "   </soapenv:Body>" +
                "</soapenv:Envelope>";

                streamWriter.Write(xmlData);
            }

            var xmlResult = "";
            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                xmlResult = streamReader.ReadToEnd();
            }

            if (string.IsNullOrEmpty(xmlResult))
                return "";


            var result = "";
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xmlResult);
            XmlNodeList nodes = doc.GetElementsByTagName("ns1:AskForStackOverFlowResult");
            foreach (XmlNode item in nodes)
            {
                result = item.InnerText;
            }

            return result;

        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question The content type application/xml;charset=utf-8 of the response message does not match the content type of the binding (text/xml; charset=utf-8) The content type application/xml;charset=utf-8 of the response message does not match the content type of the binding (text/xml; charset=utf-8), WCF The content type text/html; charset=UTF-8 of the response message does not match the content type of the binding (text/xml; charset=utf-8) The content type text/plain of the response message does not match the content type of the binding (text/xml; charset=utf-8) wcf + The content type text/html of the response message does not match the content type of the binding (application/soap+xml; charset=utf-8) WCF http binding error: The content type text/html does not match the content type of the binding (text/xml; charset=utf-8) WCF Service Client: The content type text/html; charset=utf-8 of the response message does not match the content type of the binding HTTP 415 Cannot process the message because the content type 'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8' Cannot process the message because the content type 'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8' C# ASMX Service throwing The content type text/html; charset=UTF-8 of the response message does not match the content type error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM