如何为在联网计算机上运行的客户端配置WCF服务?

时间:2021-07-08 07:11:40

I’ve just created a WCF service/client and it all works fine when running on the same machine. But can’t figure out how to configure it to run on different machines. Do you know how?

我刚刚创建了一个WCF服务/客户端,它在同一台机器上运行时工作正常。但无法弄清楚如何配置它在不同的机器上运行。你知不知道怎么?

At the moment the URI is set to http://localHost:8000......

目前,URI设置为http:// localHost:8000 ......

But I think I want something like net.tcp://MyServer:8000…..

但我想我想要像net.tcp:// MyServer:8000 ......

Any ideas would be great. Thanks.

任何想法都会很棒。谢谢。

2 个解决方案

#1


From what it sounds like, you have both the service and client in the same executable. While this can be done, when you want them on separate machines you need to have an executable/host for the service (either self hosted, or in IIS) and an executable for the client. Each will need to be properly configured with the address, binding, and contract in the appropriate configuration section for it. So on the server you'd have something like this:

从它的声音来看,您将服务和客户端都放在同一个可执行文件中。虽然可以这样做,但是当您希望它们位于不同的计算机上时,您需要拥有服务的可执行文件/主机(自托管或IIS)和客户端的可执行文件。每个都需要在相应的配置部分中使用地址,绑定和合同进行正确配置。所以在服务器上你会有这样的东西:

<configuration>
    <system.serviceModel>
        <services>
            <service name="YourService">
                <endpoint address="http://MyServer:8000/..."
                          binding="BasicHttpBinding"
                          contract="Your.IContract" />
            </service>
        </services>
    </system.serviceModel>
</configuration>

And on the client you'd have this:

在客户端你有这个:

<configuration>
    <system.serviceModel>
        <client>
            <endpoint address="http://MyServer:8000/..."
                      binding="BasicHttpBinding"
                      contract="Your.IContract"
                      name="ClientEndpoint" />
        </client>
    </system.serviceModel>
</configuration>

The main thing is to make sure that the client and server can communicate with each other over the specified port and protocol (primarily making sure a firewall isn't blocking communication). The other thing to be aware of is changing your binding protocol may impact other aspects of your service (security is a big one, but also what you can and cannot do with the service).

主要是确保客户端和服务器可以通过指定的端口和协议相互通信(主要是确保防火墙不阻止通信)。另一件要注意的事情是更改绑定协议可能会影响服务的其他方面(安全性是一个很大的问题,但也是您可以和不能对服务做什么)。

#2


There isn't enough information here to answer your question.

这里没有足够的信息来回答您的问题。

Assuming you aren't setting address/binding/contract information in the ServiceHost and proxies through code, you need to post the section of your config file.

假设您没有通过代码在ServiceHost和代理中设置地址/绑定/合同信息,则需要发布配置文件的部分。

If you are doing it in code, then you need to show what code you are using.

如果您在代码中执行此操作,则需要显示正在使用的代码。

From what I can tell, it seems that you might have a mismatch with the transport binding. The service and the client have to be on the same transport (http, tcp, named pipes, etc, etc).

据我所知,您似乎可能与传输绑定不匹配。服务和客户端必须在同一个传输(http,tcp,命名管道等)上。

#1


From what it sounds like, you have both the service and client in the same executable. While this can be done, when you want them on separate machines you need to have an executable/host for the service (either self hosted, or in IIS) and an executable for the client. Each will need to be properly configured with the address, binding, and contract in the appropriate configuration section for it. So on the server you'd have something like this:

从它的声音来看,您将服务和客户端都放在同一个可执行文件中。虽然可以这样做,但是当您希望它们位于不同的计算机上时,您需要拥有服务的可执行文件/主机(自托管或IIS)和客户端的可执行文件。每个都需要在相应的配置部分中使用地址,绑定和合同进行正确配置。所以在服务器上你会有这样的东西:

<configuration>
    <system.serviceModel>
        <services>
            <service name="YourService">
                <endpoint address="http://MyServer:8000/..."
                          binding="BasicHttpBinding"
                          contract="Your.IContract" />
            </service>
        </services>
    </system.serviceModel>
</configuration>

And on the client you'd have this:

在客户端你有这个:

<configuration>
    <system.serviceModel>
        <client>
            <endpoint address="http://MyServer:8000/..."
                      binding="BasicHttpBinding"
                      contract="Your.IContract"
                      name="ClientEndpoint" />
        </client>
    </system.serviceModel>
</configuration>

The main thing is to make sure that the client and server can communicate with each other over the specified port and protocol (primarily making sure a firewall isn't blocking communication). The other thing to be aware of is changing your binding protocol may impact other aspects of your service (security is a big one, but also what you can and cannot do with the service).

主要是确保客户端和服务器可以通过指定的端口和协议相互通信(主要是确保防火墙不阻止通信)。另一件要注意的事情是更改绑定协议可能会影响服务的其他方面(安全性是一个很大的问题,但也是您可以和不能对服务做什么)。

#2


There isn't enough information here to answer your question.

这里没有足够的信息来回答您的问题。

Assuming you aren't setting address/binding/contract information in the ServiceHost and proxies through code, you need to post the section of your config file.

假设您没有通过代码在ServiceHost和代理中设置地址/绑定/合同信息,则需要发布配置文件的部分。

If you are doing it in code, then you need to show what code you are using.

如果您在代码中执行此操作,则需要显示正在使用的代码。

From what I can tell, it seems that you might have a mismatch with the transport binding. The service and the client have to be on the same transport (http, tcp, named pipes, etc, etc).

据我所知,您似乎可能与传输绑定不匹配。服务和客户端必须在同一个传输(http,tcp,命名管道等)上。