Skip to content

Commit c866e8e

Browse files
jiayi11MichaelSimons
authored andcommitted
Add WCF sample (#136)
* add wcf sample * multi-stage build dockerfiles * update readme * update readme * small fixes * fixes according to comments * add docker compose files * small fixes according to comment
1 parent 37b3058 commit c866e8e

28 files changed

+913
-2
lines changed

manifest.samples.json

+62
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,68 @@
6666
}
6767
}
6868
]
69+
},
70+
{
71+
"sharedTags": {
72+
"wcfservice":{}
73+
},
74+
"platforms": [
75+
{
76+
"dockerfile": "samples/wcfapp/Dockerfile.web",
77+
"os": "windows",
78+
"osVersion": "ltsc2016",
79+
"tags": {
80+
"wcfservice-windowsservercore-ltsc2016": {}
81+
}
82+
},
83+
{
84+
"dockerfile": "samples/wcfapp/Dockerfile.web",
85+
"os": "windows",
86+
"osVersion": "1709",
87+
"tags": {
88+
"wcfservice-windowsservercore-1709": {}
89+
}
90+
},
91+
{
92+
"dockerfile": "samples/wcfapp/Dockerfile.web",
93+
"os": "windows",
94+
"osVersion": "1803",
95+
"tags": {
96+
"wcfservice-windowsservercore-1803": {}
97+
}
98+
}
99+
]
100+
},
101+
{
102+
"sharedTags": {
103+
"wcfclient":{}
104+
},
105+
"platforms": [
106+
{
107+
"dockerfile": "samples/wcfapp/Dockerfile.client",
108+
"os": "windows",
109+
"osVersion": "ltsc2016",
110+
"tags": {
111+
"wcfclient-windowsservercore-ltsc2016": {}
112+
}
113+
},
114+
{
115+
"dockerfile": "samples/wcfapp/Dockerfile.client",
116+
"os": "windows",
117+
"osVersion": "1709",
118+
"tags": {
119+
"wcfclient-windowsservercore-1709": {}
120+
}
121+
},
122+
{
123+
"dockerfile": "samples/wcfapp/Dockerfile.client",
124+
"os": "windows",
125+
"osVersion": "1803",
126+
"tags": {
127+
"wcfclient-windowsservercore-1803": {}
128+
}
129+
}
130+
]
69131
}
70132
]
71133
}

samples/README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
The samples show various ways to use .NET Framework and Docker together. You can use the samples as the basis of your own Docker images or just to play.
44

