简体   繁体   中英

WCF Service, working in WCF Svc Host, NOT working as a Windows service

Environment: Windows XP, Visual Studio 2010, IIS NOT installed.

I've developed a WCF service as a library that works fine when hosted in WCFSvcHost; I can connect to it, update a client's service reference, the works.

I'm unable to to host it as a Windows Service, though. The service installs okay, I can attach to it and break in my code. In order to debug the hosting, I've moved the code to OnResume() (OnStart does nothing).

Here's my OnResume (names have been changed to protect the innocent):

protected override OnResume()
{
    if (_selfHost == null)
    {
        _selfHost = new ServiceHost(typeof(MyService));
        _selfHost.Open();
    }
}

When stepping over the Open() method, I get the following exception:

System.InvalidOperationException was unhandled by user code
  Message=The HttpGetEnabled property of ServiceMetadataBehavior is set to true and the HttpGetUrl property is a relative address, but there is no http base address.  Either supply an http base address or set HttpGetUrl to an absolute address.
  Source=System.ServiceModel
  StackTrace:
       at System.ServiceModel.Description.ServiceMetadataBehavior.EnsureGetDispatcher(ServiceHostBase host, ServiceMetadataExtension mex, Uri url, String scheme)
       at System.ServiceModel.Description.ServiceMetadataBehavior.CreateHttpGetEndpoints(ServiceDescription description, ServiceHostBase host, ServiceMetadataExtension mex)
       at System.ServiceModel.Description.ServiceMetadataBehavior.ApplyBehavior(ServiceDescription description, ServiceHostBase host)
       at System.ServiceModel.Description.ServiceMetadataBehavior.System.ServiceModel.Description.IServiceBehavior.ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase)
       at System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription description, ServiceHostBase serviceHost)
       at System.ServiceModel.ServiceHostBase.InitializeRuntime()
       at System.ServiceModel.ServiceHostBase.OnBeginOpen()
       at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)
       at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
       at System.ServiceModel.Channels.CommunicationObject.Open()
       at MyService.StartService()
       at MyService.OnContinue()
       at System.ServiceProcess.ServiceBase.DeferredContinue()
  InnerException: 

No amount of changing my config file seems to solve the problem:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" />
  </startup>
  <system.web>
    <compilation debug="true" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="MyService">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8732/MyService/" />
          </baseAddresses>
        </host>
        <endpoint address="" binding="wsHttpBinding" contract="MyLib.IMyService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="http://localhost:8732/MyService/mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="True" />
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

As a test, I've tried to set httpGetEnabled to False and to remove the mex endpoint, but that only moved the problem elsewhere: the error message goes away and the service starts, but then my client can't call the service:

System.ServiceModel.EndpointNotFoundException was caught
  Message=There was no endpoint listening at http://localhost:8732/MyService/ that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
[...]

  InnerException: System.Net.WebException
       Message=The remote server returned an error: (400) Bad Request.
       Source=System
       StackTrace:
            at System.Net.HttpWebRequest.GetResponse()
            at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
       InnerException: 

Any help?

Got it.

I had incorrectly copied the relevant section from the config file, not once, but twice (both in the windows service and the console app).

Thanks Terry. You got me thinking along the right lines.

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