mercredi 26 septembre 2018

Autofac - how to register 2 different version of the same type with the same name

Looking for some help on this. Would be much appreciated.

What I'm trying to accomplish is registering multiple versions of a type to maintain backwards compatibility on an API. This API will allow operations to be executed using older versions of the code.

My code does the following to accomplish this:

  1. Load each version of each DLL's types into memory.

    foreach (var directory in Directories)
    {
        assembliesToLoad.AddRange(directory.EnumerateFiles("*.dll").Select(file => Assembly.LoadFile(file.FullName)));
    }
    foreach (var assembly in assembliesToLoad)
    {
        RegisterActivityTypesFromAssembly(assembly);
    }
    
    
  2. Register them using Autofac in a loop.

    var type = value.Key;
    var version = $"{value.Value.Major}.{value.Value.Minor}.{value.Value.Build}";
    var typeId = $"{keyValuePair.Key}@{version}";
    if (type != null)
    {
        foreach (var interfaceType in type.GetInterfaces())
        {
            Builder.RegisterType(type).Named(typeId, interfaceType);
        }
    }
    
    
  3. Then I load it later in the pipeline based on the version specified in the API.

    var autofacTypeId = $"{_typeId}@{_version}";
    _activity = Scope.ResolveNamed<IActivity>(autofacTypeId);
    
    

I've noticed this code will resolve the current version of the type no problem. Attempting to resolve older versions it fails on. What am I doing wrong here? It seems like the older version types go away for some reason, even though during the loading phase they seem to be loaded just fine after reflection.

Any help would be greatly appreciated.





Aucun commentaire:

Enregistrer un commentaire