C#Web服务:调用异步方法的Web方法返回Taskoff对象 [英] C# Webservice: Webmethod, that calls an async method, returns Taskoff object

查看:176
本文介绍了C#Web服务:调用异步方法的Web方法返回Taskoff对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用Web服务的初学者,因此我参加了 以下问题,与Web服务和异步日志记录有关. (c#)

I'm a total beginner with webservices and I ran in the following problem, which is about a webservice and asynchronous logging. (c#)

问题描述

我有一个Web服务,可将一些数据返回到客户端(asp.net网站).

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.

由于我想知道Web服务发布后的状况,因此我尝试实现一些简单的日志记录.

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.

因此,我更改了WebMethod:

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;
}

在我的asp.net网站上更新ServiceReference之后,我注意到Web服务器不再返回"MyClass"对象,而是"TaskOffCustomClass"对象.我还没有找到从"TaskOffMyClass"对象获取"MyClass"对象的解决方案.

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.

问题

  1. 如何从"TaskOffMyClass"中获取"MyClass"对象?

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

是否可以异步进行日志记录并仍然返回"MyClass" -Object? (使用Task时)

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?

推荐答案

我是使用Web服务的初学者

I'm a total beginner with webservices

我建议您学习现代的框架.现在,我什至不知道ASMX的年龄...据我发现,它还没死",但我认为这与Silverlight一样.换句话说,它已经死了,但他们不想正式说它已经死了.

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.

尤其是,我认为ASMX根本不了解async或任务.我强烈建议学习ASP.NET WebAPI(如果您真的想要SOAP,则可以学习WCF).

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

但是,如果您需要维护现有的Web方法,那么...

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

首先要意识到的-正如 Crowcoder所指出的-您可能不想开火-忘记了.如果效果足够好,那就保持它像这样:

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;
}

我对替代方法进行了说明在我的博客上.

这篇关于C#Web服务:调用异步方法的Web方法返回Taskoff对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