WCF Service : WSHttpBinding
        Posted  
        
            by jitm
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by jitm
        
        
        
        Published on 2010-06-17T12:30:15Z
        Indexed on 
            2010/06/17
            12:43 UTC
        
        
        Read the original article
        Hit count: 771
        
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.
© Stack Overflow or respective owner