您需要某种形式的IPC(进程间通信).您选择哪一个可以在很大程度上取决于您想要允许的内容以及您不希望允许的内容.例如,您需要考虑以下事项:
如您所见,在引入IPC时,您会遇到许多问题.考虑上述问题并研究以下可能的解决方案:
所有这些都有起伏,利弊.这取决于您的具体需求.
由于您指示尝试从本地计算机调用服务,因此最简单的方法是使用WCF over Named Pipes(尝试限制与本地计算机的通信).如果您只想将二进制数据传入和传出服务,那么上面链接的RPC库可以正常工作.
以下示例演示如何使用上面的RPC库:
把它放在你的服务中:
Guid iid = new Guid("{....}");
using (RpcServerApi server = new RpcServerApi(iid))
{
server.OnExecute +=
delegate(IRpcClientInfo client, byte[] arg)
{
byte[] response;
//do whatever
return response;
};
server.AddProtocol(RpcProtseq.ncalrpc, "MyServiceName", 5);
server.AddAuthentication(RpcAuthentication.RPC_C_AUTHN_WINNT);
server.StartListening();
...
Run Code Online (Sandbox Code Playgroud)
这是客户端代码:
Guid iid = new Guid("{....}");
using (RpcClientApi client = new RpcClientApi(iid, RpcProtseq.ncalrpc, null, "MyServiceName"))
{
client.AuthenticateAs(null, RpcClientApi.Self, RpcProtectionLevel.RPC_C_PROTECT_LEVEL_PKT_PRIVACY, RpcAuthentication.RPC_C_AUTHN_WINNT);
byte[] input, output;
output = client.Execute(input);
}
Run Code Online (Sandbox Code Playgroud)