OPCFoundation / UA-.NETStandard

OPC Unified Architecture .NET Standard
Other
1.95k stars 945 forks source link

Add async delegate handlers when address is called #2203

Open juaal12 opened 1 year ago

juaal12 commented 1 year ago

Type of issue

Current Behavior

Right now when an address is called the delegate handler works synchronously. An async function is not able to be used since it is not supported by the package. Just a small workaround has to be done but it is not the best way of handling Tasks in my opinion. Find here some attached code:

public ServiceResult Handle(ISystemContext context,
    IList<object> inputArguments, IList<object> outputArguments)
{
    // Start the asynchronous operation on a separate thread
    var task = Task.Run(() => HandleAsync(context, inputArguments, outputArguments));

    // Synchronously wait for the task to complete
    task.Wait();

    // Return the result of the completed task
    return task.Result;
}

private async Task<ServiceResult> HandleAsync(ISystemContext context,
    IList<object> inputArguments, IList<object> outputArguments)
{
    // Your asynchronous logic here
}

Expected Behavior

I think the package should handle async methods. Not sure it is supported by the communication standard but if it is supported, the async delegate handler should be implemented in this package.

Steps To Reproduce

  1. Open reference server project
  2. Go to reference node manager and for Add function for example, try to attach to the handler an async method.
  3. It wont work so you will have to use the workaround written on this comment.

Environment

The environment is not important here.

Anything else?

No response

mregen commented 1 year ago

Hi @juaal12, is this for server development? Client has async service calls.

juaal12 commented 1 year ago

Hi @juaal12, is this for server development? Client has async service calls.

Yes this is for server development. Right now when you create an address space and you call it using an external tool for example UA Expert, if the handler is synchronous it works like a charm but if the handler is asynchronous it does not get called.