Demonstrate how to run WCF service as well as .NET Core WCF client in containers
This is the OS and software environment required to complete this sample.
- Windows 10 (or Windows Server 2016) with 'Containers' feature enabled.
- Docker for Windows
- Visual Studio 2017 with .NET Core SDK 2.0 or later installed
WCF service is supported on .NET Framework, which can only run in Windows cotnainers. Right click the docker icon and select "Switch to Windows containers..."
Build container image of WcfServiceWebApp
and run it. Parameter -d will run the container in detached mode.
C:\WcfContainerExamples\WcfServiceWebApp> docker build -t wcfservice:iis-hosted .
C:\WcfContainerExamples\WcfServiceWebApp> docker run -d --name service wcfservice:iis-hosted
Find the IP address of the container instance. This will be used later for a WCF client to connect to the service.
C:\WcfContainerExamples\WcfServiceWebApp> docker inspect --format="{{.NetworkSettings.Networks.nat.IPAddress}}" service
172.23.70.146
Buld project WcfServiceConsoleApp
in either Visual Stuio or command line first. Then build container image of WcfServiceConsoleApp
and run it.
C:\WcfContainerExamples\WcfServiceConsoleApp> docker build -t wcfservice:self-hosted .
C:\WcfContainerExamples\WcfServiceConsoleApp> docker run -it --rm --name myservice wcfservice:self-hosted
The service is ready at http://localhost/Service1.svc
The service is ready at net.tcp://localhost/Service1.svc
The service is running...
Open another console window to find the IP address of the self-hosted WCF service (alternatively, you can run the self-hosted WCF service container in detached mode by docker run -d --name myservice wcfservice:self-hosted
).
C:\WcfContainerExamples\WcfClientNetCore> docker inspect --format="{{.NetworkSettings.Networks.nat.IPAddress}}" myservice
172.23.69.75
The WCF client project, WcfClientNetCore
, is a .NET Core 2.0 application. First, let's run it locally to make sure everything works. The IP address of service is passed through the environment variable host
.
C:\WcfContainerExamples\WcfClientNetCore> set host=172.23.70.146
C:\WcfContainerExamples\WcfClientNetCore> dotnet build
C:\WcfContainerExamples\WcfClientNetCore> dotnet run
Client OS: Microsoft Windows NT 6.2.9200.0
Service Host: 172.23.70.146
Hello WCF via Http from Container!
Hello WCF via Net.Tcp from Container!
Now, let's build the Windows container image of WcfClientNetCore
and run it. The host IP address is passed into the container through --env
parameter. Parameter --rm
will clean up the container after it's done.
C:\WcfContainerExamples\WcfClientNetCore> docker build -t wcfclient .
C:\WcfContainerExamples\WcfClientNetCore> docker run --env host=172.23.70.146 --rm wcfclient
Client OS: Microsoft Windows NT 10.0.14393.0
Service Host: 172.23.70.146
Hello WCF via Http from Container!
Hello WCF via Net.Tcp from Container!
As .NET Core is cross-platform, we can also build a Linux container image of WcfClientNetCore
and run it. To do this, first, right click the docker icon and select "Switch to Linux containers..." and then run the same commands again, except this time you will notice the client OS becomes Unix as below.
C:\WcfContainerExamples\WcfClientNetCore> docker build -t wcfclient .
C:\WcfContainerExamples\WcfClientNetCore> docker run --env host=172.23.70.146 --rm wcfclient
Client OS: Unix 4.9.49.0
Service Host: 172.23.70.146
Hello WCF via Http from Container!
Hello WCF via Net.Tcp from Container!