3

我有一个自托管网络服务(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 页面,这有时会提供一个损坏的页面(参见双 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。

4

2 回答 2

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" />-->
于 2012-11-16T20:37:40.320 回答
0

我们也面临同样的问题。设置或删除设置有帮助logMessagesAtTransportLevelfalse

于 2012-11-28T13:30:15.817 回答