简体   繁体   English

无法连接到Docker容器中托管的WCF服务

[英]Cannot connect to WCF service hosted in Docker container

I'm trying to run an application that exposes a WCF service in a Docker container. 我正在尝试运行一个在Docker容器中公开WCF服务的应用程序。

The application is built using TopShelf so it can run either as Windows Service or (in development) as stand-alone console program. 该应用程序是使用TopShelf构建的,因此可以作为Windows服务运行,也可以作为独立的控制台程序运行(开发中)。

The problem I'm experiencing is that the WCF Test Client can't get the metadata. 我遇到的问题是WCF测试客户端无法获取元数据。 It's worth saying that the application works correctly when executed locally. 值得一提的是,该应用程序在本地执行时可以正常工作。

Here is the metadata file generated by the WCF Test Client when the application is running locally 这是WCF测试客户端在本地运行应用程序时生成的元数据文件

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.serviceModel>
        <bindings>
            <netTcpBinding>
                <binding name="NetTcpBinding_ITestWcfDocker" sendTimeout="00:05:00">
                    <security mode="None" />
                </binding>
            </netTcpBinding>
        </bindings>
        <client>
            <endpoint address="net.tcp://localhost:9998/" binding="netTcpBinding"
                bindingConfiguration="NetTcpBinding_ITestWcfDocker" contract="ITestWcfDocker"
                name="NetTcpBinding_ITestWcfDocker" />
        </client>
    </system.serviceModel>
</configuration>

This is the Dockerfile I'm using the build the container 这是我正在使用的Dockerfile构建容器

FROM microsoft/dotnet-framework:4.6.2
RUN powershell -Command Add-WindowsFeature NET-WCF-HTTP-Activation45
WORKDIR app
COPY bin/Release/net461 .
EXPOSE 9998 9999
ENTRYPOINT ["EMG.Service.TestWcfDocker.exe"]

After building the project, I build the container with this command 构建项目后,我使用此命令构建容器

docker build -t emg/test-wcf:latest .

Then I launch a container with this command 然后我用这个命令启动一个容器

docker run -p 9998:9998 -p 9999:9999 -t emg/test-wcf

This is the output I get on the console log 这是我在控制台日志上得到的输出

Configuration Result:
[Success] Name EMG.TestWcfDocker, [Success] DisplayName EMG TestWcfDocker, [Success] Description EMG TestWcfDocker, [Success] ServiceName EMG.TestWcfDocker
Topshelf v4.0.0.0, .NET Framework v4.0.30319.42000
2017-05-15 15:46:48.0700|DEBUG|WcfServiceHost|Adding NetTcpBinding endpoint for ITestWcfDocker at net.tcp://localhost:9998/
2017-05-15 15:46:48.1012|DEBUG|WcfServiceHost|Adding Metadata endpoint at http://localhost:9999/
2017-05-15 15:46:49.2207|INFO|EMG.TestWcfDocker.Program|Starting EMG.TestWcfDocker
The EMG.TestWcfDocker service is now running, press Control+C to exit.
2017-05-15 15:46:50.2675|INFO|EMG.TestWcfDocker.Program|EMG.TestWcfDocker started

Once the container is started, I use this to get the IP of the container 容器启动后,我将使用它来获取容器的IP

docker inspect -f="{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" container-name

I then use the returned IP to connect to the service via the WCF Test Client, but I receive this error message 然后,我使用返回的IP通过WCF测试客户端连接到服务,但是我收到此错误消息

Error: Cannot obtain Metadata from http://172.19.111.72:9999/ If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address.  For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error    URI: http://172.19.111.72:9999/    Metadata contains a reference that cannot be resolved: 'http://172.19.111.72:9999/'.    There was no endpoint listening at http://172.19.111.72:9999/ that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.    Unable to connect to the remote server    A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 172.19.111.72:9999HTTP GET Error    URI: http://172.19.111.72:9999/    There was an error downloading 'http://172.19.111.72:9999/'.    Unable to connect to the remote server    A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 172.19.111.72:9999

If I use localhost, I get a similar error. 如果使用本地主机,则会收到类似的错误。

tl;dr: tl; dr:

I have a WCF service in a Docker container, I cannot connect to it from the host machine. 我在Docker容器中有WCF服务,但无法从主机连接到它。

So you going to need to do this for TCP hosting support: 因此,您需要为TCP托管支持执行此操作:

SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]

Install Windows components required for WCF service hosted on IIS 安装IIS上托管的WCF服务所需的Windows组件

RUN Add-WindowsFeature NET-WCF-TCP-Activation45; \
Add-WindowsFeature NET-WCF-HTTP-Activation45; \
Add-WindowsFeature Web-WebSockets

Enable net.tcp protocol for default web site on IIS 在IIS上为默认网站启用net.tcp协议

RUN windows\system32\inetsrv\appcmd.exe set app 'Default Web Site/' /enabledProtocols:"http,net.tcp"

EXPOSE 808

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM