[英]Call Asynchronous webservice in c#
将结果返回给用户后,我需要在C#Web服务中调用一些函数,因此我打算使用OneWay方法。
我在同一项目上创建了2个Web服务,如下所示:第一个:调用者服务:
public class Caller : System.Web.Services.WebService {
[WebMethod]
public string HelloWorld() {
var bg = new Background();
bg.HelloWorldBG();
return "Hello World";
}
}
第二个要在后台调用的服务:
[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples", SessionMode = SessionMode.Required)]
public class Background : System.Web.Services.WebService {
[SoapDocumentMethod(OneWay = true)]
[WebMethod(EnableSession = true)]
public void HelloWorldBG()
{
Thread.Sleep(60000);
var file = @"D:\testHello.txt";
File.WriteAllText(file, "Hello World");
}
}
但是,当我调用HelloWorld()时,它在完成HelloWorldBG()的执行之前不会返回
您可以通过使用方法启动新任务来运行从线程池中挑选出的单独线程:
var bg = new Background();
Task.Factory.StartNew(bg.HelloWorldBG);
return "Hello World";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.