本机消息主机和C#应用程序之间的通信
我有一个chrome扩展,可以与我在C#中创建的本机消息传递主机通信本机消息主机和C#应用程序之间的通信,c#,google-chrome-extension,chrome-native-messaging,C#,Google Chrome Extension,Chrome Native Messaging,我有一个chrome扩展,可以与我在C#中创建的本机消息传递主机通信 但是,如何在本机消息传递主机和C#app之间进行通信。我认为这将通过本机消息主机输入/输出流实现,但我还没有找到如何做到这一点。我找不到使用Process.StandardInput的方法,因为chrome启动了本机消息主机 相反,我使用了WCF。代码如下: private static object SyncRoot = new object(); static string returnValue = nu
但是,如何在本机消息传递主机和C#app之间进行通信。我认为这将通过本机消息主机输入/输出流实现,但我还没有找到如何做到这一点。我找不到使用Process.StandardInput的方法,因为chrome启动了本机消息主机 相反,我使用了WCF。代码如下:
private static object SyncRoot = new object();
static string returnValue = null;
[ServiceContract]
public interface IGetChromeString
{
[OperationContract]
string GetString(string value);
}
public class CGetChromeString : IGetChromeString
{
public string GetString(string value)
{
OpenStandardStreamOut(value);
// Wait until OpenStandardStreamIn() sets returnValue in the Main Thread
lock (SyncRoot)
{
Monitor.Wait(SyncRoot);
}
return returnValue;
}
}
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(CGetChromeString), new Uri[] { new Uri("net.pipe://localhost") }))
{
host.AddServiceEndpoint(typeof(IGetChromeString), new NetNamedPipeBinding(), "PipeReverse");
host.Open();
while (returnValue != "")
{
returnValue = OpenStandardStreamIn();
// returnValue has been set, GetUrl can resume.
lock (SyncRoot)
{
Monitor.Pulse(SyncRoot);
}
}
host.Close();
}
}
static string OpenStandardStreamIn()
{
Stream stdin = Console.OpenStandardInput();
byte[] bytes = new byte[4];
stdin.Read(bytes, 0, 4);
int length = 0;
length = System.BitConverter.ToInt32(bytes, 0);
string input = "";
for (int i = 0; i < length; i++)
{
input += (char)stdin.ReadByte();
}
return input;
}
static void OpenStandardStreamOut(string stringData)
{
string msgdata = "{\"text\":\"" + stringData + "\"}";
int DataLength = msgdata.Length;
Stream stdout = Console.OpenStandardOutput();
stdout.WriteByte((byte)((DataLength >> 0) & 0xFF));
stdout.WriteByte((byte)((DataLength >> 8) & 0xFF));
stdout.WriteByte((byte)((DataLength >> 16) & 0xFF));
stdout.WriteByte((byte)((DataLength >> 24) & 0xFF));
Console.Write(msgdata);
}
private static object SyncRoot=new object();
静态字符串returnValue=null;
[服务合同]
公共接口IGetChromeString
{
[经营合同]
字符串GetString(字符串值);
}
公共类CGetChromeString:IGetChromeString
{
公共字符串GetString(字符串值)
{
OpenStandardStreamOut(值);
//等待OpenStandardStreamIn()在主线程中设置returnValue
锁定(同步根)
{
Monitor.Wait(SyncRoot);
}
返回值;
}
}
静态void Main(字符串[]参数)
{
使用(ServiceHost host=newServiceHost(typeof(CGetChromeString)),新Uri[]{new Uri(“net”)。pipe://localhost") }))
{
AddServiceEndpoint(typeof(IGetChromeString),新的NetNamedPipeBinding(),“PipeReverse”);
host.Open();
while(返回值!=“”)
{
returnValue=OpenStandardStreamIn();
//已设置returnValue,GetUrl可以继续。
锁定(同步根)
{
监视器。脉冲(同步根);
}
}
host.Close();
}
}
静态字符串OpenStandardStreamIn()
{
Stream stdin=Console.OpenStandardInput();
字节[]字节=新字节[4];
标准读取(字节,0,4);
整数长度=0;
长度=System.BitConverter.ToInt32(字节,0);
字符串输入=”;
for(int i=0;i>0)和0xFF));
stdout.WriteByte((字节)((数据长度>>8)和0xFF));
stdout.WriteByte((字节)((数据长度>>16)和0xFF));
stdout.WriteByte((字节)((数据长度>>24)和0xFF));
Console.Write(msgdata);
}
这是一个使用本机消息传递的项目,主机使用stdin/stdout并用C#编写: