1

I have in my computer a WSDL link to my local EndPoint. I have generated the Service reference and can access everything fine when i do it locally i.e. http://localhost/ApplicationHost/EndPoint

If I use another computer(in the network) the request is done via IP i.e. Http://15.5.20.10/ApplicationHost/EndPoint but this will respond a 400 Bad request.

Changing the Host of the request to "localhost" (as tested on firefox and android) works fine.

enter image description here

The image has the perfect functional case. The Address has the IP and the host is "localhost" but I cant manage to replicate this in c#.

How can I do the same using C# and the Service reference? I have tried so far a lot of solutions that ADDS a http header but none of them will acctually change a existing one: i.e Adding http headers to a Service Reference service method or How to add a custom HTTP header to every WCF call? or how to modify http header of request; web reference in C#

Ink Archer
  • 153
  • 1
  • 8

1 Answers1

0

On the client-side, we can modify the header through IClientMessageInspector,here is a demo:

   public class ClientMessageLogger : IClientMessageInspector
    {
        public void AfterReceiveReply(ref Message reply, object correlationState)
        { 
        }

        public object BeforeSendRequest(ref Message request, IClientChannel channel)
        {
            HttpRequestMessageProperty header;
            if (request.Properties.ContainsKey(HttpRequestMessageProperty.Name))
            {
                header = (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name];
            }
            else
            {
                header = new HttpRequestMessageProperty();
                request.Properties.Add(HttpRequestMessageProperty.Name, header);
            }

            header.Headers["Host"] = "localhost";
            return null;
        }
    }

ClientMessageLogger implements the IClientMessageInspector interface, and I modify its request header before the client post a request.

 [AttributeUsage(AttributeTargets.Interface | AttributeTargets.Class, AllowMultiple = false)]
    public class CustContractBehaviorAttribute : Attribute, IContractBehavior, IContractBehaviorAttribute, IOperationBehavior
    {
        public Type TargetContract => throw new NotImplementedException();

        public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
            return;
        }

        public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
        {
            throw new NotImplementedException();
        }

        public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
            clientRuntime.ClientMessageInspectors.Add(new ClientMessageLogger());
        }

        public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
        {
            throw new NotImplementedException();
        }

        public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
        {
        }

        public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
        {

        }

        public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
        {
            return;
        }

        public void Validate(OperationDescription operationDescription)
        {
            throw new NotImplementedException();
        }
    }

We add the ClientMessageLogger to the client's behavior.

    [CustContractBehavior]
    public interface IService
    {...}

Finally, we add this behavior to the interface of the service.

Ding Peng
  • 3,702
  • 1
  • 5
  • 8