Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class HeaderAdder : ContextBoundObject, IClientMessageInspector
- {
- public bool RequestFailedDueToAuthentication;
- public string UserName { get; set; }
- public string Password { get; set; }
- public object BeforeSendRequest(ref Message request, IClientChannel channel)
- {
- var property = new UserNameHeader
- {
- Password = Password,
- UserName = UserName
- };
- request.Headers.Add(MessageHeader.CreateHeader("UserNameHeader", "test", property));
- return null;
- }
- public void AfterReceiveReply(ref Message reply, object correlationState)
- {
- RequestFailedDueToAuthentication = reply.ToString().Contains("ErrorCode>-4<");
- }
- }
- public class CustomEndpointBehavior : IEndpointBehavior
- {
- private readonly HeaderAdder _headerAdder;
- public CustomEndpointBehavior(HeaderAdder headerAdder)
- {
- _headerAdder = headerAdder;
- }
- public void Validate(ServiceEndpoint endpoint)
- {
- //throw new NotImplementedException();
- }
- public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
- {
- //throw new NotImplementedException();
- }
- public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
- {
- //throw new NotImplementedException();
- }
- public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
- {
- var credentials = endpoint.Behaviors.Find<ClientCredentials>();
- if (!string.IsNullOrEmpty(credentials.UserName.Password))
- {
- _headerAdder.UserName = credentials.UserName.UserName;
- _headerAdder.Password = credentials.UserName.Password;
- clientRuntime.ClientMessageInspectors.Add(_headerAdder);
- }
- }
- }
Add Comment
Please, Sign In to add comment