You can use two versions in the same executable, as long as you configure it in the app.config or web.config (the assembled and signed thing is yours, look at the attribute publicKeyToken
of assemblyIdentity
and set the assemblies in a subfolder of bin
each with its version.
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Factura.API" publicKeyToken="12ab3cd4e5f6abcd"
culture="neutral" />
<codeBase version="1.0.0.0" href="v1\Factura.API.dll" />
<codeBase version="2.0.0.0" href="v2\Factura.API.v2.dll" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
Then you should create two AppDomain
different and load in each of them the specific version. To redirect the specified assembly (through the full path) you must implement a "proxy" class. This way you can distinguish in your code the version you want to use at all times.
An example can be
Module Module1
Sub Main()
Dim myAppDomainSetup As New AppDomainSetup()
myAppDomainSetup.ApplicationBase = System.Environment.CurrentDirectory
Dim myEvidence As Evidence = AppDomain.CurrentDomain.Evidence
Dim myDomain As AppDomain = AppDomain.CreateDomain("MyDomain", myEvidence, myAppDomainSetup)
Dim myDomain2 As AppDomain = AppDomain.CreateDomain("MyDomain2", myEvidence, myAppDomainSetup)
Dim type As Type = GetType(Proxy)
Dim proxy = DirectCast(myDomain.CreateInstanceAndUnwrap(type.Assembly.FullName, type.FullName), Proxy)
Dim pathToDll = System.IO.Path.Combine(myAppDomainSetup.ApplicationBase, "v1\Factura.API.dll")
Dim pathToDll2 = System.IO.Path.Combine(myAppDomainSetup.ApplicationBase, "v2\Factura.API.v2.dll")
Dim assembly = proxy.GetAssembly(pathToDll)
Dim assembly2 = proxy.GetAssembly(pathToDll2)
Dim typeFacturacion1 As Type = assembly.GetType("Factura.API.Tipo1", True)
Dim facturacion1 = Activator.CreateInstance(typeFacturacion1)
Dim typeFacturacion2 As Type = assembly2.GetType("Factura.API.Tipo1", True)
Dim facturacion2 = Activator.CreateInstance(typeFacturacion2)
AppDomain.Unload(myDomain)
AppDomain.Unload(myDomain2)
Console.ReadKey()
End Sub
End Module
Public Class Proxy
Inherits MarshalByRefObject
Public Function GetAssembly(assemblyPath As String) As Assembly
Try
Return Assembly.LoadFile(assemblyPath)
Catch generatedExceptionName As Exception
' throw new InvalidOperationException(ex);
Return Nothing
End Try
End Function
End Class