WCF 通信三种模式 请求与答复、单向、双工通信

WCF 通信三种模式

  1. 请求与答复 默认模式
  2. 单向
  3. 双工

请求与答复

在这里插入图片描述

[OperationContract]
string GetInfo(string id);
[OperationContract]
void Getxxx();

即使返回值是void 也属于请求与答复模式。

缺点:如果用WCF在程序A中上传一个2G的文件,那么要想执行程序B也许就是几个小时后的事情了。如果操作需要很长的时间,那么客户端程序的响应能力将会大大的下降。

优点:有返回值我们就可以向客户端返回错误信息,如:只接收".rar"文件等信息。

单向模式

使用 IsOneWay=true 标记的操作不得声明输出参数、引用参数或返回值
在这里插入图片描述

双工模式

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
》》》 支持回调的绑定有4种:WSDualHttpBinding、NetTcpBinding、NetNamedPipeBinding、NetPeerTcpBinding。

wcfInterface 项目

》》》wcf服务端提供的服务协议

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
namespace wcfInterface
{
   [ServiceContract(CallbackContract= typeof(ICallBack))]
    public interface ICalculatorService
    {
        [OperationContract(IsOneWay =true)]
        void Minus(int x, int y);
    }
}

》》》 回调接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
namespace wcfInterface
{
    /// <summary>
    /// 这个是约束,不需要协议,有客户端实现
    /// </summary>
    public interface ICallBack
    {
        [OperationContract(IsOneWay = true)]
        void Result(int x, int y, int result);
    }
}

wcfService 实现wcfInterface
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using wcfInterface;

namespace wcfService
{
    public class CalculatorService : ICalculatorService
    {
        /// <summary>
        /// 完成计算,然后返回值
        /// 客户端请问服务器,服务端处理完成之后,触发客户端方法
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public void Minus(int x, int y)
        {
            int result = x - y;
            // 模拟服务端费时操作
            Thread.Sleep(3000);
            //OperationContext  操作上下文  ,客户端请求 服务端,会携带回调信息的
            ICallBack callback = OperationContext.Current.GetCallbackChannel<ICallBack>();
            callback.Result(x, y, result);
        }
    }
}

在这里插入图片描述
在这里插入图片描述
参考

宿主到控制台

支持双工的 协议 WSDualHttpBinding、NetTcpBinding、NetNamedPipeBinding、NetPeerTcpBinding
》》》 config配置文件

 <system.serviceModel>
    <bindings>
      <netTcpBinding>
        <binding name="tcpbinding" >
          <security mode="None">
            <transport clientCredentialType="None" protectionLevel="None"></transport>
          </security>
        </binding>
      </netTcpBinding>
    </bindings>
    <services>
      <service name="wcfService.CalculatorService" behaviorConfiguration="behaviorConfiguration">
        <!--服务的对象-->
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8000/CalculatorService"/>
            <!--服务的IP和端口号-->
          </baseAddresses>
        </host>
        <endpoint address="" binding="netTcpBinding" bindingConfiguration="tcpbinding" contract="wcfInterface.ICalculatorService"></endpoint>
        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" ></endpoint>
        <!--contract:服务契约-->
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="behaviorConfiguration">
          <serviceDebug httpHelpPageEnabled="false"/>
          <serviceMetadata httpGetEnabled="false"/>
          <serviceTimeouts transactionTimeout="00:10:00"/>
          <serviceThrottling maxConcurrentCalls="1000"  maxConcurrentInstances="1000" maxConcurrentSessions="1000"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

》》》 启动服务

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using wcfInterface;
using wcfService;
namespace wcfConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            //用列表 可以方便添加多个服务挂载
            List<ServiceHost> hosts = new List<ServiceHost>() {
                 new ServiceHost(typeof(CalculatorService))
            };
            foreach (var host in hosts)
            {
                host.Opening += delegate 
                {
                    Console.WriteLine($"{host.GetType()}服务正在启动中。。。。");
                };
                host.Opened += (o, e) =>
                {
                    Console.WriteLine($"{host.GetType()}服务 启动成功啦!");
                };
                host.Open();
            }
            Console.WriteLine("按任意键,终止服务");
            Console.Read();
            foreach (var host in hosts)
            {
                host.Close();
            }
            Console.Read();
        }
    }
}

》》》》 以上 是服务端的代码

》》》》以下是客户端代码

客户端代码

》》》引用服务
在这里插入图片描述

》》》 新建一个类 CallBackService 继承 上面的服务中的 回调接口 zenCalculator.ICalculatorServiceCallback

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    public class CallBackService : zenCalculator.ICalculatorServiceCallback
    {
        public void Result(int x, int y,  int result)
        {
            Console.WriteLine($"回调操作结果:{x}-{y}{result}");
        }
    }
}

在这里插入图片描述

》》》 以控制台 为了 请问wcf服务

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.SelfHost;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            zenCalculator.CalculatorServiceClient client = null;
            try
            {
                Console.WriteLine("===============================");
                //双工要借助 InstanceContext   new InstanceContext(  上面新建类对象 )
                InstanceContext context = new InstanceContext(new CallBackService() );
                client = new zenCalculator.CalculatorServiceClient(context);
                client.Minus(55, 3);
                client.Close();
            }
            catch (Exception ex)
            {
                if (client!=null)
                {
                    client.Abort();

                }
                throw;
            }

            Console.Read();
        }

     
    }
}

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值