I have a selfhosted webservice (WebServiceHost) that also delivers html sites (with JavaScript files,images etc.) The service runs fine in .net 4.0 (with vs2010).
Now I’ve installed vs2012 and my app seems to be broken (still using vs2010 – perhaps the installation of .net 4.5 is the cause). It compiles fine and I also have no runtime errors, but html and JavaScript seem to be broken under some circumstances. Here are the (I think) relevant codefragments:
Used Interface (System.Data.Services):
public interface IRequestHandler
{
Message ProcessRequestForMessage(Stream messageBody);
}
Creation of the message:
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));
}
}
If I request a HTML page, this delivers sometimes a corrupt page (see the double dtd definition and the empty tag after “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>
<>
.....
Somtimes the correct page is delivered:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="UTF-8" />
....
It seems like the initial page request always delivers a correct page. Subsequential request go corrupt.
Has anyone an explanation for this strange behaviour? Any help appreciated (This problem reminds me of the c++ days).
This is the serviceModel section of the App.Config:
<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>
Update
I’ve created a small project, where you can reproduce this problem. You can download it here: https://skydrive.live.com/redir?resid=CBC3C885DE2032B8!131&authkey=!ALZOaSL0V0s-itM
simply press the refresh button of the browser several times until the corrupted html appears.
This is a known issue with webHttpBinding + 4.5 on enabling message logging. And is the same as http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/2bc5e5a6-1971-46cb-8b4f-f1c46130faca.
This issue happens when message logging is enabled in the service. Until a fix is available for this issue please disable message logging in your app config file by commenting out these lines from your config. I verified that your application works fine on disabling message logging. Hope this helps!