0

I am trying to build a ASP.NET Core application that should receive information in unmanaged code and present this information to the web users. When the ASP.NET Core application starts it will start up a background service that will register a callback in the unmanaged code.

In the Main function I have registered the background service to start:

 public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            }).ConfigureServices(services =>
            {
                services.AddHostedService<ReadService>();
            })
        ;
}

Then in my background service I register the callback in the unmanaged code:

public class ReadService: BackgroundService
{
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    public delegate void CSCALLBACK([In, MarshalAs(UnmanagedType.LPStr)] string a);

    [DllImport("managed_service_wrapper", CallingConvention = CallingConvention.Cdecl)]
    public static extern int SetCallback(CSCALLBACK callback);

    public ReadService()
    {
        CSCALLBACK cb = OnNewUpdates;
        var a = SetCallback(cb);
    }
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            //something
        }
    }

    static void OnNewUpdates(string a)
    {
        //something
    }

}

When calling the SetCallback function the unmanaged code will start listen for updates but will return when the start listening is started.

Will this even work? When the callback is triggered will it block the rest of the code? What should I use in OnNewUpdates to signal to ExecuteAsync that there is a new update?

Update 2020-11-02 Maybe my question wasn't so clear so to make it more clear:

  • What should I put in the ExecuteAsync function? Nothing?
  • Is it wrong to implement my service as a BackgroundService?
Pipecity
  • 73
  • 6
  • Do you meet any error when running the code? Try to set break point or add logger, then debug your code to check whether the code works well or not. Besides, about the ExecuteAsync(), do you mean you want to cancel the executing task (using the CancellationToken)? If that the is the case, try to use the [CancellationTokenSource.Cancel()](https://learn.microsoft.com/en-us/dotnet/api/system.threading.cancellationtokensource.cancel?view=netcore-3.1) to cancel the task, refer [this thread](https://stackoverflow.com/questions/29910993/). – Zhi Lv Oct 21 '20 at 08:17
  • _“What should I put in the ExecuteAsync function? Nothing?”_ – Well, what is it that you want this service to do? If it’s just for registering the callback and then doing something whenever the callback is called by the unmanaged code, then you are probably better of using the `IHostedService` interface directly instead of keeping a thread busy with a while loop that doesn’t do anything. – poke Nov 07 '20 at 11:46

1 Answers1

0

After investigating this a bit more I realized that just like @poke says having a BackgroundService is just wrong and it just needs to be a "normal" service.

Pipecity
  • 73
  • 6