Skip to content

Instantly share code, notes, and snippets.

@pawelpabich
Last active December 27, 2015 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pawelpabich/7371055 to your computer and use it in GitHub Desktop.
Save pawelpabich/7371055 to your computer and use it in GitHub Desktop.
It looks like Dynamic Proxy struggles with optional nullable value types
using System;
using Autofac;
using Autofac.Extras.DynamicProxy2;
using Castle.DynamicProxy;
namespace DynamicProxy
{
//1. Install-Package Autofac
//2. Install-Package Autofac.Extras.DynamicProxy2
//3. F5
class Program
{
static void Main(string[] args)
{
var builder = new ContainerBuilder();
builder.RegisterType<CallInterceptor>()
.AsSelf()
.InstancePerLifetimeScope();
builder.RegisterType<Work>()
.AsImplementedInterfaces()
.InstancePerLifetimeScope()
.EnableInterfaceInterceptors()
.InterceptedBy(typeof (CallInterceptor));
builder.RegisterType<DontWork>()
.AsImplementedInterfaces()
.InstancePerLifetimeScope()
.EnableInterfaceInterceptors()
.InterceptedBy(typeof(CallInterceptor));
var container = builder.Build();
//Works
container.Resolve<IWork>();
//Throws: An exception was thrown while executing a resolve operation. See the InnerException for details.
// ---> Null is not a valid constant value for this type.
container.Resolve<IDontWork>();
Console.WriteLine("All Good");
}
}
public interface IWork
{
int Method(int? value2, string value3 = null);
}
public class Work : IWork
{
public int Method(int? value2, string value3 = null)
{
return 0;
}
}
public interface IDontWork
{
int Method(int? value2 = null); //Change null to 5 and everything works
}
public class DontWork : IDontWork
{
public int Method(int? value2 = null)
{
return 0;
}
}
public class CallInterceptor: IInterceptor
{
public void Intercept(IInvocation invocation)
{
invocation.Proceed();
}
}
}
at System.Reflection.Emit.TypeBuilder.SetConstantValue(ModuleBuilder module, Int32 tk, Type destType, Object value)
at System.Reflection.Emit.ParameterBuilder.SetConstant(Object defaultValue)
at Castle.DynamicProxy.Generators.Emitters.MethodEmitter.DefineParameters(ParameterInfo[] parameters)
at Castle.DynamicProxy.Generators.Emitters.MethodEmitter..ctor(AbstractTypeEmitter owner, String name, MethodAttributes attributes, MethodInfo methodToUseAsATemplate)
at Castle.DynamicProxy.Generators.Emitters.AbstractTypeEmitter.CreateMethod(String name, MethodAttributes attributes, MethodInfo methodToUseAsATemplate)
at Castle.DynamicProxy.Generators.MethodGenerator.Generate(ClassEmitter class, ProxyGenerationOptions options, INamingScope namingScope)
at Castle.DynamicProxy.Contributors.CompositeTypeContributor.ImplementMethod(MetaMethod method, ClassEmitter class, ProxyGenerationOptions options, OverrideMethodDelegate overrideMethod)
at Castle.DynamicProxy.Contributors.CompositeTypeContributor.Generate(ClassEmitter class, ProxyGenerationOptions options)
at Castle.DynamicProxy.Generators.InterfaceProxyWithTargetGenerator.GenerateType(String typeName, Type proxyTargetType, Type[] interfaces, INamingScope namingScope)
at Castle.DynamicProxy.Generators.InterfaceProxyWithTargetGenerator.<>c__DisplayClass1.<GenerateCode>b__0(String n, INamingScope s)
at Castle.DynamicProxy.Generators.BaseProxyGenerator.ObtainProxyType(CacheKey cacheKey, Func`3 factory)
at Castle.DynamicProxy.Generators.InterfaceProxyWithTargetGenerator.GenerateCode(Type proxyTargetType, Type[] interfaces, ProxyGenerationOptions options)
at Castle.DynamicProxy.DefaultProxyBuilder.CreateInterfaceProxyTypeWithTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy, Type targetType, ProxyGenerationOptions options)
at Castle.DynamicProxy.ProxyGenerator.CreateInterfaceProxyTypeWithTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy, Type targetType, ProxyGenerationOptions options)
at Castle.DynamicProxy.ProxyGenerator.CreateInterfaceProxyWithTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy, Object target, ProxyGenerationOptions options, IInterceptor[] interceptors)
at Castle.DynamicProxy.ProxyGenerator.CreateInterfaceProxyWithTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy, Object target, IInterceptor[] interceptors)
at Autofac.Extras.DynamicProxy2.RegistrationExtensions.<EnableInterfaceInterceptors>b__7[TLimit,TActivatorData,TSingleRegistrationStyle](Object sender, ActivatingEventArgs`1 e)
at Autofac.Core.Registration.ComponentRegistration.RaiseActivating(IComponentContext context, IEnumerable`1 parameters, Object& instance)
at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)
at Autofac.Core.Resolving.InstanceLookup.<Execute>b__0()
at Autofac.Core.Lifetime.LifetimeScope.GetOrCreateAndShare(Guid id, Func`1 creator)
at Autofac.Core.Resolving.InstanceLookup.Execute()
at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment