简体   繁体   English

WCF:NetMsmq而不是Http的应用程序池挂起后,自定义ServiceHostFactory错误

[英]WCF: Custom ServiceHostFactory errors after Application Pool suspension for NetMsmq but not Http

I've created a custom host factory to support container based serviced instantiation and imperfect async cancellation. 我创建了一个自定义宿主工厂,以支持基于容器的服务实例化和不完美的异步取消。 The service is instantiated on a per-call basis: 该服务在每个呼叫的基础上实例化:

public class DIServiceHostFactory : ServiceHostFactory
{
    protected IContainer Container { get; private set; }
    public DIServiceHostFactory()
        : this(ContainerFactory.GetContainerForMap<DIServiceHostFactory>())
    {
    }
    public DIServiceHostFactory(IContainer container)
    {
        this.Container = container;
    }
    protected override System.ServiceModel.ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
        return new DIServiceHost(this.Container, serviceType, baseAddresses);
    }
}
public class DIServiceHost : ServiceHost
{
    protected IContainer Container { get; private set; }
    public DIServiceHost() : this(ContainerFactory.GetContainerForMap<DIServiceHost>()) { }
    public DIServiceHost(IContainer container) 
    {
        this.Container = container;
    }
    public DIServiceHost(IContainer container, Type serviceType, params Uri[] baseAddresses) : base(serviceType, baseAddresses) 
    {
        this.Container = container;
    }
    protected override void OnOpening()
    {

        Description.Behaviors.Add(CreateBehavior());
        base.OnOpening();
    }
    protected virtual DIServiceBehavior CreateBehavior()
    {
        return new DIServiceBehavior(this.Container);
    }

}
public class DIServiceBehavior : IServiceBehavior
{
    protected IContainer Container { get; private set; }
    public DIServiceBehavior(IContainer container)
    {
        this.Container = container;
    }
    public virtual void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
    {


    }
    protected virtual DIInstanceProvider ConstructProvider(ServiceDescription description)
    {
        return new DIInstanceProvider(description.ServiceType, Container);
    }
    public virtual void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        foreach (ChannelDispatcher cd in serviceHostBase.ChannelDispatchers.OfType<ChannelDispatcher>())
        {
            foreach (EndpointDispatcher ed in cd.Endpoints)
            {
                ed.DispatchRuntime.InstanceProvider = ConstructProvider(serviceDescription);
            }
        }
    }

    public virtual void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {

    }

}
public class DIInstanceProvider : IInstanceProvider
{
    protected Type ServiceType { get; private set; }
    protected IContainer Container { get; private set; }
    protected ILogger Logger { get; private set; }
    public DIInstanceProvider(Type serviceType, IContainer container)
    {
        this.ServiceType = serviceType;
        this.Container = container;
        ILogger logger;
        container.TryResolve<ILogger>(out logger);
        this.Logger = logger;
    }
    public virtual object GetInstance(InstanceContext instanceContext, Message message)
    {
        if (this.Logger != null)
            this.Logger.Info(message.ToString());
        CancellationTokenSource cancellationToken = new CancellationTokenSource();

        var s = this.Container.Resolve(this.ServiceType);
        var service = s as ICancellableService;
        if (service != null)
        {
            service.CancellationToken = cancellationToken.Token;
            instanceContext.Faulted += (sender, e) => cancellationToken.Cancel();
            instanceContext.Closing += (sender, e) => cancellationToken.Cancel();
        }
        return s;
    }

    public object GetInstance(InstanceContext instanceContext)
    {
        return GetInstance(instanceContext, null);
    }

    public virtual void ReleaseInstance(InstanceContext instanceContext, object instance)
    {

    }
}

