AppDomain.AssemblyResolve Event


Occurs when the resolution of an assembly fails.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

Public Event AssemblyResolve As ResolveEventHandler

It is the responsibility of the ResolveEventHandler for this event to return the assembly that is specified by the ResolveEventArgs.Name property, or to return null if the assembly is not recognized. The assembly must be loaded into an execution context; if it is loaded into the reflection-only context, the load that caused this event to be raised fails.

For guidance on the use of this event, see Resolving Assembly Loads.

Beginning with the .NET Framework 4, the ResolveEventArgs.RequestingAssembly property returns the assembly that requested the assembly load that could not be resolved. For example, the loader might be unable to load a dependency of the requesting assembly because the requesting assembly and its dependency are not in the probing path. Knowing the identity of the requesting assembly might be useful in locating the dependency or in identifying the correct version, if more than one version of the dependency is available. For more information, see ResolveEventArgs.RequestingAssembly.


Beginning with the .NET Framework 4, the ResolveEventHandler event is raised for all assemblies, including resource assemblies. In earlier versions, the event was not raised for resource assemblies. If the operating system is localized, the handler might be called multiple times: once for each culture in the fallback chain.

For this event, the ResolveEventArgs.Name property returns the assembly name before policy is applied.


If more than one event handler is registered for this event, the event handlersarecalled in order until an event handler returns a value that isn't null. Subsequent event handlers are ignored.

For more information about handling events, see Handling and Raising Events.

The following sample demonstrates the AssemblyResolve event.

For this code example to run, you must provide the fully qualified assembly name. For information about how to obtain the fully qualified assembly name, see Assembly Names.

Public Class MyType

    Public Sub New()
        Console.WriteLine("MyType instantiated!")
    End Sub 'NewNew

End Class 'MyType

Class Test

    Public Shared Sub Main()
        Dim currentDomain As AppDomain = AppDomain.CurrentDomain

        ' This call will fail to create an instance of MyType since the
        ' assembly resolver is not set

        AddHandler currentDomain.AssemblyResolve, AddressOf MyResolveEventHandler

        ' This call will succeed in creating an instance of MyType since the
        ' assembly resolver is now set.

        ' This call will succeed in creating an instance of MyType since the
        ' assembly name is valid.
    End Sub 'Main

    Private Shared Sub InstantiateMyTypeFail(domain As AppDomain)
        ' Calling InstantiateMyType will always fail since the assembly info
        ' given to CreateInstance is invalid.
            ' You must supply a valid fully qualified assembly name here.
            domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyType")
        Catch e As Exception
        End Try
    End Sub 'InstantiateMyType

    Private Shared Sub InstantiateMyTypeSucceed(domain As AppDomain)
            Dim asmname As String = Assembly.GetCallingAssembly().FullName
            domain.CreateInstance(asmname, "MyType")
        Catch e As Exception
        End Try
    End Sub

    Private Shared Function MyResolveEventHandler(sender As Object, args As ResolveEventArgs) As Assembly
        Return GetType(MyType).Assembly
    End Function 'MyResolveEventHandler

End Class 'Test


Requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.

.NET Framework
Available since 1.1
Available since 2.0
Return to top