.NET Development Examined

Deploying a WCF Service

Friday, December 12, 2014

In our last three part series (Part One, Part Two, Part Three) we learned how to create a WCF service, using the COODE Framework to simplify the setup and configuration that is normally associated with a WCF service. We also saw how the CODE Framework makes it easy to stand up our service endpoints during development.

This great for development, but at some point we will need to deploy our services to productions. Part of the flexibility of WCF services is the various methods it can be hosted. You can deploy your WCF service as a Windows Service, or it can be deployed to IIS, or it can be Self-Hosted (inside your application). If you are using the CODE Framework there is a final option, you can expose your WCF service as a Web API layer.

When deploying a SOA based application (as created in these exercises) you have three distinct parts to deploy: SQL Database, Services, and UI. The process is for the first two is the same, but the UI deployment varies based on the type of UI (i.e. WPF Application, Website, IOS, Android). This is one of the benefits to the SOA architecture, and specifically the CODE Framework based service endpoints, in that multiple types of UIs can utilize a common set of service endpoints

When considering your SOA architecture each part of the deployment can be made to a separate machine (i.e. SQL, Services, and Website) or they can be deployed to the same machine.

Task 1 - Create CODE Framework Windows Service Project

  1. Add new Project to solution
  2. For the template select a CODE Framework Windows Service Host Project
  3. Enter an appropriate name for the project
  4. After you select OK, you will be presented options
  5. The wizard will examine your project for a service interface and if found will present you with options for what types of endpoints to expose. We recommend to leave all options checked, you will see later how to modify these settings after the project is created. Enter an appropriate display name for your services and select OK
  6. Examine your Solution Explorer and expand the newly created project. Delete the two highlighted placeholder references, then right click on the WindowsServiceHost.cs file and select View Code
  7. In this code you will see how the different service endpoints are initialized. You can comment out any endpoints you do not anticipate using
    protected override void OnStart(string[] args)
       //ServiceGarden.TryAddServiceHostBasicHttp(typeof(CFBootCampService), true);
       //ServiceGarden.TryAddServiceHostWsHttp(typeof(CFBootCampService), true);
  8. Add a reference to Entity Framework to the project. From the top Visual Studio menu select Tools/Nuget Package Manager/Manage Nuget Packages for Solution. Make sure the Installed packages is selected on the left side. Highlight EntityFramework and click the Manage button. Check the newly created Windows Service Host project and select OK.
  9. Update the App.config with an EF connection string. Open the configuration file and add a copy of the connection string from your Development Host project. It should look similar to the following, but will vary depending on how you are accessing the SQL database. Note that you will need to change the database name and credentials to match that of the Server this will be installed on. You will also want to use the SQL credentials that we create in the next task.
       <add name="{yourEntities}" connectionString="provider=System.Data.SqlClient;provider connection string="data source={your database name}; initial catalog={db name}; persist security info=True;user id={db user}; password={password}; MultipleActiveResultSets=True; App=EntityFramework"" 
       providerName="System.Data.EntityClient" />
  10. 10. Build the project.

Task 2 – Configure SQL Server Credentials

  1. Using SQL Management Studio, connect to the database that will be used for this deployment.
  2. Expand the Security/Logins tree, right click and select New Login. Enter a Login Name, select SQL Server authentication and enter a password. Make sure to uncheck the Enforce password expiration and User must change password at next login.
  3. (Optional) If using EF CODE First the user must be able to create a database. Click on the Server Roles and check dbcreator role. Depending on your security you may have to check other roles.
  4. Be sure to update the Windows Service project App.config with these user credentials. When this is deployed it will have a name of project name.WindowsServiceHost.config

Task 3 – Configure Windows Server for WCF Endpoints

  1. On the Windows server that will host the service, open the Add/Remove Features of Windows. On the Features portions ensure that both .NET Framework options are enabled. Expand .NET Framework 4.5, then expand the WCF Services node and check the three Activation options.

