zoukankan      html  css  js  c++  java
  • 使用非ServiceDependency方法获得模块中已注册的服务

    WCSF中注入的服务最终都是存在OB中, 在View, Presenter和Controller中可用通过[ServiceDependency]修饰来引入使用.

    通过分析CompositeWeb的源码, 这里给出一种直接获得当前模块中服务的方法, 当然, 一般项目中的模块都能使用[ServiceDependency].

    public static object GetInjectService(Type serviceType, IHttpContext context)
    {
        object service = null;
        if (context.ApplicationInstance is WebClientApplication)
        {
            WebClientApplication app = (WebClientApplication)context.ApplicationInstance;

            // 获得用来加载模块的服务.
            IModuleContainerLocatorService locatorService = app.RootContainer.Services.Get<IModuleContainerLocatorService>();
            // 获得当前模块中的container.
            CompositionContainer container = locatorService.GetContainer(context.Request.AppRelativeCurrentExecutionFilePath);

            // 从container中获得服务.
            service = container.Services.Get(serviceType);
        }
        return service;
    }

    若对于当前用户, IHttpContext接口可这样得到:

    IHttpContext context = new Microsoft.Practices.CompositeWeb.Web.HttpContext(System.Web.HttpContext.Current)

  • 相关阅读:
    opencv目录
    qt5-编译并添加opencv库
    java版gRPC实战之二:服务发布和调用
    java版gRPC实战之一:用proto生成代码
    github搜索技巧小结
    client-go实战之五:DiscoveryClient
    client-go实战之四:dynamicClient
    client-go实战之三:Clientset
    client-go实战之二:RESTClient
    client-go实战之一:准备工作
  • 原文地址:https://www.cnblogs.com/MolbyHome/p/1570529.html
Copyright © 2011-2022 走看看