Amit Pal
Amit Pal

Reputation: 11062

How to access WCF WebServiceHost endpoint with HTTPS?

I have successfully created WCF self-hosted HTTP web service. I use webserviceHost to create this service. I didn't make any changes in web.config file. Here is my code:

Instance.cs:

 [OperationContract, WebInvoke(Method = "GET", UriTemplate = "/getstatus/", ResponseFormat = WebMessageFormat.Json)]
    bool getstatus();

service.cs:

 public bool getstatus()
    {
        return true;
    }

BindingWS.cs

 void bindservice()
    {
        try
        {
            m_running = true;
            // Start the host
            m_serviceHost = new WebServiceHost(typeof(Swiper.cs), new Uri(http://localhost:8083"));
            ServiceEndpoint ep = m_serviceHost.AddServiceEndpoint(typeof(Instace.cs), new WebHttpBinding(), "");
            m_serviceHost.Open();
            while (m_running)
            {
                // Wait until thread is stopped
                Thread.Sleep(SleepTime);
            }

            // Stop the host
            m_serviceHost.Close();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
            if (m_serviceHost != null)
            {
                m_serviceHost.Close();
            }
        }
    }

Above code works fine to run WCF at Htpp. but how i convert it into HTTPS. I followes so many blogs but didn't got anything. Is it possible to do it?

Upvotes: 2

Views: 5764

Answers (1)

stephenl
stephenl

Reputation: 3129

Basically you need to manually register the certificate against the port number you are using in your host. Here is some details on how to accomplish that

http://msdn.microsoft.com/en-us/library/ms733791.aspx

UPDATE 21/2/13:

Provided that you have registered your certificate for the domain as described above you should be able to get this working by making some adjustments to the code above. Here is some sample code using a console application as a host. HTH.

    using (var serviceHost = new WebServiceHost(typeof(Swiper), new Uri("https://localhost:8083")))
        {
            var secureWebHttpBinding = new WebHttpBinding(WebHttpSecurityMode.Transport) { Name = "secureHttpWeb" };
            serviceHost.AddServiceEndpoint(typeof(ISwiper), secureWebHttpBinding, "");
            serviceHost.Open();
            Console.WriteLine("Service running...");
            Console.WriteLine("Press any key to stop service.");
            Console.ReadLine();

            // Stop the host
            serviceHost.Close();
        }

Upvotes: 1

Related Questions