Click here to Skip to main content
13,733,090 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


38 bookmarked
Posted 26 Nov 2010
Licenced CPOL

WCF self hosted service over HTTPS

, 26 Nov 2010
Rate this:
Please Sign up or sign in to vote.
A guide for creating a self hosted WCF SOAP Service over HTTPS.


With this article, C# programmers get a very easy way of creating a self hosted WCF Service, discover different WCF bindings, and most importantly, see a step by step guide for installing a server certificate and run their WCF Service over HTTPS.

The code

First of all, we will handle the process of creating the Windows Service that will host the WCF Web Service and set-up the configuration needed for WCF. Create a Windows Service project in Visual Studio, and then add an Installer class using Add Item. Add the method GetServiceProcessInstaller where you can specify the account under which the Windows Service will be installed. Then, add the method GetServiceInstaller where you need to specify a Name and a Description of the Windows Service.

private static ServiceProcessInstaller GetServiceProcessInstaller()
    ServiceProcessInstaller installer = 
      new ServiceProcessInstaller { Account = ServiceAccount.LocalSystem };
    return installer;

private static ServiceInstaller GetServiceInstaller()
    Process pc = Process.GetCurrentProcess();

    ServiceInstaller installer = new ServiceInstaller
        ServiceName = "WCF Over Https",
        Description = "WCF Over Https"

    return installer;

Place these two lines of code in the Installer class constructor:


Now let's create the interfaces that we need in our WCF Service. This WCF Service will host two different bindings, a SOAP one and an HTTP one that will be used for downloading files.

using System.IO;
using System.ServiceModel;
using System.ServiceModel.Web;

namespace WCFOverHttps.WCFHost
    [ServiceContract(Namespace = "WCFOverHttps.WCFHost")]
    public interface IWcfHttpService
        [WebGet(UriTemplate = "GetFile/{path}")]
        Stream GetFile(string path);

using System.ServiceModel;

namespace WCFOverHttps.WCFHost
    [ServiceContract(Namespace = "WCFOverHttps.WCFHost")]
    public interface IWcfSoapService
        string HelloWorld();

Once we finish creating the interfaces and specifying all the methods we will add to our WCF Service, it's time to write the actual implementation of the Service.

using System;
using System.IO;
using System.ServiceModel.Web;

namespace WCFOverHttps.WCFHost
    public class WcfService : IWcfHttpService, IWcfSoapService
        public string HelloWorld()
            return "Hello World";

        public Stream GetFile(string path)
            path = string.Format("c:\\{0}", path);
                if (!File.Exists(path))
                    throw new Exception("File not found");

                if (WebOperationContext.Current != null)
                    WebOperationContext.Current.OutgoingResponse.ContentType = 

                return File.OpenRead(path);
            catch (Exception ex)
                if (WebOperationContext.Current != null)

                return null;

The final step in finishing the WCF Service is the ServiceModel configuration, so just add an Application Configuration File using Add Item, and under configuration section, we will add the system.ServiceModel section where we will configure all endpoints and security settings.

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
        <binding name ="soapBinding">
          <security mode="Transport">
            <transport clientCredentialType="None"/>
        <binding name="httpBinding">
          <security mode="Transport">
            <transport clientCredentialType="None"/>
      <service name="WCFOverHttps.WCFHost.WcfService"

            <add baseAddress=""/>
        <endpoint address=""



                  contract="WCFOverHttps.WCFHost.IWcfSoapService" />
        <endpoint address=""




        <endpoint address="mex"


                contract="IMetadataExchange" />
        <behavior name="WebBehavior">
          <webHttp />
        <behavior name="ServiceBehaviour">
          <serviceDebug includeExceptionDetailInFaults="False"/>
          <serviceMetadata httpsGetEnabled="true"/>

You can customize in this config section the URL of the WCF Service and also the URL of each endpoint separately. The HTTPS configuration is found in the binding configuration: <security mode="Transport">.

Now the Window Service is finished, and we can install it using the command InstallUtil32(64).exe WCFOverHttps.exe, but if you test it now, it won't work because there is no certificate installed and authorized for the WCF Server. So let's install a certification authority and then use it to create and sign a server certificate:

  1. Open the Visual Studio Command Prompt.
  2. Create the certificate authority:
  3. makecert.exe -sv SignRoot.pvk -cy authority -r 
      signroot.cer -a sha1 -n "CN=AuthorityName" 
      -ss my -sr localmachine
  4. Create the server certificate:
  5. makecert.exe -iv SignRoot.pvk -ic signroot.cer -cy 
      end -pe -n CN="localhost" -eku -ss 
      my -sr localmachine -sky exchange 
      -sp "Microsoft RSA SChannel Cryptographic Provider" -sy 12

The certificate is now installed, but we need to map it to the port specified in the WCF Service configuration file (here, we have port 81). We can do this by using the attached HttpConfig utility. Open it and go to the SSL tab, click Add, enter the IP address of your WCF Service, the port, the GUID of the Windows Service found in AssemblyInfo, and then Browse for the certificate you just created. Click OK and then Apply, and your WCF Service should work now over secure HTTPS.

Future goal

Find a way of self hosting an HTTPS WCF Service with an embedded certificate so it can be easily moved to any server. Any suggestions really appreciated.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Cotofana Radu
Software Developer Xrio
Romania Romania
No Biography provided

You may also be interested in...


Comments and Discussions

Questionmakecert Pin
Erika Chinchio21-Mar-14 2:37
memberErika Chinchio21-Mar-14 2:37 
QuestionHttpConfig utility - all magic is in this util, may I ask for elaboration on this please? Pin
devvvy26-Jan-13 17:20
memberdevvvy26-Jan-13 17:20 
QuestionSelf-hosting with WCFStormHost Pin
pogiman25-Jan-12 15:56
memberpogiman25-Jan-12 15:56 
GeneralMy vote of 1 Pin
Gerardo Laguzzi18-Jul-11 5:03
memberGerardo Laguzzi18-Jul-11 5:03 
QuestionEndpoint not found. Pin
jeff00seattle29-Jun-11 14:01
memberjeff00seattle29-Jun-11 14:01 
SuggestionNote... Pin
jeff00seattle29-Jun-11 13:03
memberjeff00seattle29-Jun-11 13:03 
QuestionVery nice - could you add the source for the HttpConfig utility? Pin
redWingBB16-Dec-10 4:34
memberredWingBB16-Dec-10 4:34 
Very nice - could you add the source for the HttpConfig utility?
Thats a nice little app in itself.
Bill in PA/USA

AnswerRe: Very nice - could you add the source for the HttpConfig utility? Pin
Cotofana Radu19-Dec-10 22:08
memberCotofana Radu19-Dec-10 22:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05-2016 | 2.8.180920.1 | Last Updated 27 Nov 2010
Article Copyright 2010 by Cotofana Radu
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid