WAS 非 HTTP 激活 - 挂钩应用程序启动

发布于 2024-08-25 16:55:53 字数 248 浏览 15 评论 0原文

我正在尝试将 WAS 内托管的基于 netTcpBinding 的应用程序与 IoC 容器(autofac/spring)集成。不幸的是,当它在 WAS 内部启动时,由于它不是基于 Http 的应用程序,因此全局应用程序类内部不会触发任何事件。

我需要捕获应用程序域启动,以便我可以配置 IoC 容器。在 WAS 中托管时有什么办法可以做到这一点吗?

我见过涉及在 App_Code 文件夹中使用静态类的可怕事情,但我想要一些更可测试且不太脏的东西。

I'm trying to integrate a netTcpBinding based application that is hosted inside WAS with an IoC container (autofac/spring). Unfortunately, when it starts inside WAS and due to the fact that it is not an Http based application, no events are fired inside the Global application class.

I need to catch the application domain startup so that I can configure the IoC container. Is there any way to do this when hosting in WAS?

I've seen horrible things involving using static classes inside App_Code folders, but I'd like something somewhat more testable and not quite as dirty.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如歌彻婉言 2024-09-01 16:55:53

您可以实现 IInstanceProvider(请参阅 http://msdn.microsoft .com/en-us/library/system.servicemodel.dispatcher.iinstanceprovider.aspx)并告诉 WCF 运行时通过服务行为使用它。

可以使用自定义主机、配置或属性来配置服务行为。这是后者的一个例子:

public class DependencyInjectionServiceBehaviorAttribute : Attribute, IServiceBehavior
{
    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        foreach (var cdb in serviceHostBase.ChannelDispatchers)
        {
            var cd = cdb as ChannelDispatcher;
            if (cd != null)
            {
                foreach (EndpointDispatcher ed in cd.Endpoints)
                {
                    ed.DispatchRuntime.InstanceProvider = new MyServiceFactory(serviceDescription.ServiceType);
                }
            }
        }
    }

    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase,  
            Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) {}
    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) {}
}

You can implement IInstanceProvider (see http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.iinstanceprovider.aspx) and tell the WCF runtime to use it via a service behavior.

The service behavior can be configured with a custom host, configuration or with an attribute. Here an example of the latter:

public class DependencyInjectionServiceBehaviorAttribute : Attribute, IServiceBehavior
{
    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        foreach (var cdb in serviceHostBase.ChannelDispatchers)
        {
            var cd = cdb as ChannelDispatcher;
            if (cd != null)
            {
                foreach (EndpointDispatcher ed in cd.Endpoints)
                {
                    ed.DispatchRuntime.InstanceProvider = new MyServiceFactory(serviceDescription.ServiceType);
                }
            }
        }
    }

    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase,  
            Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) {}
    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) {}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文