Autofac: adding a decorator to a delegate registration that takes parameters -
i'm trying manage registration of wcf clients using autofac.
i need resolve func<machineendpoint, ifooservice>
, had registered service using:
builder.register((c, p) => fooservice(c, p.typedas<machineendpoint>())).usewcfsaferelease()
where fooservice is:
private ijobservice fooservice(icomponentcontext c, machineendpoint endpoint) {...}
this works fine, want add decorator handle security errors, tried following:
builder.register((c, p) => fooservice(c, p.typedas<machineendpoint>())).usewcfsaferelease().named<ifooservice>("simple foo service"); builder.registerdecorator<ifooservice>((c, p, inner) => wrap(inner, p.typedas<machineendpoint>(), c.resolve<certificatelookupcache>()), "simple foo service");
but dependecyresolutionexception:
autofac.core.dependencyresolutionexception: exception thrown while executing resolve operation. see innerexception details. ---> system.invalidoperationexception: sequence contains no elements @ system.linq.enumerable.first[tsource](ienumerable`1 source) @ mymodule.<load>b__4(icomponentcontext c, ienumerable`1 p) in mymodule.cs:line 30 (the first line above) @ autofac.builder.registrationbuilder.<>c__displayclass1`1.<fordelegate>b__0(icomponentcontext c, ienumerable`1 p) @ autofac.core.activators.delegate.delegateactivator.activateinstance(icomponentcontext context, ienumerable`1 parameters) @ autofac.core.resolving.instancelookup.activate(ienumerable`1 parameters) @ autofac.core.resolving.instancelookup.execute() @ autofac.core.resolving.resolveoperation.getorcreateinstance(isharinglifetimescope currentoperationscope, icomponentregistration registration, ienumerable`1 parameters) @ autofac.features.lightweightadapters.lightweightadapterregistrationsource.<>c__displayclass3.<registrationsfor>b__1(icomponentcontext c, ienumerable`1 p) @ autofac.builder.registrationbuilder.<>c__displayclass1`1.<fordelegate>b__0(icomponentcontext c, ienumerable`1 p) @ autofac.core.activators.delegate.delegateactivator.activateinstance(icomponentcontext context, ienumerable`1 parameters) @ autofac.core.resolving.instancelookup.activate(ienumerable`1 parameters) @ autofac.core.resolving.instancelookup.execute() @ autofac.core.resolving.resolveoperation.getorcreateinstance(isharinglifetimescope currentoperationscope, icomponentregistration registration, ienumerable`1 parameters) @ autofac.core.resolving.resolveoperation.execute(icomponentregistration registration, ienumerable`1 parameters) --- end of inner exception stack trace --- server stack trace: @ autofac.core.resolving.resolveoperation.execute(icomponentregistration registration, ienumerable`1 parameters) @ autofac.core.lifetime.lifetimescope.resolvecomponent(icomponentregistration registration, ienumerable`1 parameters) @ lambda_method(closure , machineendpoint )
how can register these functions can machinendpoint parameter passed both wcf client object , decorator?
the problem caused parameter usage: p.typedas<machineendpoint>()
because there limitation in autofac namely registerdecorator
not pass through parameters when creating inner
decorated object.
so when tries create inner
(c, p) => fooservice(c, p.typedas<machineendpoint>())
(ienumerable<parameter> p)
empty exception.
you can 2 things:
you can try reorganize code in way doesn't use p.typedas<machineendpoint>()
anymore.
or
because ifooservice
anyway wcf service not have multiple implementations can register decorator "hand":
builder.register((c, p) => fooservice(c, p.typedas<machineendpoint>())) .usewcfsaferelease() .named<ifooservice>("simple foo service"); builder.register<ifooservice>((c, p) => wrap(c.resolvenamed<ifooservice>("simple foo service", typedparameter.from(p.typedas<machineendpoint>())), p.typedas<machineendpoint>(), c.resolve<certificatelookupcache>()));
Comments
Post a Comment