[英]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.