views:

608

answers:

2

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?

A: 

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.

AnthonyWJones
Would that class be contained in my web service, or on the external site?I'd rather not have to tell the client to add more code on their end.
It would have to be on the external site, its the one with access to the Context.
AnthonyWJones
+5  A: 

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.

Joseph
+1 exactly - this would also open up the method to callers that are *not* coming from an ASP.NET app. A Winforms app doesn't have a HttpContext and could thus never call this "LogError" method - not your best choice in terms of design...
marc_s
@Joseph: What will this custom class look like? What kind of parameters is it looking for? What exactly do you mean by 'encapsulate the information'?
rohanbk
@rohanbk It would be a POCO class that would just have properties with the information that you want. You would set the properties (via the HttpContext), and then use that class across the wire (since you should be able to mark the POCO as Serializable)
Joseph