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

时间:2012-02-20 22:43:21

标签: c# wcf

环境:Windows XP,Visual Studio 2010,IIS NOT 已安装。

我已经开发了一个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: 

任何帮助?

1 个答案:

答案 0 :(得分:0)

知道了。

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

谢谢特里。你让我思考正确的方向。