6
votes

I've created a web service that other sites can use to store errors in my database. They can then come to my site to view their errors, search through errors, filter errors, etc. However, I'm getting the following error for my web service:


System.Web.HttpContext cannot be serialized because it does not have a parameterless constructor.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: System.Web.HttpContext cannot be serialized because it does not have a parameterless constructor.


The web service contains the following function:

[WebMethod]
public static void LogError(HttpContext context, Exception exception, string APIKey)
{
    //Log the error
}

The external site that is using the web service to log exceptions contains the following code in the global.asax file:

void Application_Error(object sender, EventArgs e) 
{ 
    // Code that runs when an unhandled error occurs

    WebService.Errors.ErrorHandler.LogError(HttpContext.Current, Server.GetLastError(), "NOLDFHOI");
}

How can I get the HttpContext from their site into my function from the web service?

2

2 Answers

9
votes

You're not going to be able to serialize the HttpContext. Your best bet would be to create a custom class to encapsulate the information that you want out of the HttpContext and pass that into your WebMethod.

0
votes

You will need to create a class that is serialisable designed to hold the sort of information that you wish to gather. Then create Static method on that class which will create an new instance of the class and gather up the info required.

Its would be this new class that would be passed to LogError.