BindingElement 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
为用于处理传出和传入消息的各种通道的通道工厂和通道侦听器的绑定元素。
public ref class BindingElement abstract
public abstract class BindingElement
type BindingElement = class
Public MustInherit Class BindingElement
- 继承
-
BindingElement
- 派生
示例
以下示例演示如何将传输绑定元素添加到自定义绑定,然后生成可以接受传入消息的通道侦听器。
CustomBinding binding = new CustomBinding();
binding.Elements.Add(new HttpTransportBindingElement());
BindingParameterCollection paramCollection = new BindingParameterCollection();
IChannelListener<IReplyChannel> listener = binding.BuildChannelListener<IReplyChannel>
(new Uri("http://localhost:8000/ChannelApp"), paramCollection);
listener.Open();
IReplyChannel channel = listener.AcceptChannel();
Console.WriteLine("Listening for messages");
channel.Open();
RequestContext request = channel.ReceiveRequest();
Message msg = request.RequestMessage;
Console.WriteLine("Message Received");
Console.WriteLine("Message Action: {0}", msg.Headers.Action);
if (msg.Headers.Action == "hello")
{
Message reply = Message.CreateMessage(MessageVersion.Default, "wcf");
request.Reply(reply);
}
msg.Close();
channel.Close();
listener.Close();
Dim binding As New CustomBinding()
binding.Elements.Add(New HttpTransportBindingElement())
Dim paramCollection As New BindingParameterCollection()
Dim listener As IChannelListener(Of IReplyChannel)
listener = binding.BuildChannelListener(Of IReplyChannel)(New Uri("http://localhost/channelApp"), paramCollection)
listener.Open()
Dim channel As IReplyChannel = listener.AcceptChannel()
Console.WriteLine("Listening for messages")
channel.Open()
Dim request As RequestContext = channel.ReceiveRequest()
Dim msg As Message = request.RequestMessage
Console.WriteLine("Message Received")
Console.WriteLine("Message Action: {0}", msg.Headers.Action)
If (msg.Headers.Action = "hello") Then
Dim reply As Message = Message.CreateMessage(MessageVersion.Default, "wcf")
request.Reply(reply)
End If
msg.Close()
channel.Close()
listener.Close()
注解
绑定由一组从此抽象基类继承的有序绑定元素组成。 类 TransportBindingElement 继承自 BindingElement 该类。
如果只是使用通道模型,则为传输创建绑定和绑定元素是可选的。 只要通道工厂公开,就可以通过通道工厂执行所需的一切操作,并倾听者。
Windows Communication Foundation (WCF) 服务模型使用工厂模式,其中绑定用于创建通道堆栈。 如果要使用 WCF 服务模型,则需要使用传输绑定元素。 将此绑定元素放入绑定是一个很好的做法,因为它消除了用户为传输创建自己的自定义绑定的要求。 最好同时创建绑定和绑定元素,并在程序集内隐藏通道工厂和侦听器。
在发送端,绑定用于生成通道 IChannelFactory堆栈,并返回对堆栈中顶部通道的引用。 然后,应用程序可以使用此通道发送消息。
同样,在接收端,绑定用于生成 IChannelListener侦听传入消息的绑定。 通过创建通道堆栈并将应用程序交给顶部通道,向 IChannelListener 侦听应用程序提供消息。 然后,应用程序使用此通道接收传入消息。
注释
如果要添加必须加密的消息标头,则必须在请求ChannelProtectionRequirements时从GetProperty<T>(BindingContext)方法返回ChannelProtectionRequirements具有要求的实例。
构造函数
| 名称 | 说明 |
|---|---|
| BindingElement() |
初始化 BindingElement 类的新实例。 |
| BindingElement(BindingElement) |
从指定的绑定元素初始化类的新实例 BindingElement 。 |
方法
| 名称 | 说明 |
|---|---|
| BuildChannelFactory<TChannel>(BindingContext) |
初始化通道工厂,用于从绑定上下文生成指定类型的通道。 |
| BuildChannelListener<TChannel>(BindingContext) |
初始化通道侦听器以接受绑定上下文中指定类型的通道。 |
| CanBuildChannelFactory<TChannel>(BindingContext) |
返回一个值,该值指示绑定元素是否可以为特定类型的通道生成通道工厂。 |
| CanBuildChannelListener<TChannel>(BindingContext) |
返回一个值,该值指示绑定元素是否可以为特定类型的通道生成侦听器。 |
| Clone() |
在派生类中重写时,返回绑定元素对象的副本。 |
| Equals(Object) |
确定指定的对象是否等于当前对象。 (继承自 Object) |
| GetHashCode() |
用作默认哈希函数。 (继承自 Object) |
| GetProperty<T>(BindingContext) |
在派生类中重写时,从绑定堆栈中的相应层返回所请求的类型化对象(如果存在)。 |
| GetType() |
获取当前实例的 Type。 (继承自 Object) |
| MemberwiseClone() |
创建当前 Object的浅表副本。 (继承自 Object) |
| ToString() |
返回一个表示当前对象的字符串。 (继承自 Object) |