简体   繁体   English

如何将服务注入到Orchard中的自定义IHttpHandler中

[英]How to inject services into a custom IHttpHandler in Orchard

I am developing a module for Orchard which will restrict the items in the Media folder by looking up the authenticated user permissions. 我正在为Orchard开发一个模块,该模块将通过查找经过身份验证的用户权限来限制Media文件夹中的项目。

The first thing was editing the web.config in the Media folder like this: 第一件事是编辑Media文件夹中的web.config,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <appSettings>
    <add key="webpages:Enabled" value="false" />
  </appSettings>
  <system.web>
    <httpHandlers>
      <add path="*" verb="*" type="MediaFileAccess.AuthorizedMediaHandler" />
    </httpHandlers>
  </system.web>
  <system.webServer>
    <staticContent>
      <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" />
    </staticContent>
    <handlers accessPolicy="Script,Read">
      <add name="AuthorizedFile" path="*" verb="*"
           type="MediaFileAccess.AuthorizedMediaHandler,MediaFileAccess"
           preCondition="integratedMode" />
    </handlers>
  </system.webServer>
</configuration>

The handler which is registered in the web.config: 在web.config中注册的处理程序:

public class AuthorizedMediaHandler : IHttpHandler, IDependency
{
    private readonly IAuthenticationService _authenticationService;

    public AuthorizedMediaHandler(IAuthenticationService authenticationService)
    {
        _authenticationService = authenticationService;
    }

    public bool IsReusable { get { return false; } }

    public void ProcessRequest(HttpContext context)
    {
        // Do something using the injected services...
    }
}

When I navigate to a Media item url system throws MissingMethodException. 当我导航到媒体项目url时,系统抛出MissingMethodException。 That's normal because system cannot find the parameterless constructor. 这是正常的,因为系统找不到无参数的构造函数。

So, how can I inject the Orchard services into a IHttpHandler? 那么,如何将Orchard服务注入IHttpHandler?

Have you checked Glympse out? 您是否签出了Glympse? uses HttpHandlers, theres a SO question here . 使用HttpHandlers, 这里有一个SO问题。

there is a discussion here that got me thinking. 有一个讨论, 这里是我开始思考。

Get your web.configs working and it should be ok. 让您的web.configs工作正常,应该没问题。 i do not know about the IDependency bit, might not need it. 我不知道IDependency位,可能不需要它。

You will definitely not able to inject. 您肯定无法注入。 sorry,i omitted that in my answer earlier. 对不起,我在我早些时候的回答中省略了。

You can create a Orchard module that adds a filter or with a Controller that hijacks the route you specify and serves the file from somewhere actually safe. 您可以创建一个Orchard模块来添加过滤器,或者使用Controller劫持您指定的路由并从实际上安全的地方提供文件。

Orchard takes over IIS. Orchard接管了IIS。 that is why you need to add the staticfilemodule in the folder's web.config, to let IIS in and that is what makes it not secure. 这就是为什么您需要在文件夹的web.config中添加staticfilemodule以便让IIS进入的原因,这就是使其不安全的原因。

If you really need to go that road, perhaps a very quick service call to Orchard or talking directly to a db. 如果您确实需要走这条路,也许是非常快捷的服务呼叫Orchard或直接与数据库对话。

While you won't be able to inject dependencies like this you can still access Orchard'd DI container with the magic of IShim . 虽然你不会能够注入像这样的依赖关系,你仍然可以访问Orchard'd DI容器的魔力IShim Check out how eg OrchardLog4netLogger resolves dependencies from IOrchardHostContainer . 看看如何如OrchardLog4netLogger从解决依赖IOrchardHostContainer

Keep in mind though that this container is the application-wide one: to be able to resolve your standard IDependency types you'll need to first get the ShellContext from IOrchardHost , then create a WorkContextScope from its LifetimeScope as LifetimeScope.CreateWorkContextScope() . 请记住,尽管此容器是应用程序范围的容器:要解析您的标准IDependency类型,您首先需要从IOrchardHost获取ShellContext ,然后从其LifetimeScope创建一个WorkContextScope作为LifetimeScope.CreateWorkContextScope()

I know it seems complicated, and indeed there are a lot of hoops involved. 我知道这似乎很复杂,而且确实涉及很多方面。 But the bottom line is that this way you can resolve services from Orchard's (and its shells') DI containers in any class. 但最重要的是,通过这种方式,您可以解析Orchard(及其外壳)DI容器中任何类的服务。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM