structuremap / StructureMap.DynamicInterception

OBSOLETE, see the main StructureMap repository
Apache License 2.0
3 stars 2 forks source link

The example in the documentation doesn't work #8

Closed thomaslevesque closed 7 years ago

thomaslevesque commented 7 years ago

I copied the example from the documentation in LINQPad, except the test method which I adapted as follows:

async Task Main()
{
    try
    {
        var container = new Container(x =>
        {
            x.For<IMathService>().Use<MathService>()
                .InterceptWith(new DynamicProxyInterceptor<IMathService>(new IInterceptionBehavior[]
                {
                    new NegatingInterceptor(),
                    new AsyncCachingInterceptor()
                }));
        });

        var service = container.GetInstance<IMathService>();

        Console.WriteLine(service.GetSquareRoot(16));
        Console.WriteLine(await service.GetSquareRootAsync(81));
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

And I got the following exception (it is thrown on container.GetInstance<IMathService>()):

StructureMap.Building.StructureMapInterceptorException: Decorator Interceptor failed during object construction.  See the inner exception
1.) DynamicProxyInterceptor of UserQuery+IMathService with interception behaviors: UserQuery+NegatingInterceptor, UserQuery+AsyncCachingInterceptor
2.) UserQuery+MathService
3.) Instance of UserQuery+IMathService (UserQuery+MathService)
4.) Container.GetInstance<UserQuery+IMathService>()
 ---> System.TypeInitializationException: The type initializer for 'StructureMap.DynamicInterception.DynamicProxyInterceptor`1' threw an exception. ---> System.MethodAccessException: Attempt by security transparent method 'StructureMap.DynamicInterception.DynamicProxyInterceptor`1<TPluginType>..cctor()' to access security critical method 'Castle.DynamicProxy.ProxyGenerator..ctor()' failed.

Assembly 'StructureMap.DynamicInterception, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is marked with the AllowPartiallyTrustedCallersAttribute, and uses the level 2 security transparency model.  Level 2 transparency causes all methods in AllowPartiallyTrustedCallers assemblies to become security transparent by default, which may be the cause of this exception.
   at StructureMap.DynamicInterception.DynamicProxyInterceptor`1..cctor()
   --- End of inner exception stack trace ---
   at lambda_method(Closure , IBuildSession , IContext )
   --- End of inner exception stack trace ---
   at lambda_method(Closure , IBuildSession , IContext )
   at StructureMap.Building.BuildPlan.Build(IBuildSession session, IContext context)
   at StructureMap.BuildSession.BuildNewInSession(Type pluginType, Instance instance)
   at StructureMap.Pipeline.NulloTransientCache.Get(Type pluginType, Instance instance, IBuildSession session)
   at StructureMap.BuildSession.ResolveFromLifecycle(Type pluginType, Instance instance)
   at StructureMap.SessionCache.GetObject(Type pluginType, Instance instance, ILifecycle lifecycle)
   at StructureMap.SessionCache.GetDefault(Type pluginType, IPipelineGraph pipelineGraph)
   at StructureMap.Container.GetInstance[T]()
   at UserQuery.<Main>d__0.MoveNext() in C:\Users\thoma\AppData\Local\Temp\LINQPad5\_yuoswwhz\query_sodejb.cs:line 46

Is this library supposed to be working? I tried the example from the doc after another failed attempt, which gave me a different exception (MethodAccessException on Expression.Lambda<T>). Could it be because I'm running it in LINQPad?

jeremydmiller commented 7 years ago

"Works on my box" - all the tests for this lib are passing on CI. @DixonDs Have you seen this one by chance?

DixonDs commented 7 years ago

Nope, I haven't seen it before now. But the issue is reproducible in a VS console application, so it doesn't seem to be LINQPad-specific.

thomaslevesque commented 7 years ago

I tried it in a xUnit test to be in exactly the same situation as in the example: same result. I tried targeting .NET 4.6.2 and 4.5, and .NET Core.

jeremydmiller commented 7 years ago

The fix will be in 1.0.1 in just a bit

thomaslevesque commented 7 years ago

The fix will be in 1.0.1 in just a bit

Thanks, it works now!

(Also, I seem to have posted to the wrong repo... didn't realize it was obsolete. Sorry about that)