0

我已经构建了具有 2 个服务的 WCF 应用程序。我想在 Single 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>

我正在参考这个示例来访问单个 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 服务代码

public class 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 });  
    }  
    }
4

0 回答 0