WCF 双工 回调模式

在这里插入图片描述

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);
        }
    }
}

宿主到控制台

支持双工的 协议 tcp、Duplehttp
》》》 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();
        }

     
    }
}

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

相关推荐

  1. 【C/C++】Linux系统周期函数(互斥锁

    2024-05-14 00:44:11       18 阅读
  2. 地狱Axios

    2024-05-14 00:44:11       36 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-14 00:44:11       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-14 00:44:11       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-14 00:44:11       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-14 00:44:11       18 阅读

热门阅读

  1. 孩子如何备考编程竞赛

    2024-05-14 00:44:11       9 阅读
  2. 1-3 如何学习,才能让这门课真正发挥价值

    2024-05-14 00:44:11       12 阅读
  3. 21、Flink 的 Checkpoints 使用介绍

    2024-05-14 00:44:11       9 阅读
  4. Linux——进程的优先级、ACL

    2024-05-14 00:44:11       11 阅读
  5. 【12572物联网工程概论】期末复习自考复习

    2024-05-14 00:44:11       11 阅读
  6. Redis 基础之Redis 配置

    2024-05-14 00:44:11       10 阅读
  7. Spring Security中如何配置认证和授权?

    2024-05-14 00:44:11       11 阅读