Last active
August 29, 2015 14:23
-
-
Save micklaw/d46143c4fc23f37158b4 to your computer and use it in GitHub Desktop.
InvalidProgramException - 'Common Language Runtime detected an invalid program.' IIS Express only
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Generating a proxy of a Type and overriding a virtual property with this method blows up in IIs Express, but works fine in unit tests (nunit, resharper) and | |
| local IIS. This is using VS Community 2013 on Windows 8 */ | |
| public override T OverrideProperty<T>(string propertyName, Type attribute = null, Type [] constructorParams = null, object [] constructorValues = null) | |
| { | |
| // [ML] - Default to an empty instance | |
| constructorParams = constructorParams ?? Type.EmptyTypes; | |
| constructorValues = constructorValues ?? new object[] { null }; | |
| var type = typeof(T); | |
| // [ML] - Proxy the class | |
| var proxyNamespace = new AssemblyName(this.GetType().Namespace + ".Proxied"); | |
| var assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(proxyNamespace, AssemblyBuilderAccess.Run); | |
| var module = assembly.DefineDynamicModule(proxyNamespace.Name); | |
| var proxyType = module.DefineType(type.Name + "Proxy", TypeAttributes.Public, type); | |
| // [ML] - Override the virtual converted value | |
| var oldProp = type.GetProperty(propertyName); | |
| if (oldProp != null && oldProp.GetGetMethod().IsVirtual) | |
| { | |
| var newProp = proxyType.DefineProperty(oldProp.Name, oldProp.Attributes, oldProp.PropertyType, null); | |
| // [ML] - Add the custom attribute | |
| if (attribute != null) | |
| { | |
| var ctor = attribute.GetConstructor(constructorParams); | |
| if (ctor != null) | |
| { | |
| newProp.SetCustomAttribute(new CustomAttributeBuilder(ctor , constructorValues, new FieldInfo[0], new object[0])); | |
| } | |
| } | |
| // [ML] - Define a placeholder for a getter and setter of the new override property on the proxy class | |
| var attributes = MethodAttributes.Virtual | MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig; | |
| var pGet = proxyType.DefineMethod("get_" + oldProp.Name, attributes, oldProp.PropertyType,Type.EmptyTypes); | |
| var pSet = proxyType.DefineMethod("set_" + oldProp.Name, attributes, null, new[] {oldProp.PropertyType}); | |
| // [ML] - Give the getter a method body | |
| ILGenerator pILGet = pGet.GetILGenerator(); | |
| pILGet.Emit(OpCodes.Ldarg_0); | |
| pILGet.Emit(OpCodes.Call, oldProp.GetGetMethod()); | |
| pILGet.Emit(OpCodes.Ret); | |
| // [ML] - Give the setter a method body | |
| ILGenerator pILSet = pSet.GetILGenerator(); | |
| pILSet.Emit(OpCodes.Ldarg_0); | |
| pILSet.Emit(OpCodes.Ldarg_1); | |
| pILSet.Emit(OpCodes.Call, oldProp.GetSetMethod()); | |
| pILSet.Emit(OpCodes.Ldarg_0); | |
| pILSet.Emit(OpCodes.Ldstr, oldProp.Name); | |
| // [ML] - Setter and getter for new overriden property on proxy class | |
| newProp.SetSetMethod(pSet); | |
| newProp.SetGetMethod(pGet); | |
| } | |
| // [ML] - Create and return a an instance | |
| var newType = proxyType.CreateType(); | |
| return (T)Activator.CreateInstance(newType); | |
| } | |
| /* When I try to set the value of the overriden property using the code below, this blows up in IIS express with an InvalidProgramException | |
| and the message 'Common Language Runtime detected an invalid program.'. Stack trace gives nothing away but pasted at bottom anyway */ | |
| ... | |
| var proxy = _emitter.OverrideProperty<T>("ConvertedValue", attribute, ctorParams, ctorValues); | |
| var proxyType = proxy.GetType(); | |
| var proxyObject = JsonConvert.DeserializeObject("A json string", proxyType) as T; | |
| var convertProperyInfo = proxyObject.GetType().GetProperty("ConvertedValue"); | |
| proxyObject.ConvertedValue = 1; | |
| ... | |
| /* This may be my complete lack of fully understanding opcodes, but just seems weird it works in IIS and Unit tests, but not in IIS Express. | |
| I think i remember reading somewhere that IIS doesnt propogate all exception regarding the JIT compiler, but have no evidence to back this up, feels like a bug with IIS Express. | |
| [InvalidProgramException: Common Language Runtime detected an invalid program.] | |
| ControlProxy.set_ConvertedValue(Object ) +0 | |
| Our.Umbraco.Ditto.Resolvers.Grid.Converters.ControlConverter`1.Create(JObject jObject) in c:\GIT\mlwd\Ditto.Resolvers\Our.Umbraco.Ditto.Resolvers.Grid\Converters\ControlConverter.cs:70 | |
| Our.Umbraco.Ditto.Resolvers.Grid.Converters.ControlConverter`1.ReadJson(JsonReader reader, Type objectType, Object existingValue, JsonSerializer serializer) in c:\GIT\mlwd\Ditto.Resolvers\Our.Umbraco.Ditto.Resolvers.Grid\Converters\ControlConverter.cs:86 | |
| Newtonsoft.Json.Serialization.JsonSerializerInternalReader.DeserializeConvertable(JsonConverter converter, JsonReader reader, Type objectType, Object existingValue) +292 | |
| Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id) +1357 | |
| Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id) +835 | |
| Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) +238 | |
| Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target) +599 | |
| Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id) +1900 | |
| Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) +1208 | |
| Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) +189 | |
| Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id) +1357 | |
| Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id) +835 | |
| Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) +238 | |
| Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target) +599 | |
| Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id) +1900 | |
| Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) +1208 | |
| Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) +189 | |
| Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id) +1357 | |
| Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id) +835 | |
| Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) +238 | |
| Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target) +599 | |
| Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id) +1900 | |
| Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) +1208 | |
| Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) +189 | |
| Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id) +1357 | |
| Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id) +835 | |
| Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) +238 | |
| Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target) +599 | |
| Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id) +1900 | |
| Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) +1208 | |
| Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) +189 | |
| Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) +749 | |
| Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) +354 | |
| Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType) +47 | |
| Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings) +199 | |
| Newtonsoft.Json.JsonConvert.DeserializeObject(String value, JsonSerializerSettings settings) +118 | |
| Our.Umbraco.Ditto.Resolvers.Grid.Resolvers.GridValueResolver`1.ResolveValue(ITypeDescriptorContext context, GridResolverAttribute attribute, CultureInfo culture) in c:\GIT\mlwd\Ditto.Resolvers\Our.Umbraco.Ditto.Resolvers.Grid\Resolvers\GridValueResolver.cs:41 | |
| Our.Umbraco.Ditto.DittoValueResolver`1.ResolveValue(ITypeDescriptorContext context, DittoValueResolverAttribute attribute, CultureInfo culture) +304 | |
| Our.Umbraco.Ditto.PublishedContentExtensions.GetRawValue(IPublishedContent content, CultureInfo culture, PropertyInfo propertyInfo, Object instance) +419 | |
| Our.Umbraco.Ditto.PublishedContentExtensions.GetTypedProperty(IPublishedContent content, Type type, CultureInfo culture) +2621 | |
| Our.Umbraco.Ditto.PublishedContentExtensions.As(IPublishedContent content, Type type, Action`1 convertingType, Action`1 convertedType, CultureInfo culture) +318 | |
| Our.Umbraco.Ditto.PublishedContentExtensions.As(IPublishedContent content, Action`1 convertingType, Action`1 convertedType, CultureInfo culture) +128 | |
| Our.Umbraco.Ditto.RenderModelExtensions.As(RenderModel model, Action`1 convertingType, Action`1 convertedType) +286 | |
| Ditto.Resolvers.Sample.Controllers.TextPageController.Index(RenderModel model) in c:\GIT\mlwd\Ditto.Resolvers\Ditto.Resolvers.Sample\Controllers\TextPageController.cs:18 | |
| lambda_method(Closure , ControllerBase , Object[] ) +159 | |
| System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14 | |
| System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +208 | |
| System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27 | |
| System.Web.Mvc.Async.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() +28 | |
| System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +10 | |
| System.Web.Mvc.Async.WrappedAsyncResult`1.End() +57 | |
| System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +48 | |
| System.Web.Mvc.Async.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() +57 | |
| System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +223 | |
| System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +223 | |
| System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +223 | |
| System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) +10 | |
| System.Web.Mvc.Async.WrappedAsyncResult`1.End() +57 | |
| System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +48 | |
| System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +24 | |
| System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +102 | |
| System.Web.Mvc.Async.WrappedAsyncResult`1.End() +57 | |
| System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +43 | |
| System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +14 | |
| System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23 | |
| System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62 | |
| System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +57 | |
| System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23 | |
| System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62 | |
| System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +47 | |
| System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10 | |
| System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +25 | |
| System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23 | |
| System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62 | |
| System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +47 | |
| System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9 | |
| System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9651688 | |
| System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155 */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment