I am having some trouble hosting a WCF service inside a Windows Service. I can start my WCF service in VS2008 and by navigating 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 can see it works fine, I get the page saying I created a service and code samples on how to use it are shown.
Now my next step was to create a Windows Service to host my WCF shown above.
I just used te windows service template, it gave me a Program.cs and Service1.cs which I renamed to WindowsServiceHost.cs. In it I have:
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 an installer). The service used to start and stop immediately but disabling Windows Defender got rid of this. Now the service starts (As a network service) and stays up (I think), but when I navigate to the base address, I get the not found page. Another weird thing is when I try to stop the service (which is still displayed as running) I get:
Error 1061: The service cannot accept control messages at this time
I've tried everything but am at a loss.