【问题标题】:Does using a custom ServiceHost disable simplified configuration?使用自定义 ServiceHost 会禁用简化配置吗?
【发布时间】:2013-04-19 22:57:23
【问题描述】:

我有一些一次性初始化任务要在服务启动时完成,为了做到这一点,我定义了一个自定义的 ServiceHostFactory 和 ServiceHost 来覆盖 InitializeRuntime。

Service1.svc 标记:

<%@ ServiceHost Language="C#" Debug="true" Factory="service.Our_Service_Host_Factory" Service="service.Service1" CodeBehind="Service1.svc.cs" %>

服务主机定义:

public class Our_Service_Host_Factory : ServiceHostFactory
{
    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
        return new Our_Service_Host(serviceType, baseAddresses);
    }
}

class Our_Service_Host : ServiceHost
{
    public Our_Service_Host(Type serviceType, Uri[] baseAddresses)
        : base(serviceType, baseAddresses) { }

    protected override void InitializeRuntime()
    {
         // one time setup code
    }
}

来自 web.config 的片段:

<system.serviceModel>
  <behaviors>
    <serviceBehaviors>
      <behavior>
        <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
        <serviceDebug includeExceptionDetailInFaults="false"/>
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <protocolMapping>
    <add binding="basicHttpsBinding" scheme="https" />
  </protocolMapping>    
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>

如果我删除 ServiceHost 标记上的 Factory 属性,WCF 测试客户端可以成功连接到我的服务。如果我把它放回去,它找不到元数据端点并出现以下错误:

Error: Cannot obtain Metadata from http://localhost:50154/Service1.svc If this is a Windows (R)
Communication Foundation service to which you have access, please check that you have enabled 
metadata publishing at the specified address.  For help enabling metadata publishing, please 
refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata 
Exchange Error    URI: http://localhost:50154/Service1.svc    Metadata contains a reference that 
cannot be resolved: 'http://localhost:50154/Service1.svc'.    There was no endpoint listening at 
http://localhost:50154/Service1.svc that could accept the message. This is often caused by an 
incorrect address or SOAP action. See InnerException, if present, for more details.    The 
remote server returned an error: (404) Not Found.HTTP GET Error    URI: 
http://localhost:50154/Service1.svc    There was an error 
downloading 'http://localhost:50154/Service1.svc'.    The request failed with HTTP status 404: 
Not Found.

看起来拥有自定义 ServiceHost/ServiceHostFactory 可能会破坏简化配置。是这样吗,还是有什么我忽略的东西会使它继续工作?

【问题讨论】:

  • 我注意到您有 service.Our_Service_Host_Factory 代表工厂 - 是否可以像 Service.Our_Service_Host_Factory 一样简单(大写与小写)?
  • 另外,您是否尝试将&lt;%@Assembly name="MyAssembly" %&gt; 添加到您的 svc 文件中?当然,将 MyAssembly 替换为您的程序集名称。 :)
  • 不,它所在的命名空间肯定是服务。

标签: wcf


【解决方案1】:

如果您覆盖 InitializeRuntime(),请确保调用 base.InitializeRuntime(),否则您可能会看到此行为。在调用我的附加启动逻辑之前调用 base.InitializeRuntime() 之后,我的服务现在被正确路由到。

【讨论】:

  • 感谢分享答案 - 我们在我们的应用程序中使用自定义服务主机,目前正在从 3.5 升级到 4.5,所以我需要确保它被捕获 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-27
  • 2015-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多