WCF Service using https

时间:2017-06-15 10:21:37

标签: c# web-services wcf https

I try to get a WCF Service running using https. The following code works for http

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup> 
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
</configuration>

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Threading.Tasks;
using System.Web;


namespace WebServiceTest
{

    class Program
    {
        static void Main(string[] args)
        {
            using (var serviceHost = new WebServiceHost(typeof(service), new Uri("http://localhost:8083")))
            {
                var secureWebHttpBinding = new WebHttpBinding() { Name = "secureHttpWeb" };
                serviceHost.AddServiceEndpoint(typeof(Instance), secureWebHttpBinding, "");
                serviceHost.Open();
                Console.WriteLine("Service running...");
                Console.WriteLine("Press any key to stop service.");
                Console.ReadLine();

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

        }
    }

    [ServiceContract(Namespace = "")]
    interface Instance
    {
        [OperationContract]
        [WebGet(UriTemplate = "/getstatus/", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
        string getStatus();
    }

    class service : Instance
    {
        public string getStatus()
        {

            return "test";
        }
    }


}

So what I tried to get this running for https is change the uri to use https and add the WebHttpSecurityMode.Transport to the constructor of WebHttpBinding

Instead of

using (var serviceHost = new WebServiceHost(typeof(service), new Uri("http://localhost:8083")))

it would be

using (var serviceHost = new WebServiceHost(typeof(service), new Uri("https://localhost:8083")))

and instead of

var secureWebHttpBinding = new WebHttpBinding() { Name = "secureHttpWeb" };

it would be

var secureWebHttpBinding = new WebHttpBinding(WebHttpSecurityMode.Transport) { Name = "secureHttpWeb" };

I don't get an exception, but the endpoint is not reachable. What am I missing?

0 个答案:

没有答案