5-
The samples exercise various levels of functionality. The [.NET Framework Docker sample](dotnetapp/README.md) includes the most functionality, including build, unit testing, and pushing images to a container registry. The [ASP.NET Docker sample](aspnetapp/README.md) includes instructions for testing images with [Azure Container Instances](https://azure.microsoft.com/services/container-instances/). The samples include detailed instructions for use with and without Docker.
5+
The samples exercise various levels of functionality. The [.NET Framework Docker sample](dotnetapp/README.md) includes the most functionality, including build, unit testing, and pushing images to a container registry. The [ASP.NET Docker sample](aspnetapp/README.md) includes instructions for testing images with [Azure Container Instances](https://azure.microsoft.com/services/container-instances/). The samples include detailed instructions for use with and without Docker. The [WCF Docker sample](wcfapp/README.md) includes instructions for dockerizing WCF services, either IIS-hosted or self-hosted, and how to run client app against them.
66

77
## Try a pre-built .NET Framework Docker Image
88

@@ -19,6 +19,7 @@ docker run --rm microsoft/dotnet-framework-samples
1919
* [.NET Framework Console Docker Sample](dotnetapp/README.md) - This [sample](dotnetapp/Dockerfile) builds, tests, and runs the sample. It includes and builds multiple projects.
2020
* [ASP.NET Web Forms Docker Sample](aspnetapp/README.md) - This [sample](aspnetapp/Dockerfile) demonstrates using Docker with an ASP.NET Web Forms app.
2121
* [ASP.NET MVC Docker Sample](aspnetmvcapp/README.md) - This [sample](aspnetmvcapp/Dockerfile) demonstrates using Docker with an ASP.NET MVC app.
22+
* [WCF Docker Sample](wcfapp/README.md) - This [sample](wcfapp/) demonstrates using Docker with a WCF app.
2223

2324
## Push Images to a Container Registry
2425

@@ -36,6 +37,7 @@ Docs and More Information:
3637

3738
* [.NET Docs](https://docs.microsoft.com/dotnet/)
3839
* [ASP.NET Docs](https://docs.microsoft.com/aspnet/)
40+
* [WCF Docs](https://docs.microsoft.com/dotnet/framework/wcf/)
3941
* [dotnet/core](https://github.com/dotnet/core) for starting with .NET Core on GitHub.
4042
* [dotnet/announcements](https://github.com/dotnet/announcements/issues) for .NET announcements.
4143

@@ -52,4 +54,5 @@ Docs and More Information:
5254

5355
* [microsoft/aspnet](https://hub.docker.com/r/microsoft/aspnet/) for ASP.NET Web Forms and MVC images.
5456
* [microsoft/dotnet-framework](https://hub.docker.com/r/microsoft/dotnet-framework/) for .NET Framework images.
55-
* [microsoft/dotnet-framework-samples](https://hub.docker.com/r/microsoft/dotnet-framework-samples/) for .NET Framework and ASP.NET sample images.
57+
* [microsoft/dotnet-framework-samples](https://hub.docker.com/r/microsoft/dotnet-framework-samples/) for .NET Framework and ASP.NET sample images.
58+
* [microsoft/wcf](https://hub.docker.com/r/microsoft/wcf) for WCF images.

samples/wcfapp/Dockerfile.client

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM microsoft/dotnet-framework:4.7.2-sdk AS build
2+
WORKDIR /app
3+
COPY WcfClient/. .
4+
RUN msbuild /p:Configuration=Release
5+
6+
FROM microsoft/dotnet-framework:4.7.2-runtime AS runtime
7+
WORKDIR /app
8+
COPY --from=build /app/bin/Release .
9+
ENTRYPOINT WcfClient.exe

samples/wcfapp/Dockerfile.console

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM microsoft/dotnet-framework:4.7.2-sdk AS build
2+
WORKDIR /app
3+
COPY WcfServiceConsoleApp/. .
4+
RUN msbuild /p:Configuration=Release
5+
6+
FROM microsoft/dotnet-framework:4.7.2-runtime AS runtime
7+
EXPOSE 80
8+
EXPOSE 808
9+
WORKDIR /app
10+
COPY --from=build /app/bin/Release .
11+
ENTRYPOINT WcfServiceConsoleApp.exe

samples/wcfapp/Dockerfile.web

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM microsoft/dotnet-framework:4.7.2-sdk AS build
2+
WORKDIR /app
3+
COPY WcfServiceWebApp/. .
4+
RUN msbuild /p:Configuration=Release
5+
6+
FROM microsoft/wcf:4.7.2 AS runtime
7+
WORKDIR /inetpub/wwwroot
8+
COPY --from=build /app/ .

samples/wcfapp/README.md

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# WCF Docker Sample
2+
This sample demonstrates how to dockerize WCF services, either IIS-hosted or self-hosted. A simple "hello world" service contract is used in all samples for both HTTP and NET.TCP transport bindings. The sample can also be used without Docker.
3+
4+
The sample builds the application in a container based on the larger [.NET Framework SDK Docker image](https://hub.docker.com/r/microsoft/dotnet-framework-build/). It builds the application and then copies the final build result into a Docker image based on the smaller [WCF Runtime Docker image](https://hub.docker.com/r/microsoft/wcf/) or [.NET Framework Runtime Docker image](https://hub.docker.com/r/microsoft/dotnet-framework/). It uses Docker [multi-stage build](https://github.com/dotnet/announcements/issues/18) and [multi-arch tags](https://github.com/dotnet/announcements/issues/14).
5+
6+
This sample requires [Docker 17.06](https://docs.docker.com/release-notes/docker-ce) or later of the [Docker client](https://store.docker.com/editions/community/docker-ce-desktop-windows).
7+
8+
## Try pre-built WCF Docker Images
9+
You can quickly run a container with a pre-built [sample WCF Docker image](https://hub.docker.com/r/microsoft/dotnet-framework-samples/), based on the WCF Docker sample.
10+
11+
Type the following [Docker](https://www.docker.com/products/docker) command to start a WCF service container:
12+
```console
13+
docker run --name wcfservicesample --rm -it microsoft/dotnet-framework-samples:wcfservice
14+
```
15+
Find the IP address of the container instance.
16+
```console
17+
docker inspect --format="{{.NetworkSettings.Networks.nat.IPAddress}}" wcfservicesample
18+
172.26.236.119
19+
```
20+
Type the following Docker command to start a WCF client container, set environment variable HOST to the IP address of the wcfservicesample container:
21+
```console
22+
docker run --name wcfclientsample --rm -it -e HOST=172.26.236.119 microsoft/dotnet-framework-samples:wcfclient
23+
```
24+
25+
## Getting the sample
26+
27+
The easiest way to get the sample is by cloning the samples repository with [git](https://git-scm.com/downloads), using the following instructions.
28+
29+
```console
30+
git clone https://github.com/microsoft/dotnet-framework-docker/
31+
```
32+
33+
You can also [download the repository as a zip](https://github.com/microsoft/dotnet-framework-docker/archive/master.zip).
34+
35+
## Build and run the sample with Docker
36+
WCF service is supported on .NET Framework, which can run in Windows Server Core based containers. For simplicity, we disabled security in these samples.
37+
38+
### Build a Container with IIS-hosted WCF Service
39+
Project `WcfServiceWebApp` is created from 'WCF Service Application' template in Visual Studio. A [Dockerfile](/Dockerfile.web) is added to the project. We use `microsoft/wcf` image as the base image, which has both HTTP and NET.TCP protocols enabled in IIS and exposes ports 80 (for HTTP) and 808 (for NET.TCP) for the container. We use WCF image with tag `4.7.2` for .NET Framework 4.7.2 in this example, but you can change it to use other tags (eg. `microsoft/wcf:4.7` with tag `4.7`) for WCF images with different versions of .NET Framework. The complete list of [WCF image tags](https://hub.docker.com/r/microsoft/wcf/tags/) can be found from Docker Hub.
40+
41+
Run commands below to build the container image with name `iishostedwcfservice` and start an instance of it named `myservice1`. Docker parameter `-d` will run the container in background (detached mode).
42+
```
43+
cd samples
44+
cd wcfapp
45+
docker build --pull -t iishostedwcfservice -f Dockerfile.web .
46+
docker run -d --rm --name myservice1 iishostedwcfservice
47+
```
48+
Find the IP address of the container instance. This will be used later for a WCF client to connect to the service.
49+
```
50+
docker inspect --format="{{.NetworkSettings.Networks.nat.IPAddress}}" myservice1
51+
172.23.70.146
52+
```
53+
### Build a Container with Self-hosted WCF Service
54+
Project `WcfServiceConsoleApp` is created from Windows Classic Desktop 'Console App' template in Visual Studio. We added a [Dockerfile](/Dockerfile.console) to the project. We use `microsoft/dotnet-framework` image as the base image and expose ports 80 (for HTTP) and 808 (for NET.TCP) for the container.
55+
56+
Run commands below to build the container image with name `selfhostedwcfservice` and start an instance of it named `myservice2`.
57+
```
58+
docker build --pull -t selfhostedwcfservice -f Dockerfile.console .
59+
docker run -it --rm --name myservice2 selfhostedwcfservice
60+
```
61+
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 --rm --name myservice2 selfhostedwcfservice`).
62+
```
63+
docker inspect --format="{{.NetworkSettings.Networks.nat.IPAddress}}" myservice2
64+
172.23.69.75
65+
```
66+
### Build a Container to run WCF Client against the Service
67+
Now that we have WCF services running in containers. Let's run the WCF client against them. Run commands below to build the container image with name `wcfclient`. The IP address of the WCF service is passed through the environment variable `host`.
68+
```
69+
docker build --pull -t wcfclient -f Dockerfile.client .
70+
docker run -it --rm -e HOST=172.23.70.146 --name myclient wcfclient
71+
Client OS: Microsoft Windows NT 6.2.9200.0
72+
Service Host: 172.23.70.146
73+
Hello WCF via Http from Container!
74+
Hello WCF via Net.Tcp from Container!
75+
```
76+
The result above is from running the WCF client against the IIS-hosted WCF service. Changing the environment variable `host` to the IP address of the self-hosted WCF service, you will get a similar result.
77+
78+
*To run WCF client on a different machine other than the one that hosts container instance*, we will need to map ports of a container to ports of its host by using Docker parameter `-p` when we start a constainer instance. For example, the command below started an instance of the self-hosted WCF service container named `myservice3` with its ports 80 and 808 mapped to ports 80 and 808 of its host machine respectively.
79+
```
80+
C:\wcfapp\WcfServiceConsoleApp>docker run -d -p 80:80 -p 808:808 --name myservice3 selfhostedwcfservice
81+
```
82+
Then for the WCF client to connect to the service, we need to set the `host` to be the IP address (or DNS name) of the container host machine (instead of the IP address of the container instance). The rest will be the same to start the WCF client.
83+
84+
## Build and run the sample with Docker Compose
85+
[Docker Compose](https://docs.docker.com/compose/overview/) is a tool for defining and running multi-container Docker applications. In this sample, we also added YML files to configure WCF server and client applications. You can directly start and run service and client applications without having to do any work to hookup the client to the WCF service.
86+
87+
Type the following Docker Compose command to start both iis-hosted WCF service container and client container:
88+
```
89+
docker-compose -f docker-compose-iishosted.yml up
90+
```
91+
Alternatively, if you want to build and run self-hosted WCF service container, run commands below:
92+
```
93+
docker-compose -f docker-compose-selfhosted.yml up
94+
```
95+
96+
## Build the sample locally
97+
98+
You can build this [.NET Framework 4.7.2](https://www.microsoft.com/net/download/dotnet-framework-runtime/net472) application locally with MSBuild using the following instructions. The instructions assume that you are in the root of the repository and using the `Developer Command Prompt for VS 2017`.
99+
100+
You must have the [.NET Framework 4.7.2 targeting pack](https://go.microsoft.com/fwlink/?LinkId=863261) installed. It is easiest to install with [Visual Studio 2017](https://www.microsoft.com/net/download/Windows/build) with the Visual Studio Installer.
101+
102+
```console
103+
cd samples
104+
cd wcfapp
105+
msbuild wcfapp.sln /p:Configuration=Release
106+
```
107+
108+
Note: The /p:Configuration=Release argument builds the application in release mode (the default is debug mode). See the [MSBuild Command-Line reference](https://msdn.microsoft.com/en-us/library/ms164311.aspx) for more information on commandline parameters.
109+
You can also build, test and debug the application with [Visual Studio 2017](https://www.microsoft.com/net/download/Windows/build).
110+
111+
## .NET Resources
112+
113+
More Samples
114+
115+
* [.NET Framework Docker Samples](../README.md)
116+
* [.NET Core Docker Samples](https://github.com/dotnet/dotnet-docker/blob/master/samples/README.md)
117+
118+
Docs and More Information:
119+
120+
* [.NET Docs](https://docs.microsoft.com/dotnet/)
121+
* [WCF Docs](https://docs.microsoft.com/dotnet/framework/wcf/)
122+
* [ASP.NET Docs](https://docs.microsoft.com/aspnet/)
123+
* [dotnet/core](https://github.com/dotnet/core) for starting with .NET Core on GitHub.
124+
* [dotnet/announcements](https://github.com/dotnet/announcements/issues) for .NET announcements.
125+
126+
## Related Repositories
127+
128+
.NET Core Docker Hub repos:
129+
130+
* [microsoft/aspnetcore](https://hub.docker.com/r/microsoft/aspnetcore/) for ASP.NET Core images.
131+
* [microsoft/dotnet](https://hub.docker.com/r/microsoft/dotnet/) for .NET Core images.
132+
* [microsoft/dotnet-nightly](https://hub.docker.com/r/microsoft/dotnet-nightly/) for .NET Core preview images.
133+
* [microsoft/dotnet-samples](https://hub.docker.com/r/microsoft/dotnet-samples/) for .NET Core sample images.
134+
135+
.NET Framework Docker Hub repos:
136+
137+
* [microsoft/wcf](https://hub.docker.com/r/microsoft/wcf/) for WCF images.
138+
* [microsoft/aspnet](https://hub.docker.com/r/microsoft/aspnet/) for ASP.NET Web Forms and MVC images.
139+
* [microsoft/dotnet-framework](https://hub.docker.com/r/microsoft/dotnet-framework/) for .NET Framework images.
140+
* [microsoft/dotnet-framework-build](https://hub.docker.com/r/microsoft/dotnet-framework-build/) for building .NET Framework applications with Docker.
141+
* [microsoft/dotnet-framework-samples](https://hub.docker.com/r/microsoft/dotnet-framework-samples/) for .NET Framework and ASP.NET sample images.

samples/wcfapp/WcfClient/App.config

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/>
5+
</startup>
6+
</configuration>

samples/wcfapp/WcfClient/IService1.cs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.ServiceModel;
2+
3+
namespace WcfClient
4+
{
5+
[ServiceContract]
6+
public interface IService1
7+
{
8+
[OperationContract]
9+
string Hello(string name);
10+
}
11+
}

samples/wcfapp/WcfClient/Program.cs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.ServiceModel;
3+
4+
namespace WcfClient
5+
{
6+
class Program
7+
{
8+
static string host;
9+
10+
static void Main(string[] args)
11+
{
12+
Console.WriteLine("Client OS: {0}", Environment.OSVersion);
13+
host = Environment.GetEnvironmentVariable("host") ?? "localhost";
14+
Console.WriteLine("Service Host: {0}", host);
15+
16+
CallViaHttp();
17+
18+
CallViaNetTcp();
19+
}
20+
21+
static void CallViaHttp()
22+
{
23+
var address = string.Format("http://{0}/Service1.svc", host);
24+
var binding = new BasicHttpBinding();
25+
var factory = new ChannelFactory<IService1>(binding, address);
26+
var channel = factory.CreateChannel();
27+
28+
Console.WriteLine(channel.Hello("WCF via Http"));
29+
}
30+
31+
static void CallViaNetTcp()
32+
{
33+
var address = string.Format("net.tcp://{0}/Service1.svc", host);
34+
var binding = new NetTcpBinding(SecurityMode.None);
35+
var factory = new ChannelFactory<IService1>(binding, address);
36+
var channel = factory.CreateChannel();
37+
38+
Console.WriteLine(channel.Hello("WCF via Net.Tcp"));
39+
}
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("WcfClient")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("WcfClient")]
13+
[assembly: AssemblyCopyright("Copyright © 2017")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("ebd37df1-f83c-40c1-ba61-1f6d652a1f60")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

0 commit comments

Comments
 (0)