当前位置: 首页 > news >正文

wcf深入学习笔记

从msdn中解释WCF:

WCF 通过一种面向服务的新型编程模型简化了关联应用程序的开发。通过提供分层的体系结构,WCF 支持多种风格的分布式应用程序开发。WCF 通道体系结构在底层提供了异步的非类型化消息传递基元。而建立在此基础之上的是用于进行安全可靠的事务处理数据交换的各种协议功能,以及广泛的传输协议和编码选择。

WCF 客户端和服务之间的通信

下面是wcf的配置介绍:

 

了解了wcf的大致情况,下面是总结的一些wcf运行步骤:

WCF Dispatching System

Step 1: ServiceHost和ServiceDescription的创建

           ServiceDescription        

           Behaviors  

           ServiceBehavior               EndpointBehavior               ContractBehavior               OperationBehavior

           service behavior集合                           

           Endpoints   Address,Binding,Contract,Behaviors 

  

Step 2: ServiceHost.Open()和Channel Listener & ChannelDispatcher的创建

Step 3: 请求接听和小心接受

Step 4: 获取匹配的EndpointDispatcher,并分发request message

Step 5: InstanceContext的获取

Step 6: 选择DispatchOperation

Step 7: 初始化当前的OperationContext

Step 8: 验证 Addressing信息

Step 9: 对request message进行检验或修改

Step 10: 对现有的service instance进行释放和回收

Step 11: 获取或者创建service instance

Step 12: 对当前线程的Context进行初始化

Step 13: 反序列化输入参数列表

Step 14: 对输入参数进行验证

Step 15: 执行service operation方法

Step 16: 对返回值或输出参数进行校验

Step 17: 序列化返回值或者输出参数

Step 18: 清理thread context

Step 19: Error handling

Step 20: 最后作一些资源的释放和清理工作

 

除此之外,wcf许多场景可以扩展,binging可以自定义MyBinding,可以定义自定义的MessageEncoder等。

好了说了这么多理论,该有实验场景了,

首先创建是MyRequestChannel:

public class MyRequestChannel:ChannelBase,IRequestChannel
    {
        private IRequestChannel InnerChannel
        {
            get;
            set;
        }

        public MyRequestChannel(ChannelManagerBase channelManager, IRequestChannel innerChannel)
            : base(channelManager)
        {
            this.InnerChannel = innerChannel;
        }

        #region Implement ChannelBase Members
        #endregion

        #region Implement IRequestChannel Members
        #endregion
    }

定义MyReplyChannel:

 public class MyReplyChannel:ChannelBase,IReplyChannel
    {
        private IReplyChannel InnerChannel
        {
            get;
            set;
        }

        public MyReplyChannel(ChannelManagerBase channelManager, IReplyChannel innerChannel)
            : base(channelManager)
        {
            this.InnerChannel = innerChannel;
        }

        #region Implement ChannelBase Members
         #endregion

        #region Implement IReplyChannel Members
         #endregion
    }


接下来开始定义MyChannelListener:

public class MyChannelListener<TChannel>:ChannelListenerBase<TChannel> where TChannel:class,IChannel
    {
        private IChannelListener<TChannel> InnerChannelListener
        {
            get;
            set;
        }

        public MyChannelListener(BindingContext context)
        {
            this.InnerChannelListener = context.BuildInnerChannelListener<TChannel>();
        }

        #region Implement ChannelListenerBase Members
        
        #endregion
    }


然后定义MyChannelFactory:

public class MyChannelFactory<TChannel> : ChannelFactoryBase<TChannel>
    {
        private IChannelFactory<TChannel> InnerChannelFactory { get; set; }

        public MyChannelFactory(BindingContext context)
        {
            this.InnerChannelFactory = context.BuildInnerChannelFactory<TChannel>();
        }

        #region Implement ChannelFactoryBase<TChannel>Members
       
        #endregion
    }


定义MyBindingElement:

public class MyBindingElement:BindingElement
    {
        #region Implement BindingElement Members
        
        #endregion
    }

定义MyBinding:

public class MyBinding:Binding
    {
        #region Implement Binding Members
        public override BindingElementCollection CreateBindingElements()
        {
            BindingElementCollection elements = new BindingElementCollection();
            elements.Add(new TextMessageEncodingBindingElement());
            elements.Add(new MyBindingElement());
            elements.Add(new HttpTransportBindingElement());
            return elements.Clone();
        }

        public override string Scheme
        {
            get 
            {
                return "http";
            }
        } 
        #endregion
    }


