托管WCF服务作为Windows服务?

时间:2012-07-13 08:54:53

标签: c# .net wcf windows-services self-hosting

我已设法将复杂的WCF服务移动到Windows服务中。绑定看起来像这样:

<service behaviorConfiguration="MyAppClientService.CustomValidator_Behavior" name="MyApp.ServiceImplementation.MyAppClientService">
        <endpoint binding="netTcpBinding" bindingConfiguration="netTcpRegular" address="Regular" bindingNamespace="http://MyApp.ServiceContracts/2007/11" contract="MyApp.ServiceContracts.IMyAppClientService"/>
        <endpoint binding="netTcpBinding" bindingConfiguration="netTcpWindowMessageSecurity" address="Windows" bindingNamespace="http://MyApp.ServiceContracts/2007/11" contract="MyApp.ServiceContracts.IMyAppClientService"/>
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8001/MyAppService/Client"/>
            <add baseAddress="http://localhost:8002/MyAppService/Client"/>
          </baseAddresses>
        </host>
      </service>

当服务启动时,我浏览:http://localhost:8002/MyAppService/Client这很好用,我也可以看到WSDL。

但是当我尝试使用Winform客户端连接到该服务时,它无法找到该服务,这就是客户端中地址的样子:

<client>
<endpoint address="net.tcp://localhost:8001/MyAppService/Client/MyAppClientService.svc/Regular" behaviorConfiguration="BasicBehavior" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IMyAppClientServiceRegular" contract="MyApp.ServiceContracts.IMyAppClientService" name="MyTest_RegularLogin"/>
</client>

浏览http://localhost:8001/MyAppService/Client时,我会看到一个丢失的页面,我认为这是正确的,因为它托管在tcp而不是http?

当服务托管在IIS7(WAS)中时,这工作得很好,但后来我在客户端使用了一个如下所示的端点:

<endpoint address="net.tcp://localhost/MyAppDev/MyAppClientService.svc/Regular" behaviorConfiguration="BasicBehavior" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IMyAppClientServiceRegular" contract="MyApp.ServiceContracts.IMyAppClientService" name="MyApp_RegularLogin"/>

注意:这是常规登录的常规统计信息,客户端提供用户名和密码(无Windows登录)

修改:

我已经关注了这篇文章:http://msdn.microsoft.com/en-us/library/ms733069.aspx

这就是Windows服务类的样子

public class MyAppWindowsService : ServiceBase
    {
        public ServiceHost _serviceHost = null;
        public MyAppWindowsService()
        {
            // Name the Windows Service
            ServiceName = "MyAppWindowsService";
        }

        public static void Main()
        {
            ServiceBase.Run(new MyAppWindowsService());
        }

        // Start the Windows service.
        protected override void OnStart(string[] args)
        {
            if (_serviceHost != null)
            {
                _serviceHost.Close();
            }

            // Create a ServiceHost for the CalculatorService type and 
            // provide the base address.
            _serviceHost = new ServiceHost(typeof(MyApp.ServiceImplementation.MyAppClientService));

            // Open the ServiceHostBase to create listeners and start 
            // listening for messages.
            _serviceHost.Open();
        }

        protected override void OnStop()
        {
            if (_serviceHost != null)
            {
                _serviceHost.Close();
                _serviceHost = null;
            }
        }

    }

1 个答案:

答案 0 :(得分:1)

问题是我尝试连接到localhost/MyAppDev/MyAppClientService.svc/Regular,但它会localhost/MyAppDev/Regular