Search code examples
c#wcfrestclient

ClientBase and https connection. The provided URI scheme 'https' is invalid; expected 'http'. Parameter name: via


I'm trying to use the ClientBase class for communicating to a REST service. Below is my code:

class MyService: ClientBase<IMyService>, IMyService
{
    public GridVisionService(Uri baseAddress)
        : base(new WebHttpBinding(), new EndpointAddress(baseAddress))
    {
        this.Endpoint.Behaviors.Add(new WebHttpBehavior());
    }

    #region IMyServiceMembers

    public void DoSomething()
    {
        using (new OperationContextScope(this.InnerChannel)) // Line that throws the exception
        {
            WebOperationContext.Current.OutgoingRequest.Headers.Add("details", "details");
        } // End of OperationContextScope

        this.Channel.DoSomething();
    }

    #endregion
}

I run my class like the following:

MyService myService = new MyService(new Uri("https://localhost:8080/MyService/"));

However, it throws the exception about expecting HTTP instead of HTTPS. On searching I've found multiple instances of the same error, however in all of the cases I have found the app.config is configuring the connection. In my case I am not. Does anyone know how I can make my service expect to use https?


Solution

  • When you create the WebHttpBinding, set its Security property to Transport.

    System.ServiceModel.WebHttpBinding w = new System.ServiceModel.WebHttpBinding();
    w.Security = new System.ServiceModel.WebHttpSecurity();
    w.Security.Mode = System.ServiceModel.WebHttpSecurityMode.Transport;