Big McLargeHuge
Big McLargeHuge

Reputation: 16066

Set interceptors on several types at once

I'm working on an app that hasn't been updated in years, and I'm supposed to update its dependencies. Castle Windsor needs to be upgraded from 2.5.4 to 3.3.0. After the upgrade, the following no longer compiles:

container.Register(
    Types.FromThisAssembly().Where(t => Attribute.IsDefined(t, typeof (ServiceBehaviorAttribute)))
        .WithService.DefaultInterfaces()
        .Configure(c => c.Interceptors(
            InterceptorReference.ForType<ServiceInterceptor>(),
            InterceptorReference.ForType<LoggingInterceptor>(),
            InterceptorReference.ForType<ExceptionInterceptor>()).Anywhere.LifeStyle.Transient));

The error is:

Only assignment, call, increment, decrement, await expression, and new object expressions can be used as a statement.

ReSharper tries to help out by assigning a variable:

container.Register(
    Types.FromThisAssembly().Where(t => Attribute.IsDefined(t, typeof (ServiceBehaviorAttribute)))
        .WithService.DefaultInterfaces()
        .Configure(c =>
        {
            var componentRegistration = c.Interceptors(
                InterceptorReference.ForType<ServiceInterceptor>(),
                InterceptorReference.ForType<LoggingInterceptor>(),
                InterceptorReference.ForType<ExceptionInterceptor>()).Anywhere.LifeStyle.Transient;
            componentRegistration;
        }));

This throws the same error, however.

I've looked high and low for the right way to set the interceptors on several types at once like this, but all the examples I've found are either old or are just setting the interceptors for a single component, such as this one from the docs:

container.Register(
    Component.For<ICalcService>()
        .Interceptors(InterceptorReference.ForType<ReturnDefaultInterceptor>()).Last,
    Component.For<ReturnDefaultInterceptor>()
);

This doesn't work because I can't register every component individually.

Upvotes: 0

Views: 104

Answers (1)

Krzysztof Kozmic
Krzysztof Kozmic

Reputation: 27384

replace LifeStyle.Transient with LifestyleTransient()

Upvotes: 1

Related Questions