Wednesday, 18 September 2013

WCF WebHttpBinding Craches Web Server

WCF WebHttpBinding Craches Web Server

I am banging my head against the wall here. I have a service that takes as
input an object on a POST. When debugging I foind that my service resieves
the result and performs the as expected but at the end of the method as I
return out of the webmethod I get an error:
System.Net.WebException: The underlying connection was closed: An
unexpected error occurred on a receive. ---> System.IO.IOException: Unable
to read data from the transport connection: An existing connection was
forcibly closed by the remote host. --->
System.Net.Sockets.SocketException: An existing connection was forcibly
closed by the remote host
I can not for the life of me figure out what is going on, i'm assuming its
either how i'm calling the service or config, does anyone have any ideas?
My contract looks like:
[ServiceContract]
public interface IHouseholdService
{
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "GetHouseholds",
ResponseFormat = WebMessageFormat.Json, RequestFormat =
WebMessageFormat.Json)]
HouseholdDto[] GetHouseholdsByCriteria(HouseholdSearch criteria);
}
and my config looks like this:
</endpoint>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="webBehaviour">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="Default">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
I'm calling it like this:
var dto = new HouseholdSearch() {HouseholdNumber = "16653373"};
var url = "http://localhost:48460/Household.svc/GetHouseholds";
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/json; charset=utf-8";
var ser = new DataContractJsonSerializer(dto.GetType());
var ms = new MemoryStream();
ser.WriteObject(ms, dto);
String json = Encoding.UTF8.GetString(ms.ToArray());
var writer = new StreamWriter(request.GetRequestStream());
writer.Write(json);
writer.Close();
WebResponse responce = request.GetResponse();
Stream reader = responce.GetResponseStream();
var sReader = new StreamReader(reader);
var outResult = sReader.ReadToEnd();
sReader.Close();
Any help would be super helpful!!!

No comments:

Post a Comment