用工作代码片段回答自己,因为它们可能对其他人有帮助。
因此,看起来无法以与主机无关的方式删除默认管道贡献者(尽管我不明白为什么不能修改 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>();
...