1

I am working on a project that uses WCF service. I have built the service, configured the web.config file, deployed it on a IIS 7 server. The service is accesed through HTTPS (on my dev machine, i have self-created the certificate). Everything is fine when a create the ServiceReference in Visual Studio 2010, it creates the client and it works fine.

What i need is to create a client programatically (need a little flexibility), so when i try to connect "manually", it gives me a error like this:

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

The code for web.config is: (i hope there is nothing wrong in it)

<system.serviceModel>    
    <services>           
      <service name="WcfService1.Service1" behaviorConfiguration="WcfService1.Service1Behavior">
        <endpoint address="" binding="wsHttpBinding" bindingConfiguration="TransportSecurity" contract="WcfService1.IService1" />
        <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange"/>
      </service>

    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="WcfService1.Service1Behavior">
          <serviceMetadata httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="True"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>


    <bindings>
      <wsHttpBinding>
        <binding name="TransportSecurity">
          <security mode="Transport">
            <transport clientCredentialType="None"/>
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>

  </system.serviceModel>

The procedure i wrote to access the WCF service is:

 void proc()
 {
        string ADRESASSL = "https://localhost/ServiciuSSLwsBind/Service1.svc";
        WSHttpBinding bind= new WSHttpBinding();

        EndpointAddress ea = new EndpointAddress(ADRESASSL);
        var myChannelFactory = new ChannelFactory<IService1>(bind, ea);

        IService1 client = null;
        try
        {
            client = myChannelFactory.CreateChannel();
            client.RunMethod1();
            client.Close();                
            //((ICommunicationObject)client).Close();
        }
        catch (Exception exc)
        {
            MessageBox.Show(exc.Message);
            if (client != null)
                client.Close();
        }
    }

The code for IService1

[ServiceContract]
public interface IService1 : IClientChannel
{
    [OperationContract]
    int RunMethod1();

 //....................................
}

It seems i am doing something wrong here, the procedure raises the Exception i mentioned. Something more i must do to work, but i didn't figured it out.

Thanks in advance for any advice you can give me.

kristi_io
  • 439
  • 6
  • 9

2 Answers2

3

I haven't tested this, but I believe you need to set the security mode for the binding before you create the factory. The default mode for security for WSHttpBinding is SecurityMode.Message, and you want SecurityMode.Transport.

You can resolve this one of three ways, as follows.

First, you can use the overloaded version of the WSHttpBinding constructor to specify the security mode, like this:

WSHttpBinding bind= new WSHttpBinding(SecurityMode.Transport);
bind.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;

Secondly, you can use the parameterless constructor and specify the security mode (and the client credential type) like this:

WSHttpBinding bind= new WSHttpBinding();
bind.Security.Mode = SecurityMode.Transport;
bind.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;

Third, you can place a binding configuration section in the client config and reference that section in the constructor, like this:

WSHttpBinding bind = new WSHttpBinding("TransportSecurity");

The third example assumes a wsHttpBinding section with the name "TransportSecurity" in the client config file.

For more information, check these MSDN articles:

How to: Set the Security Mode

WSHttpBinding Constructor

Tim
  • 28,212
  • 8
  • 63
  • 76
  • I will rewrote the code and test it. Thanks for your answer, i shall dig into this. – kristi_io Feb 18 '13 at 07:36
  • Thank, Tim, for the answer, but unfortunately it generates the following error: Could not establish trust relationship for the SSL/TLS secure channel with authority 'localhost'. I presume "localhost" is the local generated certificate. – kristi_io Feb 18 '13 at 18:54
  • I haven't done a lot with certs, but check [this answer](http://stackoverflow.com/a/8854765/745969) out. – Tim Feb 18 '13 at 19:29
  • Yes, i think the problem is with the certificate. It's a self generated certificate (don't have access yet to a certificate issued by a company like certsign etc.) – kristi_io Feb 18 '13 at 20:28
  • Have you tried exporting the certificate and installing it on the client as well? – Tim Feb 18 '13 at 20:34
  • Here's a link that talks about this: [IT: How To Create a Self Signed Security (SSL) Certificate and Deploy it to Client Machines](http://www.howtogeek.com/107415/it-how-to-create-a-self-signed-security-ssl-certificate-and-deploy-it-to-client-machines/) – Tim Feb 18 '13 at 20:39
1

Well, solved the problem with the self created certificate. I have changed the endpoint adress for both the programatically connection and the service reference in Viosual Studio 2010.

string ADRESASSL = "https://localhost/ServiciuSSLwsBind/Service1.svc";

now is

string ADRESASSL = "https://eu-pc/ServiciuSSLwsBind/Service1.svc";

I have changed the adress from localhost to the name of pc "eu-pc". It has to do with the domain the certificate was issued. Using localhost or 127.0.0.1 worked only for one method or the other.

Hope this will help other guys who might run into this.

kristi_io
  • 439
  • 6
  • 9