NativeMessaging 1.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package NativeMessaging --version 1.0.2
NuGet\Install-Package NativeMessaging -Version 1.0.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NativeMessaging" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NativeMessaging --version 1.0.2
#r "nuget: NativeMessaging, 1.0.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install NativeMessaging as a Cake Addin
#addin nuget:?package=NativeMessaging&version=1.0.2

// Install NativeMessaging as a Cake Tool
#tool nuget:?package=NativeMessaging&version=1.0.2

NativeMessaging

C# Chome Native Messaging Library

Can be used to receive data from or talk to a Chrome extension.

This can currently be used with the example app provided here: https://chromium.googlesource.com/chromium/src/+/master/chrome/common/extensions/docs/examples/api/nativeMessaging

Usage

Extend the host class and decide what you want to do when you receive a message.

public class MyHost : Host
    {
        private const bool SendConfirmationReceipt = true;

        public override string Hostname
        {
            get { return "com.anewtonlevey.myhost"; }
        }

        public MyHost() : base(SendConfirmationReceipt)
        {

        }

        protected override void ProcessReceivedMessage(JObject data)
        {
            SendMessage(data);
        }
    }
class Program
    {
        static public string AssemblyLoadDirectory
        {
            get
            {
                string codeBase = Assembly.GetEntryAssembly().CodeBase;
                UriBuilder uri = new UriBuilder(codeBase);
                string path = Uri.UnescapeDataString(uri.Path);
                return Path.GetDirectoryName(path);
            }
        }

		static public string AssemblyExecuteablePath
		{
			get
			{
				string codeBase = Assembly.GetEntryAssembly().CodeBase;
				UriBuilder uri = new UriBuilder(codeBase);
				return Uri.UnescapeDataString(uri.Path);
			}
		}

        static Host Host;

        static string[] AllowedOrigins = new string[] { "chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/" };
        static string Description = "Description Goes Here";

        static void Main(string[] args)
        {
            Host = new MyHost();
            if (args.Contains("--register"))
            {
                Host.GenerateManifest(Description, AllowedOrigins);
                Host.Register();
            } else if(args.Contains("--unregister"))
            {
                Host.UnRegister();
            } else
            {
                Host.Listen();
            }
        }
    }

Troubleshooting

If your're having trouble connecting from Chrome try launching chrome with --enable-logging flag as detailed in Debugging native messaging.

It also recommeneded to try compiling in Release mode if you're encountering issues.

Product Compatible and additional computed target framework versions.
.NET Framework net452 is compatible.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 1,446 5/6/2021
1.0.2 662 9/12/2020