MessageEncoderFactory 类
定义
一个抽象基类,表示用于生成消息编码器的工厂,消息编码器可从流中读取消息和将消息写入流以进行多种类型的消息编码。An abstract base class that represents the factory for producing message encoders that can read messages from a stream and write them to a stream for various types of message encoding.
public ref class MessageEncoderFactory abstract
public abstract class MessageEncoderFactory
type MessageEncoderFactory = class
Public MustInherit Class MessageEncoderFactory
- 继承
-
MessageEncoderFactory
示例
下面的代码演示如何编写派生自 MessageEncoderFactory 的类:The following code shows how to write a class that is derived from MessageEncoderFactory:
public override bool IsContentTypeSupported(string contentType)
{
if (base.IsContentTypeSupported(contentType))
{
return true;
}
if (contentType.Length == this.MediaType.Length)
{
return contentType.Equals(this.MediaType, StringComparison.OrdinalIgnoreCase);
}
else
{
if (contentType.StartsWith(this.MediaType, StringComparison.OrdinalIgnoreCase)
&& (contentType[this.MediaType.Length] == ';'))
{
return true;
}
}
return false;
}
public class CustomTextMessageEncoderFactory : MessageEncoderFactory
{
private MessageEncoder encoder;
private MessageVersion version;
private string mediaType;
private string charSet;
internal CustomTextMessageEncoderFactory(string mediaType, string charSet,
MessageVersion version)
{
this.version = version;
this.mediaType = mediaType;
this.charSet = charSet;
this.encoder = new CustomTextMessageEncoder(this);
}
public override MessageEncoder Encoder
{
get
{
return this.encoder;
}
}
public override MessageVersion MessageVersion
{
get
{
return this.version;
}
}
internal string MediaType
{
get
{
return this.mediaType;
}
}
internal string CharSet
{
get
{
return this.charSet;
}
}
}
注解
编码是将消息转换为一个字节序列的过程。Encoding is the process of transforming a message into a sequence of bytes. 解码是反向过程。Decoding is the reverse process.
如果希望实现自定义消息编码器,请使用此类。Use this class if you want to implement a custom message encoder. 若要实现自己的自定义消息编码器,必须提供以下三个抽象基类的自定义实现:To implement your own custom message encoder, you must provide custom implementations of the following three abstract base classes:
重写 Encoder 以返回自定义 MessageEncoder 的实例。Override the Encoder to return an instance of your custom MessageEncoder. 然后通过重写 MessageEncoderFactory 方法返回此工厂的实例,将自定义 CreateMessageEncoderFactory 连接到用于配置服务或客户端的绑定元素堆栈。Then wire up your custom MessageEncoderFactory to the binding element stack used to configure the service or client by overriding the CreateMessageEncoderFactory method to return an instance of this factory. 有关自定义编码器的详细信息,请参阅 数据传输和序列化。For more information about custom encoders, see Data Transfer and Serialization.
构造函数
| MessageEncoderFactory() |
初始化 MessageEncoderFactory 类的新实例。Initializes a new instance of the MessageEncoderFactory class. |
属性
| Encoder |
在派生类中重写时,获取工厂生成的消息编码器。When overridden in a derived class, gets the message encoder that is produced by the factory. |
| MessageVersion |
在派生类中重写时,获取工厂生成用于编码消息的编码器所使用的消息版本。When overridden in a derived class, gets the message version that is used by the encoders produced by the factory to encode messages. |
方法
| CreateSessionEncoder() |
返回一个消息编码器,可用于关联基于会话的交换中的消息。Returns a message encoder that can be used to correlate messages in session-based exchanges. |
| Equals(Object) |
确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object. (继承自 Object) |
| GetHashCode() |
作为默认哈希函数。Serves as the default hash function. (继承自 Object) |
| GetType() |
获取当前实例的 Type。Gets the Type of the current instance. (继承自 Object) |
| MemberwiseClone() |
创建当前 Object 的浅表副本。Creates a shallow copy of the current Object. (继承自 Object) |
| ToString() |
返回表示当前对象的字符串。Returns a string that represents the current object. (继承自 Object) |