WCF服务,在WCF Svc主机中工作,不作为Windows服务工作

本文关键字:WCF 服务工作 不作为 Windows 工作 主机 服务 Svc | 更新日期: 2023-09-27 18:25:18

环境:Windows XP、Visual Studio 2010、IIS未安装

我已经开发了一个WCF服务,作为一个库,当托管在WCFSvcHost中时可以很好地工作;我可以连接到它,更新客户的服务参考,工作。

不过,我无法将其作为Windows服务托管。该服务安装正常,我可以连接到它并插入我的代码。为了调试托管,我将代码移到了OnResume()(OnStart什么都不做)。

这是我的OnResume(为了保护无辜者,已经更改了名称):

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

当遍历Open()方法时,我得到以下异常:

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: 

再多的更改我的配置文件似乎也无法解决问题:

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

作为测试,我尝试将httpGetEnabled设置为False并删除mex端点,但这只会将问题转移到其他地方:错误消息消失,服务启动,但我的客户端无法调用服务:

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: 

有什么帮助吗?

WCF服务,在WCF Svc主机中工作,不作为Windows服务工作

明白了。

我错误地从配置文件中复制了相关部分,不是一次,而是两次(在windows服务和控制台应用程序中)。

谢谢Terry。你让我沿着正确的路线思考。