Guest User

Untitled

a guest
Aug 7th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. public class DynatracePurePathHeaderAppender : IClientMessageInspector, IEndpointBehavior
  2. {
  3. object IClientMessageInspector.BeforeSendRequest(ref Message request, IClientChannel channel)
  4. {
  5. var dynaHeader = MessageHeader.CreateHeader("Action", "ns.yellowbook.jeff", "dynatrace",false);
  6. request.Headers.Add(dynaHeader);
  7. return null;
  8. }
  9.  
  10. void IClientMessageInspector.AfterReceiveReply(ref Message reply, object correlationState)
  11. {
  12. return;
  13. }
  14.  
  15. public void Validate(ServiceEndpoint endpoint){}
  16.  
  17. public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters){}
  18.  
  19. public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher){}
  20.  
  21. public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
  22. {
  23. clientRuntime.MessageInspectors.Add(this);
  24. }
  25. }
  26.  
  27. public class DynatracePurePathHeaderAppenderElement : BehaviorExtensionElement
  28. {
  29. protected override object CreateBehavior()
  30. {
  31. return new DynatracePurePathHeaderAppender();
  32. }
  33.  
  34. public override Type BehaviorType
  35. {
  36. get { return typeof(DynatracePurePathHeaderAppender); }
  37. }
  38. }
  39.  
  40. HttpRequestMessageProperty hrmp = new HttpRequestMessageProperty();
  41. //Set hrmp.Headers, then:
  42. request.Properties.Add(HttpRequestMessageProperty.Name, hrmp);
  43.  
  44. var httpHeader = reply.Properties["httpResponse"] as HttpResponseMessageProperty;
  45.  
  46. httpHeader.Headers.Add("my Custom Header", "My Value");
  47.  
  48. Public Class AuthenticationHeader
  49. Implements IClientMessageInspector
  50.  
  51. Private itsUser As String
  52. Private itsPass As String
  53.  
  54. Public Sub New(ByVal user As String, ByVal pass As String)
  55. itsUser = user
  56. itsPass = pass
  57. End Sub
  58.  
  59. Public Sub AfterReceiveReply(ByRef reply As Message, correlationState As Object) Implements IClientMessageInspector.AfterReceiveReply
  60. Console.WriteLine("Received the following reply: '{0}'", reply.ToString())
  61. End Sub
  62.  
  63. Public Function BeforeSendRequest(ByRef request As Message, channel As IClientChannel) As Object Implements IClientMessageInspector.BeforeSendRequest
  64. Dim hrmp As HttpRequestMessageProperty = request.Properties("httpRequest")
  65. Dim encoded As String = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(itsUser + ":" + itsPass))
  66. hrmp.Headers.Add("Authorization", "Basic " + encoded)
  67. Return request
  68. End Function
  69. End Class
  70.  
  71. Public Class AuthenticationHeaderBehavior
  72. Implements IEndpointBehavior
  73.  
  74. Private ReadOnly itsUser As String
  75. Private ReadOnly itsPass As String
  76.  
  77. Public Sub New(ByVal user As String, ByVal pass As String)
  78. MyBase.New()
  79. itsUser = user
  80. itsPass = pass
  81. End Sub
  82.  
  83. Public Sub AddBindingParameters(endpoint As ServiceEndpoint, bindingParameters As BindingParameterCollection) Implements IEndpointBehavior.AddBindingParameters
  84. End Sub
  85.  
  86. Public Sub ApplyClientBehavior(endpoint As ServiceEndpoint, clientRuntime As ClientRuntime) Implements IEndpointBehavior.ApplyClientBehavior
  87. clientRuntime.MessageInspectors.Add(New AuthenticationHeader(itsUser, itsPass))
  88. End Sub
  89.  
  90. Public Sub ApplyDispatchBehavior(endpoint As ServiceEndpoint, endpointDispatcher As EndpointDispatcher) Implements IEndpointBehavior.ApplyDispatchBehavior
  91. End Sub
  92.  
  93. Public Sub Validate(endpoint As ServiceEndpoint) Implements IEndpointBehavior.Validate
  94. End Sub
  95. End Class
  96.  
  97. Dim binding As New WebHttpBinding(WebHttpSecurityMode.Transport)
  98. binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None
  99.  
  100. ChlFactory = New WebChannelFactory(Of IMyServiceContract)(binding, New Uri(url))
  101. ChlFactory.Endpoint.Behaviors.Add(New WebHttpBehavior())
  102. ChlFactory.Endpoint.Behaviors.Add(New AuthenticationHeaderBehavior(user, pass))
  103. Channel = ChlFactory.CreateChannel()
Add Comment
Please, Sign In to add comment