Home
%3CLINGO-SUB%20id%3D%22lingo-sub-796856%22%20slang%3D%22en-US%22%3EEdge%20Extension%3A%20How%20to%20re-connect%20app-service%20in%20case%20UWP%20app%20service%20is%20turned%20down%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-796856%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3EIn%20Edge%20Extension%2C%20one%20of%20typical%20infrastructures%26nbsp%3Bis%20using%26nbsp%3BCentennial%20to%20package%20three%20parts%20(Scripts%20and%20Content%20loaded%20by%20Edge%2C%20UWP%20run%20as%20App%20Service%20for%20receiving%20commands%20from%20Edge%26nbsp%3Band%20pass%20them%20to%20backend%20and%20vise%20versa%2C%26nbsp%3Bbackend%20is%26nbsp%3Brun%26nbsp%3Bas%26nbsp%3Ba%26nbsp%3BWin32%20app%26nbsp%3B(Desktop%20Bridge%20Component)%20which%20executes%20some%26nbsp%3Bhard%20core%20tasks.%20You%20can%20get%20more%20details%20from%20this%20sample%20project%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FMicrosoftEdge%2FMicrosoftEdge-Extensions-Demos%2Ftree%2Fmaster%2FSecureInput%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2FMicrosoftEdge%2FMicrosoftEdge-Extensions-Demos%2Ftree%2Fmaster%2FSecureInput%3C%2FA%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorclipboard_image_0%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F126348iCDD1E656060343BA%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%221.png%22%20title%3D%221.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EIn%20case%20UWP%20App%20Service%20is%20stopped%20for%20some%20reason%2C%20but%20Edge%20is%20still%20open%2C%20the%20Edge%20Extension%20better%20to%20consider%20a%20proper%20re-try%26nbsp%3B%20mechanism%20to%20resume%20its%20app%20service%20tasks.%20Here%20are%20two%20methods%20to%20solve%20this%20from%20Code%20(%20Use%20the%20above%20SecureInput%20as%20a%20sample).%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EMethod%20One%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3E%3CSPAN%3E%20In%20%2F%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FMicrosoftEdge%2FMicrosoftEdge-Extensions-Demos%2Ftree%2Fmaster%2FSecureInput%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3ESecureInput%3C%2FA%3E%2F%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FMicrosoftEdge%2FMicrosoftEdge-Extensions-Demos%2Ftree%2Fmaster%2FSecureInput%2FExtension%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EExtension%3C%2FA%3E%2Fcontent.js%2C%20it%20uses%20browser.runtime.sendMessage%20to%20send%20message%20to%20background.js%3A%3C%2FSPAN%3E%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CDIV%20id%3D%22tinyMceEditorclipboard_image_1%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3COL%20start%3D%222%22%3E%0A%3CLI%3E%3CSPAN%3E%20In%20%2FSecureInput%2FExtension%2Fbackground.js%2C%20it%20calls%20sendResponse%20method%20to%20send%20message%20to%20UWP%20App%20Service%2C%20if%20any%20exception%20throwed%20by%20sendNativeMessage%20(including%20app%20service%20is%20turned%20down)%2C%20it%20has%20chances%20to%20send%20response%20back%20to%20the%20Content.js%20with%20success%20%E2%80%98false%E2%80%99%20field%3A%3C%2FSPAN%3E%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20760px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F126350iED9A34B54857AC2D%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%223.png%22%20title%3D%223.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorclipboard_image_2%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F126351i53E2B7645BA12095%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20alt%3D%224.png%22%20title%3D%224.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorclipboard_image_3%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3COL%20start%3D%223%22%3E%0A%3CLI%3E%3CSPAN%3E%20Then%20we%20can%20modify%20content.js%20to%20check%20the%20success%20field%2C%20if%20it%20is%20false%2C%20we%20can%20do%20re-connect%3A%3C%2FSPAN%3E%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CDIV%20id%3D%22tinyMceEditorclipboard_image_4%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20701px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F126352iEC61717B75E281AB%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%225.png%22%20title%3D%225.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3Ethe%20connectNativeApp%20in%26nbsp%3Bcontent.js%26nbsp%3Bwill%20finally%20call%20connect%20in%20Background.js%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20943px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F126353i2ECF24A863A6883A%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%226.png%22%20title%3D%226.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorclipboard_image_5%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%3CSPAN%3EThis%20will%20work.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EMethod%20Two%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EAnother%20thought%26nbsp%3Bis%20in%20background.js%2C%20it%20has%20one%20onDisconnected%20call%20back%20when%20App%20Service%20is%20turned%20down%2C%20you%20can%20also%20handle%20this%20call%20back%20to%20do%20re-connect.%20In%20the%20real%20situation%2C%20we%20used%20the%20first%20method%20and%20it%20works.%20The%20second%20one%20cannot%20always%20work%20in%20Windows%20fast%20startup%20situation.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20790px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F126354iAF64C057A2C94859%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%227.png%22%20title%3D%227.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CDIV%20id%3D%22tinyMceEditorclipboard_image_6%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%3CSPAN%3EThanks!%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-796856%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%20style%3D%22box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20%26amp%3Bquot%3B%20segoeui%26amp%3Bquot%3B%2C%26amp%3Bquot%3Blato%26amp%3Bquot%3B%2C%26amp%3Bquot%3Bhelvetica%20neue%26amp%3Bquot%3B%2Chelvetica%2Carial%2Csans-serif%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3EIn%20case%20UWP%20App%20Service%20is%20stopped%20for%20some%20reason%2C%20but%20Edge%20is%20still%20open%2C%20the%20Edge%20Extension%20better%20to%20consider%20a%20proper%20re-try%26nbsp%3B%20mechanism%20to%20resume%20its%20app%20service%20tasks.%20%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E
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!