Let’s Attempt WCF Self-Hosted Providers in a Container



Microservices are a scorching structure idea proper now, and we hear numerous curiosity within the structure ideas.  Lots of the concepts and capabilities behind microservices are already doable with the WCF frameworks for shopper and server creation.  There may be one tenet about microservices that you may not do with WCF: run inside a container.  Till now…   On this article, we’re going to check out operating a easy WCF self-hosted service inside a Docker container. The structure that we’re going to assemble on this article is sort of easy:  our self-hosted HTTP service will run inside a Docker container on our workstation and we can use a shopper on the identical machine to attach by the container and work together with the service.  The next diagram illustrates the specified configuration:

Architecture of our sample
Structure of our pattern

WCF Providers Run on Home windows

WCF runs on the .NET framework, which signifies that it solely runs on Home windows.  Thankfully, we have now Home windows Server Core photographs out there to us.  If we

set up Docker for Home windows on Home windows 10 or Home windows Server 2016 with container assist enabled, we are able to configure Docker to make use of Home windows containers.  Proper click on on the Docker whale icon within the system tray and you may select to “Switch to Windows Containers” to get began:

Switch to Windows Containers
Change to Home windows Containers

Lets open a command immediate and seize a replica of the Home windows Server Core docker picture to get began:

docker pull microsoft/windowsservercore

We are going to construct our minimal set of WCF necessities on prime of this picture.

Our WCF Pattern Code

Let’s construct a easy service so as to add to a container after which a shopper to devour the service from the container.  I’ll begin with a service library mission that’s nearly similar to the default mission template for a WCF Service Library.  I’ll merely change the GetDataUsingContract methodology to interpret the StringValue with a barely totally different string:

We will then add a easy Home windows Console utility to the answer that can function the host of our service.  The Program class is easy and references the Service1 sort from our ServiceLibrary mission:

The app.config for this mission must include some details about the deal with, endpoint, and behaviors of our service.  Lets fill that out with some easy MetadataExchange and BasicHttpBinding:

Lastly, we want a shopper that can hook up with the service and work together with the IService1 service contract.  That is one other Home windows Console utility.  This time we add a service reference to the mission and level to the MyServiceLibrary mission.  My answer appears like the next:

My Solution Explorer
My Resolution Explorer

After I add the service, I proper click on on the WCFClientBasic mission and select “Add  – Service Reference” which brings up the next dialogue:

Add Service Reference
Add Service Reference

I clicked “Discover” and situated the entry from MyServiceLibrary so as to add to the WCFClientBasic mission.  With that reference added, I can write some easy shopper code to connect with the service and show some fundamental information interactions:

With that code in place, that merely creates a shopper connection to the Service and executes the GetData and GetDataUsingDataContract, we are able to run this pattern shopper code within the Visible Studio debugger and confirm that it really works.

Deploying the WCF Host to Docker

I’ll begin by making a publish folder on disk inside my answer. Subsequent, I’ll take the easy host utility and duplicate the EXEs, DLLs, and config information right into a folder named bin throughout the publish folder.  I’ll then create a Dockerfile on the root of publish with this content material:

This file directs Docker so as to add the WCF-HTTP Activation characteristic to Home windows Server Core, after which copy our host content material to the c:app folder.  It exposes port 83, as was configured in our host’s app.config file earlier.  Lastly, it launches the BasicHttpHost utility.  We will construct this Dockerfile right into a Docker picture by executing the docker construct command like so:

docker construct -t wcfhost:newest -t wcfhost:1 .

This creates a picture known as “wcfhost” with a model of 1 that can be tagged as the most recent model.  This construct step will take a couple of minutes for Home windows to configure the WCF options contained in the container.  As soon as full, you possibly can launch an occasion of your host with this command:

docker run -itd --name host wcfhost

We will then examine the container to find out the IP deal with of our service.  We will use a format string to seize the IP deal with immediately from the container info:

docker examine -f="{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" host

Copy that IP deal with into the app.config of the WCFClientBasic mission and replace the endpoint deal with from localhost:83 to your ip deal with (mine was  It’s best to then be capable to run your WCFClientBasic and have it connect to the container, returning the anticipated textual content ends in a console window:

Results from Docker
Outcomes from Docker


We’ve proven how WCF companies will be configured utilizing the usual WindowsServerCore container picture.  There are different Home windows choices and options that may be configured on this container, based mostly in your service’s wants. The container structure permits us to scale our service simply in addition to deploy the identical binaries and configuration that we run in improvement and staging right into a manufacturing atmosphere.

Is that this one thing that you just may use along with your current WCF companies?

The WCF crew is engaged on simplifying Docker integration with service creation and debugging.  We wish to hear from you about how you employ WCF companies and what you need in a containerized WCF answer.  Tell us your ideas within the remark space beneath and we’ll comply with up as we work to carry container assist to WCF.

Leave a Reply

Your email address will not be published. Required fields are marked *