Search code examples
c#webclientiis-express

WebClient.UploadString Returns 500 Submitting to Web Service Running On IIS Express


I'm using .NET Framework 4.5.1 WebClient and posting to a *.ashx that I have running locally so I can debug. The client site uses code like this:

var cli = new WebClient();
cli.Headers[ HttpRequestHeader.ContentType ] = "application/json";

var job = new
{
    Inputs = inputs,
    InputTables = inputTables,
    ...
};

var configuration = Newtonsoft.Json.JsonConvert.SerializeObject( job );

var response = cli.UploadString(
    serviceUrl,
    configuration
);

The serviceUrl is http://localhost:54300/Calculation.ashx and is running under IIS Express.

The web service returns the result via:

    private void ReturnResponse( HttpContext context, JObject result )
    {
        using ( var textWriter = new StreamWriter( context.Response.OutputStream ) )
        {
            context.Response.ContentType = "application/json";
            using ( var jsonWriter = new JsonTextWriter( textWriter ) )
            {
                result.WriteTo( jsonWriter );
            }
        }
    }

This all works on my own personal computer, but on my company issued computer, it doesn't work and I'm at a loss of where to look. I assume it is some sort of security setting, or at least I'm hoping.

On my company computer, the client site successfully submits to the web service, I've stepped through the web service code and all works there and it returns the result via ReturnResponse and all looks correct. But, the client site then throws an 500 error 'The remote server returned an error: (500) Internal Server Error.' without much info to go on.

Additionally: This code works fine if the client site (running on my company computer) submits to the web service when the web service is hosted on the web (obviously running under IIS). So the code is fine - in my opinion :) - but only presents problem when web service is running locally.

Anyone know of some sort security/configuration setting that might be causing this? As I said, it worked fine on multiple personal machines without issue.


Solution

  • The 500 exception thrown was of type HttpException, so I read the response and discovered this error message:

    Unable to make the session state request to the session state server. Please ensure that the ASP.NET State service is started and that the client and server ports are the same. If the server is on a remote machine, please ensure that it accepts remote requests by checking the value of HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\aspnet_state\Parameters\AllowRemoteConnection. If the server is on the local machine, and if the before mentioned registry value does not exist or is set to 0, then the state server connection string must use either 'localhost' or '127.0.0.1' as the server name.

    So I simply had to turn on ASP.NET State Service. I could make a web.config transform to have debug/local simply run in regular inproc session, and then have the release build use the State Service (which is required in our case), but turning it on locally is good enough for now.