The primary usage is through the netmsmq binding, I notice when I first start the application the client receives no errors and the MSMQ just holds the data until I hit the service over HTTP through a web browser, I'm not sure if this error is generated at this time. 主要用法是通过netmsmq绑定,我注意到,当我第一次启动该应用程序时,客户端没有收到任何错误,而MSMQ只是保留数据,直到我通过Web浏览器通过HTTP命中该服务为止,我不确定是否是此错误此时产生的。 After that it will process through the queue until the app pool suspends due to inactivity (currently set at 20min). 之后,它将处理队列直到应用程序池由于不活动而暂停(当前设置为20分钟)。 Then I receive the follow error frequently (but not with every client request, I had ~250 errors in my event log this morning and over 1500 messages waiting in my queue): 然后,我经常收到跟随错误(但不是每次客户请求都收到,今天早上我的事件日志中有250个错误,队列中有1500多个消息在等待):

WebHost failed to process a request.
 Sender Information:     System.ServiceModel.ServiceHostingEnvironment+HostingManager/17653682
 Exception: System.ServiceModel.ServiceActivationException: The     service '/MerchReport/reportconsumer.svc' cannot be activated due to an exception during compilation.  The exception message is: Object reference not set to an instance of an object.. ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at United.UEL.WCF.DIInstanceProvider..ctor(Type serviceType, IContainer container)
   at United.UEL.WCF.DIServiceBehavior.ConstructProvider(ServiceDescription description)
   at United.UEL.WCF.DIServiceBehavior.ApplyDispatchBehavior(ServiceDescription serviceDescription, 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 System.ServiceModel.ServiceHostingEnvironment.HostingManager.ActivateService(ServiceActivationInfo serviceActivationInfo, EventTraceActivity eventTraceActivity)
   at System.ServiceModel.ServiceHostingEnvironment.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath, EventTraceActivity eventTraceActivity)
   --- End of inner exception stack trace ---
   at System.ServiceModel.ServiceHostingEnvironment.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath, EventTraceActivity eventTraceActivity)
   at System.ServiceModel.ServiceHostingEnvironment.EnsureServiceAvailableFast(String relativeVirtualPath, EventTraceActivity eventTraceActivity)
 Process Name: w3wp
 Process ID: 13740

It appears that my container is not configured when MSMQ hits it. 看来我的容器没有被MSMQ命中。 Currently I call my bootstrapper in the Global.asax.Application_Start event, is there possibly a better place to call it? 当前,我在Global.asax.Application_Start事件中调用我的引导程序,有没有更好的地方可以调用它?

Found this: http://blogs.msdn.com/b/wenlong/archive/2006/01/11/511514.aspx 找到了这个: http : //blogs.msdn.com/b/wenlong/archive/2006/01/11/511514.aspx

Explains several options for dealing with non-http activation initializations. 说明用于处理非http激活初始化的几个选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在WCF服务库项目中使用自定义ServiceHostFactory - Using a custom ServiceHostFactory in a WCF Service Library project Sharepoint 2010中的WCF服务找不到我的自定义ServiceHostFactory类 - WCF Service in Sharepoint 2010 can't find my Custom ServiceHostFactory class WCF-实施ServiceHostFactory以用于IIS上下文(svc文件) - WCF - Implementing a ServiceHostFactory to use into IIS context (svc file) 应用程序池回收后,WCF服务没有响应 - WCF service is not responding after app pool recycle 自定义ServiceHostFactory:找不到合同名称“ IMetadataExchange” - Custom ServiceHostFactory: The contract name 'IMetadataExchange' could not be found NServiceBus Transport 在使用自定义 ServiceHostFactory 时为 NULL,WAS - NServiceBus Transport is NULL when using a custom ServiceHostFactory, WAS WCF - 如何制作自定义错误? - WCF - how to make custom errors? 框架升级后,错误和警告似乎与IIS中的应用程序池问题有关 - Errors and warnings seemingly related to application pool issues in IIS after framework upgrade 在Application_Deactivated事件中渲染WriteableBitmap会导致应用在暂停后无法重新激活 - Rendering a WriteableBitmap within Application_Deactivated event causes app to fail to re-activate after suspension 在WCF中使用自定义Http标头 - Using Custom Http Header in WCF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM