下面是我的主机 Windows 服务的 app.config 文件片段。
<services>
<service name="Share.Services.MainService">
<endpoint address="net.tcp://localhost:8001/MainService" behaviorConfiguration="netTcpBehavior" binding="netTcpBinding" contract="Share.Services.IClaimService" />
</service>
<service name="Share.Services.MainMasterPageService">
<endpoint address="net.tcp://localhost:8001/MainMasterPageService" behaviorConfiguration="netTcpBehavior" binding="netTcpBinding" contract="Share.Services.IMainMasterpageService" />
</service>
<service name="Share.Services.SMSService">
<endpoint address="net.tcp://localhost:8001/SMSService" behaviorConfiguration="netTcpBehavior" binding="netTcpBinding" contract="Share.ServiceContracts.Messaging.ISMSService" />
</service>
</services>
有 3 个 wcf 服务配置为使用端口为 8001 的 TCP 端点。在 Windows 服务中,我使用以下代码注册服务主机
private static ServiceHost[] Hosts = null;
public static void Start()
{
try
{
while (!System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
{
System.Threading.Thread.Sleep(1000);
}
BaseLog.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
Hosts = new ServiceHost[]
{
new ServiceHost(typeof(MainService)),
new ServiceHost(typeof(MainMasterPageService)),
new ServiceHost(typeof(SMSService))
};
foreach (ServiceHost host in Hosts)
{
RegisterServiceHost(host);
}
_log.Info("All Hosts Open");
}
catch(Exception e)
{
_log.Error( "MainServiceHost", e);
}
}
对于我调用 RegisterServiceHost 函数的每个 ServiceHost 对象,该函数的代码如下
public static void RegisterServiceHost(ServiceHost host)
{
var ops = (from e in host.Description.Endpoints
from o in e.Contract.Operations
select o).ToList();
ops.ForEach(
operation => operation.Behaviors.Add(new MainContextOperationBehavior())
);
host.Open();
}
上面的代码可以正常工作,没有任何问题。我的问题是所有服务都共享同一个端口 8001。所有服务如何能够共享同一个端口。甚至机器上也没有启用 Net.TCP 端口共享服务。我的另一个问题是,它会在共享同一端口时对性能造成任何影响。如果我为每个服务提供独特的端口,例如 8001,8002 ,8003,那么它的优势是什么。