2

I have a very odd problem where I am unable to load a generic type into another AppDomain when running inside of ASP.NET, but it works in my Test project and a Console app.

The relevant code does essentially this:

Assembly ass = Assembly.GetExecutingAssembly();
string AssemblyPath = ass.Location;

var templateType = typeof(RazorEngine<RazorTemplateBase>);

object instance = LocalAppDomain.CreateInstanceFrom(AssemblyPath,
                                             templateType.FullName)
                                .Unwrap();

The code bombs on the last line. The AppDomain exists and its base execution and bin paths are matched to the original AppDomain.

When running through this code in a test project, it works fine. The instance is created (as a TransparentProxy remoting reference) and all is well.

When running in ASP.NET I get a type error on the generic type parameter:

GenericArguments[0], 'Westwind.RazorHosting.RazorTemplateBase', on 'Westwind.RazorHosting.RazorEngine`1[TBaseTemplateType]' violates the constraint of type 'TBaseTemplateType'.

where TBaseTemplateType is the same type passed in (ie. RazorTemplateBase) with RazorEngine defined like this:

public class RazorEngine<TBaseTemplateType> : MarshalByRefObject
    where TBaseTemplateType : RazorTemplateBase

Clearly I'm passing the right type since it is the actual restraining type, but somehow the AppDomain is not recognizing the Generic type as the same.

As I mentioned it works inside of a Windows application and a test project, but inside of ASP.NET the generic type won't instantiate across AppDomain boundaries. Using the same code to create a non-generic type (even the same non-generic version of this type RazorHosting that inherits from RazorHosting) and it works fine.

Stumped. What could ASP.NET be doing to make this behave so differently than when running in a test project?

Rick Strahl
  • 17,302
  • 14
  • 89
  • 134

1 Answers1

0

I have encountered this strange issue before. It seems to come down to a combination of Shadow Copy (which ASP.NET uses by default), each AppDomain's ApplicationBase and Load Contexts.

It's been a while since I've had this problem in my brain but check out my answer to this question to see if that helps:

Domain problems

Community
  • 1
  • 1
Russell McClure
  • 4,821
  • 1
  • 22
  • 22