0

I am generating my own proxy that wraps objects returned from MongoDB. The proxy implements an interface:

interface IProxy
{
    string __ID {get;}
}

The Proxy Generator uses the following code to generate the implementation

PropertyBuilder proxyID = typeBuilder.DefineProperty("__ID", PropertyAttributes.None, typeof(string), null);
proxyID.SetCustomAttribute(new CustomAttributeBuilder(typeof(Root.Attributes.DataAnnotations.NotMappedAttribute).GetConstructor(Type.EmptyTypes), new object[0]));
 MethodBuilder proxyID_PropertyGet = typeBuilder.DefineMethod("get___ID", MethodAttributes.Virtual | MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig, typeof(string), Type.EmptyTypes);
 ILGenerator __IDILget = proxyID_PropertyGet.GetILGenerator();
 __IDILget.Emit(OpCodes.Ldarg_0);
 __IDILget.Emit(OpCodes.Ldfld, objectID);
 __IDILget.Emit(OpCodes.Callvirt, typeof(ObjectId).GetMethod("ToString", BindingFlags.Public | BindingFlags.Instance));
 __IDILget.Emit(OpCodes.Ret);
 proxyID.SetGetMethod(proxyID_PropertyGet);

The application is compiled with the "Any CPU" configuration.

When running on our development machines using the Visual Studio web server, the application works fine.

When running on IIS7.5 (Windows 2008 R2), it throws an invalid program exception whenever the __ID property is accessed. Changing the "Enable 32-bit applications" setting to true changes this behavior.

I'd like to not have to modify the IIS configuration. Why does the exception only get thrown in 64-bit applications?

svick
  • 236,525
  • 50
  • 385
  • 514
Joe Enzminger
  • 11,110
  • 3
  • 50
  • 75
  • When creating the `AssemblyBuilder` you can specify this. (Or `ModuleBuilder`, cant recall now) – leppie Sep 06 '13 at 20:56
  • @leppie I think you specify it when saving: [there is an overload of `Save()` for that](http://msdn.microsoft.com/en-us/library/ms145516.aspx). Though the parameters seem confusing to me, I'm not sure what combination corresponds to “Any CPU”. – svick Sep 06 '13 at 22:01
  • @svick: I know it is 'AnyCPU' for IronScheme. Will have to check what I have or did :) – leppie Sep 07 '13 at 05:58
  • @Joe, did you test the result with [PEVerify](http://msdn.microsoft.com/ru-ru/library/62bwd2yd.aspx)? – Jury Soldatenkov Sep 08 '13 at 06:41

1 Answers1

0

It turns out that the field ToString() is called on is a struct, so the IL had to change to:

ILGenerator __IDILget = proxyID_PropertyGet.GetILGenerator();
__IDILget.Emit(OpCodes.Ldarg_0);
__IDILget.Emit(OpCodes.Ldflda, objectID);
__IDILget.Emit(OpCodes.Constrained, typeof(ObjectId));
__IDILget.Emit(OpCodes.Callvirt, typeof(ObjectId).GetMethod("ToString", BindingFlags.Public | BindingFlags.Instance));
__IDILget.Emit(OpCodes.Ret);
proxyID.SetGetMethod(proxyID_PropertyGet);
Joe Enzminger
  • 11,110
  • 3
  • 50
  • 75