我已经构建了具有2个服务的WCF应用程序。我想在单一Windows服务下访问它们。我只能访问2中的一项服务。 我的app.config看起来像:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service behaviorConfiguration="DefaultBehavior" name="FinalTest.CalculatorService">
<endpoint address="" binding="netTcpBinding" bindingConfiguration="TCPBindingConfig" name="TCPEndpoint" contract="FinalTest.ICalculator" />
<endpoint address="mex" binding="mexTcpBinding" bindingConfiguration="" name="TcpMetaData" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8001/FinalTest/CalculatorService" />
</baseAddresses>
</host>
</service>
<service behaviorConfiguration="DefaultBehavior" name="FinalTest.Hello">
<endpoint address="" binding="netTcpBinding" bindingConfiguration="TCPBindingConfig" name="TCPEndpoint" contract="FinalTest.IHello" />
<endpoint address="mex" binding="mexTcpBinding" bindingConfiguration="" name="TcpMetaData" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8002/FinalTest/Hello" />
</baseAddresses>
</host>
</service>
</services>
<bindings>
<netTcpBinding>
<binding name="TCPBindingConfig" maxBufferSize="5242880" maxReceivedMessageSize="5242880">
<readerQuotas maxStringContentLength="5242880" />
<security mode="None" />
</binding>
</netTcpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="DefaultBehavior">
<serviceMetadata httpGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceThrottling maxConcurrentCalls="21" maxConcurrentSessions="50" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
我引用this示例来访问单个Win下的多个WCF服务。服务。
我的服务一个代码如下:
[ServiceContract(Namespace = "http://FinalTest")]
public interface IHello
{
[OperationContract]
string GetHello();
}
/// <summary>
/// TODO: Update summary.
/// </summary>
public class Hello:IHello
{
public string GetHello()
{
return "Hello World";
}
}
我实现了ServiceManager类。
public class ServiceManager
{
public ServiceHost serviceHost = null;
readonly List<ServiceHost> serviceHosts = new List<ServiceHost>();
public void OpenAll()
{
OpenHost<CalculatorWindowsService>();
OpenHost<Hello>();
}
public void CloseAll()
{
foreach (ServiceHost serviceHost in serviceHosts)
serviceHost.Close();
}
private void OpenHost<t>()
{
Type type = typeof(t);
ServiceHost serviceHost = new ServiceHost(type);
serviceHost.Open();
serviceHosts.Add(serviceHost);
}
}
我的Windows服务代码
公共类WindowsService:ServiceBase { // public ServiceHost serviceHost = null; // public CalculatorWindowsService() // { // //命名Windows服务 // ServiceName =“WCFWindowsServiceSample”; //}
public static string WindowsServiceName = "TestService";
public static string WindowsServiceDescription = "Windows Service Description";
public static string WindowsServiceUsername = @"Rahul";
public static string WindowsServicePassword = "password";
private readonly ServiceManager serviceManager = new ServiceManager();
private readonly IContainer components = new Container();
protected override void Dispose(bool disposing)
{
if (serviceManager != null) serviceManager.CloseAll();
if (disposing && (components != null)) components.Dispose();
base.Dispose(disposing);
}
public WindowsService()
{
ServiceName = WindowsServiceName;
CanStop = true;
}
protected override void OnStart(string[] args)
{
base.OnStart(args);
serviceManager.OpenAll();
}
protected override void OnStop()
{
serviceManager.CloseAll();
base.OnStop();
}
public static void Main()
{
//ServiceBase.Run(new CalculatorWindowsService());
if (Environment.UserInteractive)
{
ServiceManager serviceManager = new ServiceManager();
serviceManager.OpenAll();
Console.ReadKey();
serviceManager.CloseAll();
}
else
ServiceBase.Run(new WindowsService());
}
}
和安装程序代码
[RunInstaller(true)]
public class ProjectInstaller : Installer
{
public ProjectInstaller()
{
Installers.Add(new ServiceInstaller
{
StartType = ServiceStartMode.Automatic,
ServiceName = WindowsService.WindowsServiceName,
Description = WindowsService.WindowsServiceDescription
});
Installers.Add(new ServiceProcessInstaller { Account = ServiceAccount.User, Username = WindowsService.WindowsServiceUsername, Password = WindowsService.WindowsServicePassword });
}
}