views:

304

answers:

2

Hello, I've created the test self-hosted wcf application and tried to add support https. Code of server application is:

using System;
using System.Security.Cryptography.X509Certificates;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Security;

namespace SelfHost
{
    class Program
    {
        static void Main(string[] args)
        {
            string addressHttp = String.Format("http://{0}:8002/hello", System.Net.Dns.GetHostEntry("").HostName);
            Uri  baseAddress = new Uri(addressHttp);
            WSHttpBinding b = new WSHttpBinding();
            b.Security.Mode = SecurityMode.Transport;
            b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
            Uri a = new Uri(addressHttp);
            Uri[] baseAddresses = new Uri[] { a };
            ServiceHost sh = new ServiceHost(typeof(HelloWorldService), baseAddresses);
            Type c = typeof(IHelloWorldService);
            sh.AddServiceEndpoint(c, b, "hello");
            sh.Credentials.ServiceCertificate.SetCertificate(
                StoreLocation.LocalMachine,
                StoreName.My,
                X509FindType.FindBySubjectName,"myCert");
             sh.Credentials.ClientCertificate.Authentication.CertificateValidationMode =
             X509CertificateValidationMode.PeerOrChainTrust;
            try
            {
                sh.Open();

                string address = sh.Description.Endpoints[0].ListenUri.AbsoluteUri;
                Console.WriteLine("Listening @ {0}", address);
                Console.WriteLine("Press enter to close the service");
                Console.ReadLine();
                sh.Close();
            }
            catch (CommunicationException ce)
            {
                Console.WriteLine("A commmunication error occurred: {0}", ce.Message);
                Console.WriteLine();
            }
            catch (System.Exception exc)
            {
                Console.WriteLine("An unforseen error occurred: {0}", exc.Message);
                Console.ReadLine();
            }
        }
    }
    [ServiceContract]
    public interface IHelloWorldService
    {
        [OperationContract]
        string SayHello(string name);
    }

    public class HelloWorldService : IHelloWorldService
    {
        public string SayHello(string name)
        {
            return string.Format("Hello, {0}", name);
        }
    }
}

What name(address) should I out into line

sh.AddServiceEndpoint(c, b, "hello");

because "hello" is incorrect ?

Thanks.

+1  A: 
 sh.AddServiceEndpoint(c, b, "https://xxxx:xx/service");
Sander Pham
Like string addressHttp = String.Format("http://{0}:8002/hello", System.Net.Dns.GetHostEntry("").HostName); only changes is https ?
jitm
A: 

Basically, the third parameter in AddServiceEndpoint is the address of the service.

If you have defined a base address (as you have - http://{0}:8002/hello), it's a relative address - it will be added to the base address for the appropriate protocol.

So in your case, by adding this service endpoint, you'd get an endpoint at:

http://{0}:8002/hello/hello

Can you connect to that endpoint and talk to the service??

Or you can define a fully specified address - that's especially useful if you don't have any base addresses. If you specify a full address, that address will be used (overriding the base address defined). So if you use:

AddServiceEndpoint(c, b, "http://server:8888/HelloService")

then your service will be accessible at that specific URL - regardless of your base address defined before.

Update: thanks for your comment. Yes, if you define the security mode to be "Transport", then you need to use https:// for all your addresses.

Defining base address:

string addressHttp = String.Format("https://{0}:8002/hello", System.Net.Dns.GetHostEntry("").HostName);

or overriding with a fully qualified address:

AddServiceEndpoint(c, b, "https://server:8888/HelloService")
marc_s
Thanks, currently server started without errors but if I try to bind client side to the server I'm receiving next error:There was an error downloading 'https://server:8002/hello'.The underlying connection was closed: An unexpected error occurred on a send.Authentication failed because the remote party has closed the transport stream.Metadata contains a reference that cannot be resolved: 'https://server:8002/hello'. Continuation of error message in the next comment ...
jitm
An error occurred while making the HTTP request to https://server:8002/hello. This could be due to the fact that the server certificate is not configured properly with HTTP.SYS in the HTTPS case. This could also be caused by a mismatch of the security binding between the client and the server.The underlying connection was closed: An unexpected error occurred on a send.Authentication failed because the remote party has closed the transport stream.If the service is defined in the current solution, try building the solution and adding the service reference again.How to fix it?
jitm
In case if I do httpcfg query sslI can see configurable HTTP.SYS------------------------------------------------------------------------------ IP : 0.0.0.0:8002 Hash : 1111111111111111111111111111111111111 Guid : {00000000-0000-0000-0000-000000000000} CertStoreName : (null) CertCheckMode : 0 RevocationFreshnessTime : 0 UrlRetrievalTimeout : 0 SslCtlIdentifier : (null) SslCtlStoreName : (null) Flags : 0
jitm