(WCF) 利用WCF实现两个Process之间的通讯。

目的: 实现两个独立的Process 之间的通讯。

实现思路: 建立一个WCF Service,然后将其Host到一个Console 程序中,然后在另外一个Console程序中引用WCF的Service,并使用Client调用Interface中定义好的方法。

具体实现:

1. 创建一个WCF Service Libraray。

1.1) File -> New -> New Project -> WCF -> WCF Service Library.)

1.2) 修改Service名和IServcie名为WillWcfService

2. 创建Console程序,并且把WCF Service host 到这个Console程序中。

2.1) 添加引用 System.ServiceModel, 1步中创建的Project (Library).

2.2) 修改App.config ( 添加 Endpoints. ( Address, Binding, Contract ). ) 便捷的方法就是直接复制第一步创建的WCF Service Libraray 项目中的App.config 文件里的内容,这里面已经有提示了。

  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->

2.3) 在Console程序中插入代码创建 Host.

            using (ServiceHost host = new ServiceHost(typeof(WillWcfService)))
            {
                host.Open();
                Console.WriteLine("Host started @" + DateTime.Now.ToString()); 
            }

3. 创建Console client 程序。

3.1)运行启动第2步创建的Host程序,启动WCF Service。

3.2)  添加Service引用 (Add Service Reference), 输入在Host程序里App.config中定义的的Address。

3.3)在Console程序里面插入代码创建Client.

            WillWcfServiceReference.WillWcfServiceClient client = new WillWcfServiceReference.WillWcfServiceClient();
            string data = client.GetData(8);
            Console.WriteLine("Get data: {0}", data); 
原文地址:https://www.cnblogs.com/fdyang/p/9838943.html