最后就可以使用自己定义的Binding,WCF通讯了。

static void Main(string[] args)
        {
            MyBinding binding = new MyBinding();
            IChannelListener<IReplyChannel> channelListener = binding.BuildChannelListener<IReplyChannel>(
                new Uri("http://127.0.0.1:8888/messagingbinding"));
            channelListener.Open();

            while (true)
            {
                IReplyChannel channel = channelListener.AcceptChannel(TimeSpan.MaxValue);
                channel.Open();
                RequestContext context = channel.ReceiveRequest(TimeSpan.MaxValue);

                Console.WriteLine("Receive a request message:\n{0}", context.RequestMessage);
                Message replyMessage = Message.CreateMessage(
                    MessageVersion.Soap12WSAddressing10,
                    "http://gavin.messagebinding",
                    "this is manually created reply message for the purpose of testing");
                context.Reply(replyMessage);
                channel.Close();
            }

同样,扩展也可以用在MessageEncoder,下次继续介绍。

To be continued!

 

 

 

转载于:https://www.cnblogs.com/gavinhuang/p/3286759.html

相关文章:

  • Eclipse常用快捷键之代码编辑篇
  • QQ客服代码
  • 完美解决gradle下载慢的问题
  • 人物四(奥瑞夫特)
  • java.lang.OutOfMemoryError: PermGen space
  • 1.揭开消息中间件RabbitMQ的神秘面纱
  • 文本框不够长,显示“XXX...”
  • 统计生成日期为昨天的数据
  • 关于生成器的问题
  • Oracle 调用存储过程执行CRUD的小DEMO
  • webpack 项目实战
  • 做创业狼之前请看看这篇文章
  • json字符串转JSONObject和JSONArray以及取值
  • php计算时间差的方法
  • 前端路由实现-history
  • 【Redis学习笔记】2018-06-28 redis命令源码学习1
  • 2017 年终总结 —— 在路上
  • CentOS6 编译安装 redis-3.2.3
  • codis proxy处理流程
  • es6
  • Java IO学习笔记一
  • java8-模拟hadoop
  • Laravel核心解读--Facades
  • LintCode 31. partitionArray 数组划分
  • MD5加密原理解析及OC版原理实现
  • Protobuf3语言指南
  • vue的全局变量和全局拦截请求器
  • Vue--数据传输
  • 不用申请服务号就可以开发微信支付/支付宝/QQ钱包支付!附:直接可用的代码+demo...
  • 从零到一:用Phaser.js写意地开发小游戏(Chapter 3 - 加载游戏资源)
  • 看完九篇字体系列的文章,你还觉得我是在说字体?
  • 如何合理的规划jvm性能调优
  • 什么是Javascript函数节流?
  • 推荐一款sublime text 3 支持JSX和es201x 代码格式化的插件
  • ​LeetCode解法汇总2304. 网格中的最小路径代价
  • # Swust 12th acm 邀请赛# [ K ] 三角形判定 [题解]
  • #我与Java虚拟机的故事#连载10: 如何在阿里、腾讯、百度、及字节跳动等公司面试中脱颖而出...
  • (Redis使用系列) SpirngBoot中关于Redis的值的各种方式的存储与取出 三
  • (附源码)ssm考试题库管理系统 毕业设计 069043
  • (附源码)计算机毕业设计ssm-Java网名推荐系统
  • (附源码)计算机毕业设计SSM基于健身房管理系统
  • (十八)devops持续集成开发——使用docker安装部署jenkins流水线服务
  • (转)linux 命令大全
  • (转)创业家杂志:UCWEB天使第一步
  • (转)为C# Windows服务添加安装程序
  • .NET I/O 学习笔记:对文件和目录进行解压缩操作
  • .Net IE10 _doPostBack 未定义
  • .Net Winform开发笔记(一)
  • .NET 动态调用WebService + WSE + UsernameToken
  • .NET 线程 Thread 进程 Process、线程池 pool、Invoke、begininvoke、异步回调
  • .Net程序帮助文档制作
  • .NET国产化改造探索(一)、VMware安装银河麒麟
  • .NET简谈互操作(五:基础知识之Dynamic平台调用)
  • .net解析传过来的xml_DOM4J解析XML文件
  • /dev/VolGroup00/LogVol00:unexpected inconsistency;run fsck manually