Work with universal lifecycle managers in the configuration configuration section

I have the following general life time 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 can I refer to this in the unity configuration section. Creating Type Alias

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

and pointing him out as a life 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 refer to common lifecycle managers?

+3
source share
1 answer

You cannot use type aliases when referring to a generic type; you need to explicitly refer to types. It works now

    <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>
+4
source

Source: https://habr.com/ru/post/1743125/


All Articles