user1661621
user1661621

Reputation: 797

Servicestack registration crashes with generic types

If I have a base class for my services like

public abstract class BaseService<T,R> : ServiceStack.ServiceInterface.Service
{
    public R Get(T request)
    {
    }
}

Then service stack crashes with

An attempt was made to load a program with an incorrect format.

I think Servicestack should ignore the abstract generic classes when registering services. Is there any way to tell servicestack to ignore some service classes ?

Upvotes: 3

Views: 383

Answers (1)

Brian Kohrs
Brian Kohrs

Reputation: 389

By default, ServiceStack is including all types in the assemblies as candidates for services. It gets that exception when it tries to instantiate the class.

By overriding the CreateServiceManager in the host class, you can inject your own filtering of types so that abstract and unclosed generics are excluded.

    protected override ServiceManager CreateServiceManager(params Assembly[] assembliesWithServices)
    {
        return new ServiceManager(
            new Container(),
            new ServiceController(
                () =>
                assembliesWithServices.SelectMany(
                    assembly => assembly.GetTypes().Where(t => !t.IsAbstract && !t.IsGenericTypeDefinition))));
    }

Upvotes: 2

Related Questions