Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
799 views
in Technique[技术] by (71.8m points)

c# - "System.Web.HttpContext cannot be serialized because it does not have a parameterless constructor."

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...