Message Header سفارشی در WCF
نویسنده: مسعود پاکدل
تاریخ: ۱۳۹۲/۰۸/۲۰ ۲۱:۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
[DataContract]
public class ApplicationContext
{
[DataMember( IsRequired = true )]
public string UserId
{
get { return _userId; }
set
{
_userId = value;
}
}
private string _userId;
[DataMember( IsRequired = true )]
public static ApplicationContext Current
{
get
{
return _current;
}
private set { _current = value; }
}
private static ApplicationContext _current;
public static void Register( ApplicationContext appContext )
{
Current = appContext;
IsRegistered = true;
}
} public class ClientMessageHeaderInspector<T> : IClientMessageInspector
{
private readonly T _vaccine;
public ClientMessageHeaderInspector( T vaccine )
{
this._vaccine = vaccine;
}
public void AfterReceiveReply( ref Message reply, object correlationState )
{
}
public object BeforeSendRequest( ref Message request, IClientChannel channel )
{
MessageHeader messageHeader = MessageHeader.CreateHeader( typeof( T ).Name, typeof( T ).Namespace, this._vaccine );
request.Headers.Add( messageHeader );
return null;
}
} public class ApplicationContextMessageBehavior : IEndpointBehavior
{
ClientMessageHeaderInspector<ApplicationContext> inspector = null;
public ApplicationContextMessageBehavior()
{
inspector = new ClientMessageHeaderInspector<ApplicationContext>( ApplicationContext.Current );
}
public void AddBindingParameters( ServiceEndpoint endpoint, BindingParameterCollection bindingParameters )
{
}
public void ApplyClientBehavior( ServiceEndpoint endpoint, ClientRuntime clientRuntime )
{
clientRuntime.MessageInspectors.Add( inspector );
}
public void ApplyDispatchBehavior( ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher )
{
}
public void Validate( ServiceEndpoint endpoint )
{
}
} public class ServiceMapper<TChannel>
{
internal static EndpointAddress EPAddress
{
get
{
return _epAddress;
}
}
private static EndpointAddress _epAddress;
public static TChannel CreateChannel( Binding binding, string uriBase, string serviceName, bool setCredential )
{
_epAddress = new EndpointAddress( String.Format( "{0}{1}", uriBase, serviceName ) );
var factory = new ChannelFactory<TChannel>( binding, _epAddress );
ApplicationContext.Register( new ApplicationContext
{
UserId = Guid.NewGuid()
} );
factory.Endpoint.Behaviors.Add( new ApplicationContextMessageBehavior() );
TChannel proxy = factory.CreateChannel();
if ( factory.Endpoint.Behaviors.OfType<ApplicationContextMessageBehavior>().Any() )
{
using ( var scope = new OperationContextScope( ( IClientChannel )proxy ) )
{
OperationContext.Current.OutgoingMessageHeaders.Add( MessageHeader.CreateHeader( typeof( ApplicationContext ).Name, typeof( ApplicationContext ).Namespace, ApplicationContext.Current ) );
}
}
return proxy;
} OperationContext.Current.OutgoingMessageHeaders.Add( MessageHeader.CreateHeader( typeof( ApplicationContext ).Name, typeof( ApplicationContext ).Namespace, AppConfiguration.Application ) );
if ( OperationContext.Current != null && OperationContext.Current.IncomingMessageHeaders.FindHeader( typeof( ApplicationContext ).Name , typeof( ApplicationContext ).Namespace ) > 0 )
{
_application = OperationContext.Current.IncomingMessageHeaders.GetHeader<ApplicationContext>( typeof( ApplicationContext ).Name , typeof( ApplicationContext ).Namespace );
}