自定义消息编码绑定实现
扩展WCF 消息编码绑定,需实现MessageEncodingBindingElement,MessageEncoder,MessageEncoderFactory.如果希望通过配置的方式应用我们实现的绑定,则还需要实现BindingElementExtensionElement。
首先从消息编码绑定实现开始;实现MessageEncoderBindingElement
public abstract class CommonMessageEncodingBindingElement : MessageEncodingBindingElement { protected MessageEncodingBindingElement innerBindingElement; protected CommonMessageEncodingBindingElement() : this(new TextMessageEncodingBindingElement()) { } protected CommonMessageEncodingBindingElement( MessageEncodingBindingElement textMessageEncodingBindingElement) { innerBindingElement = textMessageEncodingBindingElement; } public MessageEncodingBindingElement InnerBindingElement { get { return innerBindingElement; } set { innerBindingElement = value; } } public override MessageVersion MessageVersion { get { return innerBindingElement.MessageVersion; } set { innerBindingElement.MessageVersion = value; } } public override IChannelFactory<TChannel> BuildChannelFactory<TChannel>(BindingContext context) { context.BindingParameters.Add(this); return context.BuildInnerChannelFactory<TChannel>(); } public override IChannelListener<TChannel> BuildChannelListener<TChannel>(BindingContext context) { context.BindingParameters.Add(this); return context.BuildInnerChannelListener<TChannel>(); } public override bool CanBuildChannelFactory<TChannel>(BindingContext context) { return innerBindingElement.CanBuildChannelFactory<TChannel>(context); } }
其次;实现MessageEncoder。代码如下;
public abstract class CommonMessageEncoder:MessageEncoder { public MessageEncoder encoder; protected CommonMessageEncoder(MessageEncoder messageEncoder) { encoder = messageEncoder; } public override string ContentType { get { return encoder.ContentType; } } public override string MediaType { get { return encoder.MediaType; } } public override MessageVersion MessageVersion { get { return encoder.MessageVersion; } } }
再次;实现ServerMessageEncoderFactory
internal class ServerMessageEncoderFactory : MessageEncoderFactory { private readonly MessageEncoder encoder; public ServerMessageEncoderFactory(MessageEncoderFactory messageEncoderFactory) { encoder = new ServerMessageEncoder(messageEncoderFactory.Encoder); } public override MessageEncoder Encoder { get { return encoder; } } public override MessageVersion MessageVersion { get { return encoder.MessageVersion; } } }
最后通过配置应用到WCF 。配置实现开始;实现BindingElementExtensionElement.
public class ServerBindingExtension : CommonBindingElementExtensionElement { public override Type BindingElementType { get { return typeof (ServerMessageEncodingBindingElement); } } protected override BindingElement CreateBindingElement() { ServerMessageEncodingBindingElement serverMessageEncodingBindingElement = new ServerMessageEncodingBindingElement(); ApplyConfiguration(serverMessageEncodingBindingElement); return serverMessageEncodingBindingElement; } }
服务端配置;
<system.serviceModel> <extensions> <bindingElementExtensions> <add name="myCustomerBinding" type="Hosting.ServerBindingExtension,Hosting"/> </bindingElementExtensions> </extensions> <bindings> <customBinding> <binding name="customerBinding"> <myCustomerBinding/> <httpTransport/> </binding> </customBinding> </bindings> <services> <service name="Service.AddService"> <endpoint address="http://127.0.0.1:8088/AddService" binding="customBinding" contract="Contract.IAdd" bindingConfiguration="customerBinding" ></endpoint> </service> </services> </system.serviceModel>
客户端配置;
<system.serviceModel> <extensions> <bindingElementExtensions> <add name="clientBinding" type="Client.ClientBindingExtension,Client"/> </bindingElementExtensions> <behaviorExtensions> <add name="clientEndpoint" type="Client.ClientMessageInspector,Client"/> </behaviorExtensions> </extensions> <behaviors> <endpointBehaviors> <behavior> <clientEndpoint /> </behavior> </endpointBehaviors> </behaviors> <bindings> <customBinding> <binding name="customerBinding"> <clientBinding /> <httpTransport></httpTransport> </binding> </customBinding> </bindings> <client> <endpoint address="http://127.0.0.1:8088/AddService" binding="customBinding" contract="Contract.IAdd" name="customEndpoint" bindingConfiguration="customerBinding"></endpoint> </client> </system.serviceModel>
服务端运行图;
客户端调用效果;