I am developing native messaging between a WebExtension in Edge and a desktop app. But the communication with the desktop app via desktopBridge does not work. In the code below, the message box Success is shown, but not the message box Test.
AppServiceResponse desktopBridgeResponse = await this.desktopBridgeConnection.SendMessageAsync(testValueSet); runs into the exception "Object reference not set to an instance of an object".
// AppService.cs
public async void Run(IBackgroundTaskInstance taskInstance)
{
if (appService.CallerPackageFamilyName == "Microsoft.MicrosoftEdge_8wekyb3d8bbwe") // App service connection from Edge
{
appServiceconnection = details.AppServiceConnection;
appServiceconnection.RequestReceived += OnRequestReceived;
try
{
// Make sure the DesktopApp.exe is in your AppX folder, if not rebuild the solution
await Windows.ApplicationModel.FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync();
}
catch (Exception)
{
MessageDialog dialog = new MessageDialog("Rebuild the solution and make sure the DesktopApp.exe is in your AppX folder");
await dialog.ShowAsync();
}
}
else (appService.CallerPackageFamilyName == Windows.ApplicationModel.Package.Current.Id.FamilyName) // App service connection from desktopBridge App
{
desktopBridgeConnection = details.AppServiceConnection;
}
}
private async void OnRequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
{
try
{
AppServiceResponse desktopBridgeResponse = await this.desktopBridgeConnection.SendMessageAsync(testValueSet);
}
catch (Exception e)
{
var errorMessage = e.Message;
}
}
// DesktopApp.cs
[STAThread]
static void Main()
{
Thread appServiceThread = new Thread(new ThreadStart(InitializeAppServiceConnection));
appServiceThread.Start();
Application.Run();
}
static async void InitializeAppServiceConnection()
{
connection = new AppServiceConnection();
connection.AppServiceName = "example";
connection.PackageFamilyName = Package.Current.Id.FamilyName;
connection.RequestReceived += Connection_RequestReceived;
AppServiceConnectionStatus status = await connection.OpenAsync();
if (status != AppServiceConnectionStatus.Success)
{
// something went wrong ...
MessageBox.Show(status.ToString());
}
else
{
MessageBox.Show("Success");
}
}
private static void Connection_RequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
{
MessageBox.Show("Test");
string key = args.Request.Message.First().Key;
string value = args.Request.Message.First().Value.ToString();
}
According to Secure Input it should work.
Why is Connection_RequestReceived() not entered, although the connection to the app service has been established successfully?
Any help will be appreciated, thanks!
Update: Through debugging I found the following out: When the app service receives a message from Edge, the Run() method runs into the "Edge connection branch". Next, OnRequestReceived() is executed in which a message is sent to desktopBridge await this.desktopBridgeConnection.SendMessageAsync(testValueSet), but this failes. Then, Run() is executed again and runs into the "DesktopBridge connection branch". At this point, the desktopBridgeConnection is defined, but it is too late.
I need to define desktopBridgeConnection before OnRequestReceived() is entered so that I can use it within OnRequestReceived(). But I don't know how to implement this.
Tools -> Options... -> Environment -> Trust Settingsare no trusted paths listed. File and folder trust settings are both set toNo verification. Do you mean this setting? - Ronquam.slnand the path toDesktopApp.exeto the list of trusted paths but the problem remains. I cannot debugDesktopApp.exein VS because breakpoints will not be hit. This is the reason why I use the MessageBoxes there. Any other suggestions? - Ronquam