简体   繁体   中英

Hosting WCF service as Windows Service?

I have manage to move my complexed WCF service into a Windows Service. The binding looks like this :

<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>

When the service is started I browse : http://localhost:8002/MyAppService/Client This works fine and I can also see the WSDL.

But when I try to connect to the service with my Winform client It cant find the service, this is how the address looks like in the client :

<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>

When browsing http://localhost:8001/MyAppService/Client I will get a missing page, I suppose that this is right because it is hosted on tcp and not http?

When the service was hosted in the IIS7(WAS) this was working just fine but then I used a endpoint at the client that looked like this :

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

Note : Regular stats that this is a regular login where the client provides username and password(no Windows login)

Edit :

I have followed this article : http://msdn.microsoft.com/en-us/library/ms733069.aspx

And this is how windows service class looks like

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;
            }
        }

    }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM