接收html的WCF ProtocolException

时间:2012-05-11 10:00:24

标签: wcf

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

内容类型text / html;响应消息的charset = utf-8与绑定的内容类型不匹配(text / xml; charset = utf-8)。如果使用自定义编码器,请确保正确实现IsContentTypeSupported方法。响应的前1024个字节是:'              运行时错误                   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-family:“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> </'.

请帮帮我。

2 个答案:

答案 0 :(得分: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);
    }
}

使用以下辅助方法

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;
}

有关详细信息http://mnaoumov.wordpress.com/2012/09/28/wcf-protocolexception/

,请参阅我的博客

答案 1 :(得分:0)

这是由WCF服务上的错误引起的,修复它的最佳方法(在检查代码中的任何显而易见的事情之后)将在WCF服务上获取一些错误记录并检查是否有任何异常。日志。

这些可能会有所帮助:

Adding logging to WCF

Creating a logging class