我在服务启动时有一些一次性的初始化任务,为了做到这一点,我已经定义了一个自定义的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可能会破坏简化配置。是这样的,还是有什么我忽视的东西会让它继续发挥作用?
答案 0 :(得分:3)
如果覆盖InitializeRuntime(),请确保调用base.InitializeRuntime(),否则您可能会看到此行为。在调用我的附加启动逻辑之前调用base.InitializeRuntime()之后,我的服务现在正确地路由到。