I'm a total beginner with webservices and I ran in
the following problem, which is about a webservice and asynchronous logging. (c#)
Problem Description
I have a webservice that returns some data to a client (asp.net website).
[WebMethod]
public MyClass getData()
{
//Do some work
return _myClassObject;
}
This works pretty good so far.
Because I want to know what is happening on the webservice once it is published, I tried to implement some simple logging.
The following class (simplified) handles the logging:
public static class Logwriter
{
public static void writeToLog(string txt)
{
//writes log to db
}
}
I want this to happen asynchronous, so it won't slow down the webservice.
Therefore i changed the WebMethod:
[WebMethod]
public async Task<MyClass> getData()
{
await Task.Run(() => Logwriter.writeToLog("Someone requested the WebMethod 'GetData'"));
//Do some work
return _myClassObject;
}
After i updated the ServiceReference on my asp.net website I noticed that the webserver no longer returns a "MyClass"-Object, but a "TaskOffCustomClass"-Object. I have not found a solution to get the "MyClass"-Object from the "TaskOffMyClass"-Object, yet.
Questions
How can I get my "MyClass" Object from "TaskOffMyClass"?
Is there a possiblity to do the logging asynchronous and still return a "MyClass"-Object? (while using Task)
I thought about doing the logging in a Thread, but I also read that it is recommended to use Task over Thread. How bad would be the impact of switching to Thread?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…