[英]WCF Service listening to multiple ports
I need to install a WCF service on a single server but I need to listen to multiple ports (due to various issues with machines running the client). 我需要在单个服务器上安装WCF服务,但是我需要侦听多个端口(由于运行客户端的计算机存在各种问题)。 It's not clear to me if this can be done with one
ServiceHost
and: one "class dervied from"(CDF) ServiceBase
with two endpoints
, or if I need two CDF ServiceBase
s each with a single endpoint
. 我不清楚这是否可以使用一个
ServiceHost
和:一个具有两个endpoints
“类源自”(CDF) ServiceBase
来完成,或者是否需要两个CDF ServiceBase
以及一个endpoint
。 Here is the relevant code in relevant files: 以下是相关文件中的相关代码:
Program.cs Program.cs中
static class Program
{
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
}
}
Service1.cs Service1.cs
public partial class Service1 : ServiceBase
{
private wcfServer _server;
ServiceHost serviceHost;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(String[] args)
{
_server = new wcfServer();
serviceHost = new ServiceHost(_server);
serviceHost.Open();
}
protected override void OnStop()
{
if (serviceHost != null)
serviceHost.Close();
}
}
wcfServer.cs wcfServer.cs
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,
ConcurrencyMode=ConcurrencyMode.Multiple)]
public class wcfServer : IwcfServer { /* ... */ }
app.config 的app.config
<services>
<service name="MyService.wcfServer">
<endpoint address=""
bindingConfiguration="BigDataBinding"
binding="basicHttpBinding"
contract="DataBackups.IwcfServer">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost/DataBackups/wcfServer/" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
I should note that 我要注意
Can someone provide an example of how to accomplish this? 有人可以提供一个如何完成此操作的示例吗?
Also, I do not understand how <service>
configurations in app.config
relate to CDF ServiceBase
instances created programmatically. 另外,我不了解
app.config
<service>
配置如何与以编程方式创建的CDF ServiceBase
实例相关。 In the current setup, there is only a single Service1
instance and a single configuration in app.config
, so I imagine they "line up." 在当前设置中,
app.config
只有一个Service1
实例和一个配置,因此我想它们会“排队”。 But, what if I there are multiple Service1
instances (which I might need)? 但是,如果我有多个
Service1
实例(可能需要)怎么办? How do I "choose" which service I am configuring? 如何“选择”正在配置的服务?
Your WCF
Service should have no issue listening on multiple ports. 您的
WCF
服务在多个端口上侦听应该没有问题。 You just need to expand the baseAddress
section of your app.config
file as follows: 您只需
baseAddress
如下所示扩展app.config
文件的baseAddress
部分:
<baseAddresses>
<add baseAddress="http://localhost:80/DataBackups/wcfServer/" />
<add baseAddress="http://localhost:8732/DataBackups/wcfServer/" />
</baseAddresses>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.