本文主要介绍如何使用请求答复模式
一、Service端
1、定义一个接口,并且添加一个ServiceContract特性,并且在方法上天上一个OperationContract特性
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.ServiceModel;
- using System.Text;
-
- namespace Mytest
- {
- // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口“IFlyService”。
- [ServiceContract]
- public interface IFlyService
- {
- [OperationContract]
- string Fly();
- }
- }
2、定义一个实现类,继承于第一步的IFlyService接口
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.ServiceModel;
- using System.Text;
-
- namespace Mytest
- {
- // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的类名“FlyService ”。
- public class FlyService : IFlyService
- {
- public string Fly()
- {
- return "You can fly";
- }
- }
- }
3、定义配置文件,其中contract为接口,name为接口实现类
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <startup>
- <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
- </startup>
- <system.serviceModel>
- <behaviors>
- <serviceBehaviors>
- <behavior name="">
- <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
- <serviceDebug includeExceptionDetailInFaults="false" />
- </behavior>
- </serviceBehaviors>
- </behaviors>
- <services>
- <service name="Mytest.FlyService">
- <endpoint address="" binding="basicHttpBinding" contract="Mytest.IFlyService">
- <identity>
- <dns value="localhost" />
- </identity>
- </endpoint>
- <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
- <host>
- <baseAddresses>
- <add baseAddress="http://localhost:8733/Design_Time_Addresses/Mytest/Service1/" />
- </baseAddresses>
- </host>
- </service>
- </services>
- </system.serviceModel>
- </configuration>
4、开启服务(宿主可以为控制台程序,IIS或Winform,当前选择Console控制台程序)
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.ServiceModel;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace Mytest
- {
- class Program
- {
- static void Main(string[] args)
- {
- ServiceHost host = new ServiceHost(typeof(FlyService));
- host.Open();
- Console.WriteLine("服务启动成功");
- Console.ReadLine();
- }
- }
- }
二、客户端
1、添加服务引用
2、请求服务
- using ConsoleApp1.ServiceReference1;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace ConsoleApp1
- {
- class Program
- {
- static void Main(string[] args)
- {
- FlyServiceClient client = new FlyServiceClient();
- string result = client.Fly();
- Console.WriteLine(result);
- Console.ReadLine();
- }
- }
- }
运行结果如图
当前为basicHttpBinding通信模式可以改为netTcpBinding并且修改address
- <endpoint address="net.tcp://localhost/my/service" binding="netTcpBinding" contract="Mytest.IFlyService">
- <identity>
- <dns value="localhost" />
- </identity>
- </endpoint>
更新服务引用即可
三、MSMQ
MSMQ(容量受硬盘大小限制)将客户端与service切开了,客户端只要将消息发送到消息队列即可,服务端自动获取消息
1、更改通信方式
- <endpoint address="net.msmq://localhost/my/service" binding="netMsmqBinding" contract="Mytest.IFlyService">
- <identity>
- <dns value="localhost" />
- </identity>
- </endpoint>
2、如果没有安装消息队列,首先安装windows功能,流程如下
控制面板-》windows功能-》MSMQ服务器
3、去找MSMQ:在计算机管理面板找到MSMQ,在MSMQ新建一个队列
4、必须为单工访问
a)服务必须为返回Void
b)IsOneWay = true
5、修改配置文件中安全性--改为None,并添加到endpoint节点 bindingConfiguration="mybinding"
- <bindings>
- <netMsmqBinding>
- <binding name="mybinding">
- <security mode="None"></security>
- </binding>
- </netMsmqBinding>
- </bindings>
6、MSMQ模式不支持端口号访问,所以address 不允许出现端口号
7、客户端删除配置重新更新
可以总结为basicHttpBinding适合.net与其他平台通信(如java等),netTcpBinding适合两个.net程序跨机器访问,MSMQ适合构建离线访问
四、上述方式都是同步调用,下面说一下异步调用方式
1、配置服务引用改为-》生成异步操作
2、
- using ConsoleApp1.ServiceReference1;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace ConsoleApp1
- {
- class Program
- {
- static void Main(string[] args)
- {
- FlyServiceClient client = new FlyServiceClient();
- client.BeginFly((obj) =>
- {
- var myclient = (FlyServiceClient)obj.AsyncState;
- myclient.EndFly(obj);
- }, client);
-
- //string result = client.Fly();
- /// Console.WriteLine(result);
- Console.ReadLine();
- }
- }
- }