【发布时间】:2019-05-15 10:16:32
【问题描述】:
我正在 Edge 中的 WebExtension 和桌面应用程序之间开发本机消息传递。但是通过 desktopBridge 与桌面应用程序的通信不起作用。在下面的代码中,显示了消息框Success,但没有显示消息框Test。
AppServiceResponse desktopBridgeResponse = await this.desktopBridgeConnection.SendMessageAsync(testValueSet); 遇到异常“对象引用未设置为对象的实例”。
// 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();
}
根据Secure Input 应该可以工作。
为什么已经成功建立了与应用服务的连接,却没有输入Connection_RequestReceived()?
任何帮助将不胜感激,谢谢!
更新:通过调试发现:当应用服务收到Edge的消息时,Run()方法运行到“Edge连接分支”。接下来,OnRequestReceived() 被执行,其中一条消息被发送到 desktopBridge await this.desktopBridgeConnection.SendMessageAsync(testValueSet),但这失败了。然后,再次执行Run(),并运行到“DesktopBridge 连接分支”。至此,desktopBridgeConnection已经定义好了,但是为时已晚。
我需要在输入OnRequestReceived() 之前定义desktopBridgeConnection,以便我可以在OnRequestReceived() 中使用它。但我不知道如何实现。
【问题讨论】:
-
我建议您在 Visual Studio 中关闭您的解决方案。尝试将您的项目文件夹移动到受信任的位置。再次尝试从受信任的位置打开解决方案。确保已加载解决方案中的所有项目。比尝试运行解决方案来检查它是否运行成功。
-
@Deepak-MSFT 你能解释一下什么是可信位置吗?如何确定文件夹是否为受信任位置?我可以将我的项目当前所在的文件夹定义为受信任的位置,以便我不需要移动它吗?当我搜索它时,我只能在 Microsoft Office 应用程序的上下文中找到有关受信任位置的信息。
-
@Deepak-MSFT 在
Tools -> Options... -> Environment -> Trust Settings下的 Visual Studio 中没有列出受信任的路径。文件和文件夹信任设置都设置为No verification。你是说这个设置吗? -
是的,我说的是这个选项。实际上,如果您使用 API 或调用可能返回 null 的方法,则可能会发生此错误。因此,您可以尝试调试代码以检查导致此问题的原因。
-
@Deepak-MSFT 我将
.sln的父目录和DesktopApp.exe的路径添加到受信任路径列表中,但问题仍然存在。我无法在 VS 中调试DesktopApp.exe,因为断点不会被命中。这就是我在那里使用 MessageBoxes 的原因。还有其他建议吗?
标签: c# microsoft-edge desktop-bridge microsoft-edge-extension