I'm working on a asp.net (.Net 5) application that hosts a signalR hub to allow to clients to communicate with canon cameras (eg. taking photos, capture videos and live streaming) using the official canon sdk. It works perfectly on windows. However, on MacOS calling the c++ library results in a deadlock unless calling on the main thread. All I could find was that the mac sdk isn't thread safe, so it should be used in any other thread.
For example, this code works:
class Program
{
static void Main(string[] args)
{
SdkWrapper.EdsInitializeSDK();
}
}
static class SdkWrapper
{
[DllImport("EDSDK")]
public static extern int EdsInitializeSDK();
}
But either running on another thread or in async await context, the function doesn't return because of the deadlock.
class Program
{
static async Task Main(string[] args)
{
await Task.Run(SdkWrapper.EdsInitializeSDK);
var thread = new Thread(() => SdkWrapper.EdsInitializeSDK());
thread.Start();
}
}
static class SdkWrapper
{
[DllImport("EDSDK")]
public static extern int EdsInitializeSDK();
}
So, one work around could be to run all the sdk function calls in the main thread (probably it's a bad idea). Is there any way to make it work with asp.net?
Or does anybody know a better way to use the canon sdk with .net on MacOS?
The Canon SDK isn't thread safe on Windows either and you must have been lucky so far to not have problems yet. The difference between Windows and macOS is that the SDK is able to work on any (STA-) thread but on macOS it forcefully runs on the main thread. Overall the Canon SDK is really not suitable for use in a web application.
My suggestion would be to have the camera code run inside a different process and have the web app communicate with it via IPC (e.g. Sockets, TCP, etc.)
Alternatively, if your camera works with the Camera Control API (CCAPI) I'd recommend that since you don't need a difficult to work with SDK and instead just have to send HTTP requests to the camera.