在统一配置部分使用通用生命周期管理器

发布于 2024-08-30 08:52:12 字数 1161 浏览 6 评论 0原文

我有以下通用生命周期管理器

    public class RequestLifetimeManager<T> : LifetimeManager, IDisposable
    {
    public override object GetValue()
    {
        return HttpContext.Current.Items[typeof(T).AssemblyQualifiedName];
    }
    public override void RemoveValue()
    {
        HttpContext.Current.Items.Remove(typeof(T).AssemblyQualifiedName);
    }
    public override void SetValue(object newValue)
    {
        HttpContext.Current.Items[typeof(T).AssemblyQualifiedName] = newValue;
    }
    public void Dispose()
    {
        RemoveValue();
    }
}

如何在统一配置部分引用它。创建类型别名

<typeAlias alias="requestLifeTimeManager`1" type=" UI.Common.Unity.RequestLifetimeManager`1,  UI.Common" />

并将其指定为生命周期管理器

   <types>
    <type type="[interface]" mapTo="[concretetype]" >
      <lifetime type="requestLifeTimeManager`1"  />
    </type>
  </types>

会导致以下错误

Cannot create an instance of UI.Common.Unity.RequestLifetimeManager`1[T] because Type.ContainsGenericParameters is true. 

如何引用通用生命周期管理器?

I have the following generic lifetime manager

    public class RequestLifetimeManager<T> : LifetimeManager, IDisposable
    {
    public override object GetValue()
    {
        return HttpContext.Current.Items[typeof(T).AssemblyQualifiedName];
    }
    public override void RemoveValue()
    {
        HttpContext.Current.Items.Remove(typeof(T).AssemblyQualifiedName);
    }
    public override void SetValue(object newValue)
    {
        HttpContext.Current.Items[typeof(T).AssemblyQualifiedName] = newValue;
    }
    public void Dispose()
    {
        RemoveValue();
    }
}

How do I reference this in the unity config section. Creating a type alias

<typeAlias alias="requestLifeTimeManager`1" type=" UI.Common.Unity.RequestLifetimeManager`1,  UI.Common" />

and specifying it as a lifetime manager

   <types>
    <type type="[interface]" mapTo="[concretetype]" >
      <lifetime type="requestLifeTimeManager`1"  />
    </type>
  </types>

causes the following error

Cannot create an instance of UI.Common.Unity.RequestLifetimeManager`1[T] because Type.ContainsGenericParameters is true. 

How do you reference generic lifetime managers ?

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

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

发布评论

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

评论(1

心在旅行 2024-09-06 08:52:12

引用泛型类型时不能使用类型别名,必须显式引用类型。现在可以使用以下内容

    <container name="defaultContainer">
  <types>
    <type type="ILayoutManager" mapTo="LayoutManager" >
      <lifetime  type="Publishing.UI.Common.Unity.RequestLifetimeManager`1[[Publishing.BLL.Managers.LayoutManager, Publishing.BLL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c02010e20f60e4d2]], Publishing.UI.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c02010e20f60e4d2"  />
    </type>
  </types>
</container>

You cannot use the type aliases when referencing generic type, you have to reference the types explicitly. The following now works

    <container name="defaultContainer">
  <types>
    <type type="ILayoutManager" mapTo="LayoutManager" >
      <lifetime  type="Publishing.UI.Common.Unity.RequestLifetimeManager`1[[Publishing.BLL.Managers.LayoutManager, Publishing.BLL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c02010e20f60e4d2]], Publishing.UI.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c02010e20f60e4d2"  />
    </type>
  </types>
</container>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文