1

删除默认管道贡献者(OpenRasta 2.0.3)的首选方法是什么?

我在网上没有找到很多,但一种方法似乎是编写自定义DependencyRegistrar,即从DefaultDependencyRegistrar 派生,然后例如覆盖AddDefaultContributors()。除此之外,我怀疑这是否是仅删除单个管道贡献者的最佳方法,它似乎需要额外的每个主机(ASP 与 InMemory)工作,而我认为将管道处理程序弄乱是与主机无关的事情。

但即使我走这条路,这里的这个人似乎也没有成功:http ://groups.google.com/group/openrasta/browse_thread/thread/d72b91e5994f402b 我尝试了类似的事情,但到目前为止还不能t 让我的自定义注册器替换默认值。

那么删除默认管道贡献者的最简单和最好的方法是什么,最好以与主机无关的方式?在某处有一个可行的例子吗?

4

2 回答 2

1

不,您只需要从注册商派生并使用可用的受保护成员来强制删除您不希望自动注册的类型。

注册器需要先在您的容器中注册,然后再将其提供给 OpenRasta,否则类型已被解析。

于 2011-11-09T14:51:10.623 回答
1

用工作代码片段回答自己,因为它们可能对其他人有帮助。

因此,看起来无法以与主机无关的方式删除默认管道贡献者(尽管我不明白为什么不能修改 OpenRasta 以允许将来轻松删除处理程序)。

需要编写的 2 个类实际上与使用的主机无关:

public class MyDependencyRegistrar : DefaultDependencyRegistrar
{
    protected override void AddDefaultContributors()
    {
        base.AddDefaultContributors();
        PipelineContributorTypes.Remove(typeof(HandlerResolverContributor));
        // If we remove the only contributor for the 'well-known'
        // IHandlerSelection stage, like done above, we need to add
        // another one implements IHandlerSelection, otherwise
        // we'll run into errors (and what's the point of a pipeline
        // without a handler selector anyway?). So let's do that here:
        AddPipelineContributor<MyOwnHandlerResolverContributor>();
    }
}

为了使 Registrar 可用,我们需要创建一个如下所示的访问器,然后需要在各个主机中设置它:

public class MyDependencyResolverAccessor : IDependencyResolverAccessor
{
    InternalDependencyResolver resolver;

    public IDependencyResolver Resolver
    {
        get
        {
            if (resolver == null)
            {
                resolver = new InternalDependencyResolver();
                resolver.AddDependency<IDependencyRegistrar, MyDependencyRegistrar>();
            }
            return resolver;
        }
    }
}

对于 Asp.Net,这似乎对我有用:

public class Global : System.Web.HttpApplication
{
    void Application_Start(object sender, EventArgs e)
    {
        OpenRastaModule.Host.DependencyResolverAccessor =
            new MyDependencyResolverAccessor();

对于我用于集成测试和处理程序的进程内访问的 InMemoryHost,我还没有找到一种方法来复制整个类 InMemoryHost 并根据我的需要对其进行修改。实际上,在这种情况下我们不需要 MyDependencyResolverAccessor,因为 InMemoryHost 已经实现了 IDependencyResolverAccessor。所以这就是它的样子。只有最后一行实际添加到 InMemoryHost 中的现有代码中:

public class TwinMemoryHost : IHost, IDependencyResolverAccessor, IDisposable
{
    readonly IConfigurationSource _configuration;
    bool _isDisposed;

    public TwinMemoryHost(IConfigurationSource configuration)
    {
        _configuration = configuration;
        Resolver = new InternalDependencyResolver();
        Resolver.AddDependency<IDependencyRegistrar, MyDependencyRegistrar>();
...
于 2011-11-10T01:57:40.147 回答