Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Servicestack registration crashes with generic types

Tags:

servicestack

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 ?

like image 626
user1661621 Avatar asked Dec 11 '25 01:12

user1661621


1 Answers

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))));
    }
like image 107
Brian Kohrs Avatar answered Dec 13 '25 20:12

Brian Kohrs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!