Task 4 - Deploy CODE Framework Windows Service Project

  1. In Visual Studio look in Solution Explorer for your Windows Service project. Right click on the project and select Open Folder in File Explorer. Expand the \bin\debug (or release) folder. This folder contains the code necessary to run your services. Copy this code to the Windows Server that will host your services. It is recommended that you copy the files to a folder with the name and version of your service deployment. For example for the first version of the CFBootCamp.WindowsService.exe, create a CFBootCampV01 folder and copy the source files into it. Additionally create a folder that will hold the currently installed service code. For the first deployment copy the code to both location.
  2. On the Windows Server, open a Command Prompt with Administrator privileges. Press the Windows key, then type in Command – right click on the Command Prompt application and select Run as administrator
    There are several different ways to install services on a Windows server. You can use an installer program to create a deployment setup, or from a command line. It is recommended that you create a batch file to automate deployment and updating.
  3. At the prompt of the Command Windows, type
    sc create CFBootCamp binpath= “C:\Services\CFBootCamp\Installed\CFBootCamp.WindowsService.exe”
    Where CFBootCamp is the name you wish to show in the Windows Service manager, and the binpath points to your code.
  4. Adjust the service settings. Open the Windows Service manager by pressing the Windows key and type in Service – select the Service Manager application. Locate the name of your service and double click on it. Set the Startup type to Automatic. While on this screen click the Start button to start the services now.
  5. Verify the service is running. Press the Windows key and type in Event – select the Event Viewer application. Expand the Windows Logs\Application tree. You should see one or more lines with a Source listed as your service. One of them should say “Service started successfully”. Additionally you should see a line for each service endpoint, indicating the exposed URL. The TCP endpoint is shown here.

Task 5 – Configure IIS

  1. To run ASP.NET MVC applications on IIS you must activate the .NET features. To enable these features, on the Windows server that will host the service, open the Add/Remove Features of Windows. On the Roles item, ensure the ASP.NET features are enabled. If you don’t see the WebDAV publishing listed and wish to publish your Website using WebDAV then you need to get it installed now by using the Microsoft Web Platform Installer (look for Web Deploy for Hosting Servers)
  2. Create your website in IIS. On the Windows Server press the Windows key and type in IIS – select the Internet Information Services Manager application. Expand the Server\Sites folder. Exactly where you create your website will depend on if this server will host one or more website. You may create a website that replaces the Default Web Site, or you may create one or more under the Default Web Site and control access by routing. Based on your needs, create your website and highlight it. Be sure to note the exact path to your website, it will be used when publishing from Visual Studio.

Task 6 – Publish ASP.NET MVC Website

  1. Make sure your web.config has the correct service endpoint settings for the deployed services (these can be verified from Task 4, Step 5 above). If you are installing Serivces and Website on the same machine then TCP is the recommended protocol. This setting is something like this:
    <add key="ServiceProtocol" value="NetTcp" />
    <add key="ServicePort:I{ServiceName}Service" value="50000" />
    For services using HTTP it may look like this:
    <add key="ServiceProtocol" value="BasicHTTP" />
    <add key="ServiceUrl: I{ServiceName}Service"   value="http://www.goo.com/{Name}Service.svc" />
  2. Right click on your MVC project and select Publish. Create a new Profile and go to the Connection portion of the wizard. The Publish Method has several different options, depending on your exact system setup you may choose different ones, but if you have the Web Deploy installed on your server it is recommended to us this method.
  3. Enter the information for the targeted web server. Depending on your network you may have to use the IP address instead of the machine name.
  4. For the Site name, be sure to enter the web site name created when configuring IIS. This may mean that it looks like Default Web Site\CFBootCamp – spaces in the site name are valid.
  5. The User name / Password must be valid on the Web Server, and it must have sufficient privileges to perform the installation.
  6. The destination URL is not overly critical, it is used to automatically navigate to the website once the publish operation is complete.
  7. Click the Validate Connection button. If it does not succeed then the publish operation will fail.
  8. Optionally adjust setting for the published site.
  9. Publish the site. If all goes well when the publish operation is completed it will open up a browser showing your site!


  1. If for some reason the install fails the service may be listed, but not show up in the uninstall list. You can use the command line sc delete “service name” to remove the service. This does require a re-boot to clear it from the service list.
  2. To add/remove endpoints (i.e. Http, JSON, etc.) on the Windows Service Host project – right click on the WindowsServiceHost.cs file and select “view code”. The OnStart method add endpoints to the ServiceGarden
  3. Make sure that the Windows Service Host project’s App.config has the proper connection strings for the target installation machine. If for some reason the project is deployed and you need to adjust the settings you can open the “name.Services.WindowsServiceHost.exe.config” file located in the installation folder on the target machine. Note that changing the config file once deployed does require you to stop/restart the service


Add Comment