我创建了一个C#WPF项目。我有两个exe的运行,都是由我创建的。一个exe有一个Window而另一个没有。
现在我想从exe到另一个进行通信。 我想从exe(无窗口)向另一个发送一条小消息。
我对Windows C#中的这个IPC感到很困惑,有谁能建议我哪个会对这个问题有所帮助
答案 0 :(得分:2)
你的评论不应该粗鲁。
现在试试这个:
在客户端上:使用以下几行创建客户端代理
// Create the proxy:
EndpointAddress ep = new EndpointAddress("net.pipe://localhost/SomeAddress/PipeEndpoint/");
IMyinterface instance = ChannelFactory<IMyinterface>.CreateChannel(new NetNamedPipeBinding(), ep);
// now use it:
instance.SendMessage();
在服务器端,运行服务器并注册对象以执行工作:
ServiceHost host = new ServiceHost(new MyClass(), new Uri("net.pipe://localhost/SomeAddress"));
host.AddServiceEndpoint(typeof(IMyinterface), new NetNamedPipeBinding(), "PipeEndpoint");
host.Open();
服务器端的MyClass代码:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class MyClass : IMyinterface
{
public void SendMessage()
{
// do something here
}
}
接口应该位于客户端和服务器项目的单独项目中:
[ServiceContract]
interface IMyinterface
{
[OperationContract]
void SendMessage();
}
备注:当我说“客户”时,我指的是发送邮件的人。服务器是接收消息的人。我认为在你的架构中是相反的,所以我想用我的术语清楚。
我希望它有所帮助