WCF HTTP服务:安装vs2012破坏了应用程序

时间:2012-11-16 14:30:50

标签: wcf .net-4.0 .net-4.5

我有一个自主的webservice( WebServiceHost ),它也提供html网站(包含JavaScript文件,图像等)。该服务在.net 4.0(使用vs2010)中运行良好。

现在我已经安装了vs2012并且我的应用程序似乎被破坏了(仍然使用vs2010 - 也许安装.net 4.5是原因)。编译很好,我也没有运行时错误,但在某些情况下,html和JavaScript似乎被打破了。以下是(我认为)相关的代码片段:

使用的接口(System.Data.Services):

public interface IRequestHandler
{
    Message ProcessRequestForMessage(Stream messageBody);
}

创建消息:

string filePath = _physicalPath + string.Join("\\", incomingRequest.UriTemplateMatch.RelativePathSegments.ToArray());
HttpResponseMessageProperty responseProperty = new HttpResponseMessageProperty();

if (File.Exists(filePath) && SetContentType(responseProperty.Headers, Path.GetExtension(filePath).ToLower()))
{
    Message message = Message.CreateMessage(MessageVersion.None, "", HttpServiceBodyWriter.Create(filePath));
    message.Properties.Add(WebBodyFormatMessageProperty.Name, new WebBodyFormatMessageProperty(WebContentFormat.Raw));
    message.Properties.Add(HttpResponseMessageProperty.Name, responseProperty);
}

HttpServiceBodyWriter:

class HttpServiceBodyWriter : StreamBodyWriter
{
    private System.IO.Stream _source = null;

    private HttpServiceBodyWriter(System.IO.Stream source) : base(false)
    {
        _source = source;
    }

    protected override void OnWriteBodyContents(System.IO.Stream stream)
    {
        _source.CopyTo(stream);
    }

    internal static HttpServiceBodyWriter Create(System.Net.WebResponse response)
    {
        return new HttpServiceBodyWriter(response.GetResponseStream());
    }

    internal static HttpServiceBodyWriter Create(string filePath)
    {
        return new HttpServiceBodyWriter(File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
    }
}

如果我请求HTML页面,这有时会导致页面损坏(请参阅double dtd定义和“head”之后的空标记):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <>
    .....

有时会传递正确的页面:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="UTF-8" />
....

初始页面请求似乎始终提供正确的页面。后续请求变得腐败。

有没有人解释这种奇怪的行为?任何帮助表示赞赏(这个问题让我想起了c ++时代)。

这是App.Config的serviceModel部分:

<system.serviceModel>
  <diagnostics>
    <messageLogging logEntireMessage="true" logMalformedMessages="true" logMessagesAtServiceLevel="false" logMessagesAtTransportLevel="true" maxMessagesToLog="300000" maxSizeOfMessageToLog="200000" />
  </diagnostics>
  <services>
    <service name="WcfTest.HttpService" behaviorConfiguration="DataServiceBehavior">
      <endpoint name="DataService" binding="webHttpBinding" bindingConfiguration="DataService" contract="System.Data.Services.IRequestHandler" />
      <host>
        <baseAddresses>
          <add baseAddress="http://localhost:8080/TestSite/" />
        </baseAddresses>
      </host>
    </service>
  </services>
  <behaviors>
    <endpointBehaviors />
    <serviceBehaviors>
      <behavior name="DataServiceBehavior">
        <serviceDebug includeExceptionDetailInFaults="False" />
        <dataContractSerializer maxItemsInObjectGraph="1000000" />
        <serviceMetadata httpGetEnabled="true" />
        <serviceThrottling maxConcurrentCalls="200" maxConcurrentInstances="2147483647" maxConcurrentSessions="200" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <bindings>
    <webHttpBinding>
      <binding name="DataService" maxReceivedMessageSize="5000000" maxBufferPoolSize="5000000" maxBufferSize="5000000" closeTimeout="00:03:00" openTimeout="00:03:00" receiveTimeout="00:10:00" sendTimeout="00:03:00">
        <readerQuotas maxStringContentLength="5000000" maxArrayLength="5000000" maxBytesPerRead="5000000" />
        <security mode="None"/>
      </binding>
    </webHttpBinding>
  </bindings>
</system.serviceModel>

更新

我创建了一个小项目,您可以在其中重现此问题。您可以在此处下载:https://skydrive.live.com/redir?resid=CBC3C885DE2032B8!131&authkey=!ALZOaSL0V0s-itM

只需按几次浏览器的刷新按钮,直到出现损坏的html。

2 个答案:

答案 0 :(得分:1)

这是启用消息日志记录时webHttpBinding + 4.5的已知问题。与http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/2bc5e5a6-1971-46cb-8b4f-f1c46130faca相同。 在服务中启用消息日志记录时会发生此问题。在针对此问题提供修复之前,请通过在配置中注释掉这些行来禁用应用配置文件中的消息记录。我验证了您的应用程序在禁用邮件日志记录方面正常工作。希望这有帮助!

<!--
<messageLogging logEntireMessage="true" logMalformedMessages="true" logMessagesAtServiceLevel="false" logMessagesAtTransportLevel="true"
maxMessagesToLog="300000" maxSizeOfMessageToLog="200000" />-->

答案 1 :(得分:0)

我们遇到了同样的问题。将logMessagesAtTransportLevel设置为false或删除设置有所帮助。