Heba El-Fadly
Heba El-Fadly

Reputation: 301

Call Asynchronous webservice in c#

I need to invoke some functions in a C# webservice after returning the result to the user, so I intend to use a OneWay method.

I created 2 webservices on the same project as following: 1st: the caller service:

public class Caller : System.Web.Services.WebService {

[WebMethod]
public string HelloWorld() {

    var bg = new Background();
    bg.HelloWorldBG();
    return "Hello World";
}
}

2nd the service to be invoked in the background:

[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");
}    

}

but when I call HelloWorld() it doesn't return before completing the execution of HelloWorldBG()

Upvotes: 0

Views: 1744

Answers (1)

AgentFire
AgentFire

Reputation: 9770

You could run a separate thread picked out from thread pool by starting a new task with your method:

var bg = new Background();
Task.Factory.StartNew(bg.HelloWorldBG);
return "Hello World";

Upvotes: 1

Related Questions