.NET Framework Class Library
AppDomain..::.AssemblyResolve Event

Updated: December 2009

Occurs when the resolution of an assembly fails.

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

Visual Basic (Declaration)
Public Event AssemblyResolve As ResolveEventHandler
Visual Basic (Usage)
Dim instance As AppDomain
Dim handler As ResolveEventHandler

AddHandler instance.AssemblyResolve, handler
C#
public event ResolveEventHandler AssemblyResolve
Visual C++
public:
virtual  event ResolveEventHandler^ AssemblyResolve {
    void add (ResolveEventHandler^ value);
    void remove (ResolveEventHandler^ value);
}
JScript
JScript does not support events.

Implements

_AppDomain..::.AssemblyResolve
Remarks

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

It is the responsibility of ResolveEventHandler for this event to return the assembly that resolves the type, assembly, or resource. 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.

To register an event handler for this event, you must have the required permissions, or a SecurityException is thrown.

For more information about handling events, see Consuming Events.

Examples

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.

Visual Basic
Public Class MyType

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

End Class 'MyType

Module Test

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

      InstantiateMyType(currentDomain)    ' Failed!

      AddHandler currentDomain.AssemblyResolve, AddressOf MyResolveEventHandler

      InstantiateMyType(currentDomain)    ' OK!
   End Sub 'Main

   Sub InstantiateMyType(domain As AppDomain)
      Try
     ' You must supply a valid fully qualified assembly name here.
         domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyType")
      Catch e As Exception
         Console.WriteLine(e.Message)
      End Try
   End Sub 'InstantiateMyType

   Function MyResolveEventHandler(sender As Object, args As ResolveEventArgs) As [Assembly]
      Console.WriteLine("Resolving...")
      Return GetType(MyType).Assembly
   End Function 'MyResolveEventHandler

End Module 'Test
C#
public class MyType {
   public MyType() {
      Console.WriteLine("MyType instantiated!");
   }
}

class Test {
   public static void Main() {
      AppDomain currentDomain = AppDomain.CurrentDomain;

      InstantiateMyType(currentDomain);    // Failed!

      currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);

      InstantiateMyType(currentDomain);    // OK!
   }

   static void InstantiateMyType(AppDomain domain) {
      try {
     // You must supply a valid fully qualified assembly name here.
         domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyType");
      } catch (Exception e) {
         Console.WriteLine(e.Message);
      }
   }

   static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args) {
      Console.WriteLine("Resolving...");
      return typeof(MyType).Assembly;
   }
}
Visual C++
public ref class MyType
{
public:
   MyType()
   {
      Console::WriteLine( "MyType instantiated!" );
   }

};

ref class Test
{
public:
   static void InstantiateMyType( AppDomain^ domain )
   {
      try
      {
         // You must supply a valid fully qualified assembly name here.
         domain->CreateInstance( "Assembly text name, Version, Culture, PublicKeyToken", "MyType" );
      }
      catch ( Exception^ e ) 
      {
         Console::WriteLine( e->Message );
      }
   }

   static Assembly^ MyResolveEventHandler( Object^ sender, ResolveEventArgs^ args )
   {
      Console::WriteLine( "Resolving..." );
      return MyType::typeid->Assembly;
   }
};

int main()
{
   AppDomain^ currentDomain = AppDomain::CurrentDomain;
   Test::InstantiateMyType( currentDomain ); // Failed!
   currentDomain->AssemblyResolve += gcnew ResolveEventHandler( Test::MyResolveEventHandler );
   Test::InstantiateMyType( currentDomain ); // OK!
}
.NET Framework Security

Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
See Also

Reference

Change History

Date

History

Reason

December 2009

Clarification: The returned assembly must be loaded into an execution context, not the reflection-only context.

Customer feedback.

Tags :


Community Content

Indrajeet Valera
See my 'RemoteAppDeployment' article on www.codeproject.com
For a fully working & reusable example of AssemblyResolve in C#, see my article on www.codeproject.com:

Deploy .NET thick client application assemblies to remote server
By Indrajeet Valera

http://www.codeproject.com/KB/miscctrl/RemoteAppDeployment.aspx

Uses .NET Remoting, AppDomain.CurrentDomain.Load as well.

Enjoy,
Indar
Tags :

j2associates
For VB.Net users, you must clear the Root namespace in My Project

Hello all,

If you are using the VB.Net code, you must clear the Root namespace in My Project or the code as given above will not work.

Tags :

Page view tracker