2

I'm trying to send SOAP Request to Amadeus Enterprise WSAP. they provide little documentation for how to connect, I followed their code sample

class SecurityTokenInspector : IClientMessageInspector
{
    .....
    public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
    {
        if (channel.State == CommunicationState.Closed)
        {
            UsernameToken token = new UsernameTokenBP10(this.username, this.password, PasswordOption.SendHashed);
            XmlElement securityToken = token.GetXml(new XmlDocument());
            MessageHeader securityHeader = MessageHeader.CreateHeader("Security", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", securityToken, false);
            request.Headers.Add(securityHeader);
        }
        return null;
    }
    .....
}

Here is the using:

AmadeusWebServicesPTClient client = new AmadeusWebServicesPTClient("AmadeusWebServicesPort");

            SecurityTokenInspector iSecurity = new SecurityTokenInspector(ConfigurationManager.AppSettings["user"], ConfigurationManager.AppSettings["pass"]);
            SecurityBehavior behavior = new SecurityBehavior
            {
                inspector = iSecurity
            };
            client.Endpoint.Behaviors.Add(behavior);

but BeforeSendRequest not called.

Can anyone help?

  • So you did invoke a `client.ServiceMethod`, right? Because just adding the behavior will not call anything. – rene Dec 26 '19 at 18:18

1 Answers1

1

Your code snippets seem no problem. How do you define the SecurityBehavior implementing the IEndpointBehavior interface and BehaviorExtensionElement abstract class? Here is my example, I wish it is useful to you.
ClientMessageLogger.

    public class ClientMessageLogger : IClientMessageInspector
    {
        public void AfterReceiveReply(ref Message reply, object correlationState)
        {
            string displayText = $"the client has received the reply:\n{reply}\n";
            Console.Write(displayText);
        }

        public object BeforeSendRequest(ref Message request, IClientChannel channel)
        {
            request.Headers.Add(MessageHeader.CreateHeader("myheader", "", "myvalue"));
            string displayText = $"the client send request message:\n{request}\n";
            Console.WriteLine(displayText);
            return null;
        }
}

EndpointBehavior.

public class AuthBehavior : BehaviorExtensionElement, IEndpointBehavior
    {
        public override Type BehaviorType => typeof(AuthBehavior);

        public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
        }
        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
            ClientMessageLogger inspector = new ClientMessageLogger();
            clientRuntime.ClientMessageInspectors.Add(inspector);
        }
        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
        }
        public void Validate(ServiceEndpoint endpoint)
        {
        }
        protected override object CreateBehavior()
        {
            return new AuthBehavior();
        }
}

Sample.

ServiceReference1.ServiceClient client = new ServiceReference1.ServiceClient();
//apply the endpoint behavior.
            client.Endpoint.Behaviors.Add(new AuthBehavior());
            var result = client.SayHello();
            Console.WriteLine(result);

Result.
enter image description here
Feel free to let me know if the problem still exists.

Abraham Qian
  • 7,117
  • 1
  • 8
  • 22
  • I tried your code and code from [link](https://social.msdn.microsoft.com/Forums/Lync/en-US/3a3ad168-2363-4115-bab3-ba5d18b715e0/how-can-i-change-the-wssecurity-prefix?forum=wcf), not working I don't know how to solve it, both codes are identical !!!!! – Mohammad Kalloub Dec 28 '19 at 01:27
  • Please post your whole code, I will try to pinpoint the problem. There is a difference between these two ways. One uses Service Contract extension and the other uses endpoint behavior extension. According to your above your code, we should use endpoint behavior extension. – Abraham Qian Dec 30 '19 at 01:43