Edge Extension: How to re-connect app-service in case UWP app service is turned down
Published Aug 08 2019 11:51 PM 2,897 Views
Microsoft

In Edge Extension, one of typical infrastructures is using Centennial to package three parts (Scripts and Content loaded by Edge, UWP run as App Service for receiving commands from Edge and pass them to backend and vise versa, backend is run as a Win32 app (Desktop Bridge Component) which executes some hard core tasks. You can get more details from this sample project:

 

https://github.com/MicrosoftEdge/MicrosoftEdge-Extensions-Demos/tree/master/SecureInput

 

1.png

 

In case UWP App Service is stopped for some reason, but Edge is still open, the Edge Extension better to consider a proper re-try  mechanism to resume its app service tasks. Here are two methods to solve this from Code ( Use the above SecureInput as a sample).

 

Method One

 

  1. In /SecureInput/Extension/content.js, it uses browser.runtime.sendMessage to send message to background.js:
 

 

  1. In /SecureInput/Extension/background.js, it calls sendResponse method to send message to UWP App Service, if any exception throwed by sendNativeMessage (including app service is turned down), it has chances to send response back to the Content.js with success ‘false’ field:

 

3.png

 

4.png

 
  1. Then we can modify content.js to check the success field, if it is false, we can do re-connect:
 

5.png

 

the connectNativeApp in content.js will finally call connect in Background.js:

 

6.png

 

This will work.

 

Method Two

 

Another thought is in background.js, it has one onDisconnected call back when App Service is turned down, you can also handle this call back to do re-connect. In the real situation, we used the first method and it works. The second one cannot always work in Windows fast startup situation.

 

7.png

 

Thanks!

 

Version history
Last update:
‎Aug 15 2019 05:38 AM
Updated by: