Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

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

web services - C# Webservice: Webmethod, that calls an async method, returns Taskoff object

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

  1. How can I get my "MyClass" Object from "TaskOffMyClass"?

  2. Is there a possiblity to do the logging asynchronous and still return a "MyClass"-Object? (while using Task)

  3. 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

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

1 Answer

0 votes
by (71.8m points)

I'm a total beginner with webservices

I recommend you learn a modern framework. I don't even know how old ASMX is, now... According to what I could find, it's "not dead", but I think that's the same kind of "not dead" that Silverlight is. In other words, it's dead but they don't want to officially say it's dead.

In particular, I don't think that ASMX understands async or tasks at all. I strongly recommend learning ASP.NET WebAPI (or WCF if you really want SOAP).

However, if you have existing web methods that you need to maintain, then...

The first thing to realize - as Crowcoder pointed out - is that you probably don't want to do fire-and-forget. If it works well enough, then just keep it like this:

[WebMethod]
public MyClass getData()
{
  Logwriter.writeToLog("Someone requested the WebMethod 'GetData'");
  //Do some work
  return _myClassObject;
}

If you recognize and accept the drawbacks of unprotected background operations (specifically, that they can be aborted without any way for you to detect that, meaning that your log may be missing some messages), then you can use something like HostingEnvironment.QueueBackgroundWorkItem:

[WebMethod]
public MyClass getData()
{
  HostingEnvironment.QueueBackgroundWorkItem(() => Logwriter.writeToLog("Someone requested the WebMethod 'GetData'"));
  //Do some work
  return _myClassObject;
}

I have a description of alternative approaches on my blog.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...