c# - Eventhandlers in my WebApi -


currently have connect plc-terminal(tcp/sockets based). part manufacturer has provided dll abstracts functionality me. bad part, programmed eventhandlers. simplified example of this

public void getoutputs(string id) {         configurationmanager cm = new configurationmanager();     cm.getoutputresult += ongetoutputresult;         cm.getoutputasync(id); }  private void ongetoutputresult(output output) {     //do here output when not null } 

i want create webapi project 'clients'(uwp,xamarin,asp mvc) can access terminal thru http , there no fuss portable or .net core libraries can't reference full .net framework dll manufacturer.

so question is: possible these things in webapi? there way convert these callbacks nicely awaitable tasks?

public class outputscontroller : apicontroller {     public ihttpactionresult get(string id)     {         configurationmanager cm = new configurationmanager();         //task/async/await magic here         return ok(output); // or notfound();     } 

regards, miscode

you can use taskcompletionsource designed kind of scenario

private taskcompletionsource<output> tcs;  public task<output> getoutputs(string id) {         tcs = new taskcompletionsource<output>();      configurationmanager cm = new configurationmanager();     cm.getoutputresult += ongetoutputresult;         cm.getoutputasync(id);      // task complete once tcs.setresult or similar has been called     return tcs.task; }  private void ongetoutputresult(output output) {     if (tcs == null) {         throw new fatalexception("taskcompletionsource wasn't instantiated before called");     }      // tcs calls here signal task has happened.     if (output == null) {        // demoing functionality        // can set exceptions        tcs.setexception(new nullreferenceexception());        return;     }      // or if we're happy result can send if , finish task     tcs.setresult(output); } 

in api:

public class outputscontroller : apicontroller {     public async task<ihttpactionresult> get(string id)     {         configurationmanager cm = new configurationmanager();          var output = await cm.getouputs(id);          return ok(output); // or notfound();     } 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -