I spent a few hours today researching how to get Chrome native messaging working with a C# native host. Conceptually it was quite simple, but there were a few snags that I resolved with help (in part) from these other questions:
Native Messaging Chrome
Native messaging from chrome extension to native host written in C#
Very Slow to pass "large" amount of data from Chrome Extension to Host (written in C#)
My solution is posted below.
Assuming the manifest is set up properly, here is a complete example for talking to a C# host using the "port" method:
If you don't need to actively communicate with the host, using
runtime.sendNativeMessage
will work fine. To prevent the host from hanging, simply remove thewhile
loop and do Read/Write once.To test this, I used the example project provided by Google here: https://chromium.googlesource.com/chromium/src/+/master/chrome/common/extensions/docs/examples/api/nativeMessaging
Note: I'm using Json.NET to simplify the json serialization/de-serialization process.
I hope this is helpful to somebody!