接收html的WCF ProtocolException

Mat*_*han 2 wcf

从WCF访问方法到DotNet时,我收到以下错误

内容类型text/html; 响应消息的charset = utf-8与绑定的内容类型不匹配(text/xml; charset = utf-8).如果使用自定义编码器,请确保正确实现IsContentTypeSupported方法.响应的前1024个字节是:'Runtime Error body {font-family:"Verdana"; font-weight:normal; font-size:.7em; color:black;} p {font-family:"Verdana"; font-weight:normal; color:black; margin-top:-5px} b {font-family:"Verdana"; font-weight:bold; color:black; margin-top:-5px} H1 {font-family: "Verdana"; font-weight:normal; font-size:18pt; color:red} H2 {font-family:"Verdana"; font-weight:normal; font-size:14pt; color:maroon} pre {font-家庭:"Lucida Console"; font-size:.9em} .marker {font-weight:bold; color:black; text-decoration:none;} .version {color:grey;} .error {margin-bottom:10px;} .expandable {text-decoration:underline; 字体重量:粗体; 颜色:海军; 光标:手; }

<body bgcolor="white">

        <span><H1>Server Error in '/ORSXMLWCFServiceNew' Application.<hr width=100% size=1 color=silver></H1>

        <h2> <i>Runtime Error</i> </'.
Run Code Online (Sandbox Code Playgroud)

请帮助我.

mna*_*mov 6

我找到了一种如何获得完整响应的方法,而不仅仅是无用的1024字节......

using (var client = new Service1Client())
{
    try
    {
        Console.WriteLine(client.DoWork());
    }
    catch (ProtocolException e)
    {
        var webException = e.InnerException as WebException;

        var responseString = webException.ExtractResponseString();

        if (string.IsNullOrEmpty(responseText))
            Console.WriteLine(e);
        else
            Console.WriteLine(responseString);
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
    }
}
Run Code Online (Sandbox Code Playgroud)

其中使用以下辅助方法

public static string ExtractResponseString(this WebException webException)
{
    if (webException == null || webException.Response == null)
        return null;

    var responseStream = webException.Response.GetResponseStream() as MemoryStream;

    if (responseStream == null)
        return null;

    var responseBytes = responseStream.ToArray();

    var responseString = Encoding.UTF8.GetString(responseBytes);
    return responseString;
}
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅我的博客http://mnaoumov.wordpress.com/2012/09/28/wcf-protocolexception/

  • 不适合我,因为e.InnerException为null.有任何想法吗?我使用的是.Net 4.0. (3认同)