Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
796 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...