WCF Service Hosting Inside a Windows Service

I am having problems hosting a WCF service in a Windows service. I can start my WCF service in VS2008 and navigate to the base address in my app.config

<configuration> <system.web> <compilation debug="true" /> </system.web> <system.serviceModel> <services> <service behaviorConfiguration="WCF.IndexerBehavior" name="WCF.Indexer"> <endpoint address="" binding="wsHttpBinding" contract="WCF.IIndexer"> <identity> <dns value="localhost" /> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> <host> <baseAddresses> <add baseAddress="http://localhost/WCFService/Action/" /> </baseAddresses> </host> </service> </services> <behaviors> <serviceBehaviors> <behavior name="WCF.IndexerBehavior"> <serviceMetadata httpGetEnabled="True"/> <serviceDebug includeExceptionDetailInFaults="False" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration> 

I see that it works fine, I get a page that says that I created a sample service and code, how to use it.

Now my next step was to create a Windows service to host my WCF as shown above.

I just used the te windows service template, it gave me Program.cs and Service1.cs, which I renamed to WindowsServiceHost.cs. In it I:

 private ServiceHost host; public WindowsServiceHost() { InitializeComponent(); } protected override void OnStart(string[] args) { try { var serviceType = typeof(Indexer.WCF.Indexer); host = new ServiceHost(serviceType); host.Open(); } catch (Exception ex) { } } protected override void OnStop() { if (host != null) { host.Close(); } } 

Everything compiles fine, I can run InstallUtil (I defined the installer). The service, which was used to immediately start and stop, but disabling Windows Defender, got rid of this. Now the service starts (as a network service) and remains (I think), but when I go to the base address, I get a page not found. Another weird thing is when I try to stop the service (which still shows up as work), I get:

Error 1061: the service cannot receive control messages at this time

I tried everything but do not understand.

+3
source share
3 answers

I’m not sure what the reason is - in fact - we just support WCF services in Windows services, and this generally works fine.

Two points you could try - just to feel the behavior and the potential key to the problem:

1) I notice that you open ServiceHost only with the type of service - it works, but you can still add the base address even to the new ServiceHost() call - like this:

 host = new ServiceHost(serviceType, new Uri("http://localhost:8181/WCFService/Action/"); 

Can you go to this address and get a service page?

2). Another thing I noticed is that your service seems to be called Indexer.WCF.Indexer , as indicated in typeof (), before opening the host, but in the configuration file, the name= tag in the <service> only "WCF.Indexer".

Could you try changing this read tag:

 <service behaviorConfiguration="WCF.IndexerBehavior" name="Indexer.WCF.Indexer"> 

Does it help? Now can you see the service page when navigating it in a browser?

Mark

+3
source

Self-hosted HTTP hosting in a Windows service may require endpoint registration with HttpCfg.exe. Take a look here .

+1
source

Try to remove the catch statement, there may be an error that you do not see

0
source

Source: https://habr.com/ru/post/1333804/


All Articles