Subscribe

RSS Feed (xml)

Avoid Loading Unnecessary Assemblies into Application Domains

When you pass a marshal-by-value (MBV) object across application domain boundaries, the runtime creates a new instance of that object in the destination application domain. This means that the runtime must load the assembly containing that type metadata into the application domain. Passing MBV references across intermediate application domains can result in the runtime loading unnecessary assemblies into application domains. Once loaded, these superfluous assemblies can't be unloaded without unloading the containing application domain.
The ObjectHandle class allows you to wrap an object reference so that you can pass it between application domains without the runtime loading additional assemblies. When the object reaches the destination application domain, you can unwrap the object reference, causing the runtime to load the required assembly and allowing you to access the object as normal. To wrap an object (such as a System.Data.DataSet), use the following statement:// Create a new DataSet. System.Data.DataSet data1 = new System.Data.DataSet(); // Configure/populate the DataSet. § // Wrap the DataSet. System.Runtime.Remoting.ObjectHandle objHandle = new System.Runtime.Remoting.ObjectHandle(data1); To unwrap the object, use the ObjectHandle.Unwrap method and cast the returned object to the correct type, as shown here. // Unwrap the DataSet System.Data.DataSet data2 = (System.Data.DataSet)objHandle.Unwrap();

No comments:

Post a Comment

Archives

LocalsAdda.com-Variety In Web World

Fun Mail - Fun in the Mail