博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
wcf自定义绑定
阅读量:4617 次
发布时间:2019-06-09

本文共 4338 字,大约阅读时间需要 14 分钟。

一,创建自定义绑定

有时候我们需要创建自己的绑定,这在很多情况下不是出于特殊的安全要求和使用的传输协议。为了创建自定义的绑定,需要建立一组绑定元素。绑定元素是由System.ServiceModel.Channels.BindingElement派生而来的。

代码1,使用HTTP传输协议和Binary编码格式的自定义绑定:

 

除了用配置方式创建一个自定义绑定,也可以采用编程方式。

代码2,用编程方式配置一个自定义绑定:

class Program    {        static void Main(string[] args)        {            CustomBinding binding = new CustomBinding();            binding.Elements.Add(new BinaryMessageEncodingBindingElement());            binding.Elements.Add(new HttpTransportBindingElement());            ServiceHost host = new ServiceHost(typeof(CarRentalService));            ServiceEndpoint serviceEndpoint =                host.AddServiceEndpoint(typeof(ICarRentalService),                    binding,                    "http://localhost:8080/CarRentalService");            try            {                host.Open();                Console.WriteLine("The car rental service is up and listening on the following addresses:");                foreach (var item in host.Description.Endpoints)                {                    Console.WriteLine("> [{0}] {1}", item.Binding.Name, item.Address.Uri.ToString());                }                Console.ReadLine();                host.Close();            }            catch (CommunicationException ex)            {                host.Abort();                Console.WriteLine(ex);            }            catch (TimeoutException ex)            {                host.Abort();                Console.WriteLine(ex);            }            catch (Exception ex)            {                host.Abort();                Console.WriteLine(ex);            }        }    }

 

二,重用自定义绑定

有时,可能需要创建一个能够重复用于不同解决方案的自定义绑定。由于每个绑定都派生于System.ServiceModel.Channels.Binding这个抽象基类,我们只需建立自己的绑定类,并实现CreateBindingElement方法即可实现此目的。

代码1,由基类Binding创建一个可重用的自定义绑定:

public class NetTcpTextBinding : Binding    {        private TcpTransportBindingElement transport;        private TextMessageEncodingBindingElement encoding;        public NetTcpTextBinding()            : base()        {            this.Initialize();        }        public override BindingElementCollection CreateBindingElements()        {            BindingElementCollection elements = new BindingElementCollection();            elements.Add(this.encoding);            elements.Add(this.transport);            return elements;        }        public override string Scheme        {            get { return this.transport.Scheme; }        }        private void Initialize()        {            this.transport = new TcpTransportBindingElement();            this.encoding = new TextMessageEncodingBindingElement();        }    }

代码2,使用NetTcpTextBing绑定:

NetTcpTextBinding binding = new NetTcpTextBinding();      ServiceHost host = new ServiceHost(typeof(Service1));      host.AddServiceEndpoint(typeof(IService1),                binding,                "net.tcp://localhost:10101/IService");      host.Open();

如果需要通过配置文件使用自定义绑定,必须先扩展BindingCollectionElement抽象基类,然后再实现需要的方法。

代码3,实现NetTcpTextBindingCollectionElement:

namespace ConsoleApplication1{    public class NetTcpTextBindingCollectionElement : BindingCollectionElement    {        public override Type BindingType        {            get { return typeof(NetTcpTextBinding); }        }        public override ReadOnlyCollection
ConfiguredBindings { get { return new ReadOnlyCollection
(new List
()); } } public override bool ContainsKey(string name) { throw new NotImplementedException(); } protected override Binding GetDefault() { return new NetTcpTextBinding(); } protected override bool TryAdd(string name, Binding binding, Configuration config) { throw new NotImplementedException(); } }}

 

现在,如果要在配置文件中使用自定义绑定NetTcpTextBinding,就必须用绑定扩展(<bindingExtensions>)配置NetTcpTextBindingCollectionElement元素。总之,如果要在配置文件中使用自定义绑定,就需要先进行定义。

代码4,在配置文件中使用自定义绑定NetTcpTextBinding:

 

2013-03-30

 

转载于:https://www.cnblogs.com/yuanli/archive/2013/03/30/2990675.html

你可能感兴趣的文章
图的邻接表存储
查看>>
2018 leetcode
查看>>
各浏览器对 onbeforeunload 事件的支持与触发条件实现有差异
查看>>
PHP中获取当前页面的完整URL
查看>>
所谓输入掩码技术,即只有数字键起作用
查看>>
Display对象,Displayable对象
查看>>
安装oracle11G,10G时都会出现:注册ocx时出现OLE初始化错误或ocx装载错误对话框
查看>>
数据结构(并查集):COGS 260. [NOI2002] 银河英雄传说
查看>>
生产环境下正则的应用实例(一)
查看>>
在CentOS7命令行模式下安装虚拟机
查看>>
Arduino可穿戴开发入门教程Arduino开发环境介绍
查看>>
Windows平台flex+gcc词法分析实验工具包
查看>>
3.Python基础 序列sequence
查看>>
Chapter 4 Syntax Analysis
查看>>
vi/vim使用
查看>>
讨论Spring整合Mybatis时一级缓存失效得问题
查看>>
Maven私服配置Setting和Pom文件
查看>>
Linux搭建Nexus3.X构建maven私服
查看>>
Notepad++使用NppFTP插件编辑linux上的文件
查看>>
NPOI 操作Excel
查看>>