ronilk
ronilk

Reputation: 372

Can I register a servicestack response filter inside web.config?

I am using servicestack v3. I have two websites(Public services and Storage services) in IIS where the servicestack services are deployed. In the public services website we need to use certain response filters. Whereas they are not required in storage services website.

I was wondering if it is possible to register response filters inside web.config, rather than inside the AppHost.Configure method to make it easy to register them only in websites where they are required and use the same servicestack(AppHost) dlls. I would prefer not to have two different code versions of servicestack(Apphost) dlls. The Apphost code is in C# and the actual services are written in F#.

Thanks.

Upvotes: 0

Views: 98

Answers (1)

ronilk
ronilk

Reputation: 372

Eventually, I followed these steps to create configurable plug-in responsefilters:

1) Created dll containing ICustomResponseFilter interface:

public interface ICustomResponseFilter
{
    Action<IHttpRequest, IHttpResponse, object> GetAction();
}

2) Created dll containing class for actual response filter that is to be registered which implements above interface:

public class AuthResponseFilter : ICustomResponseFilter
{
    public Action<IHttpRequest, IHttpResponse, object> GetAction()
    {
        // code implementation that returns Action type
    }
}

3) Stored the type names for the filters in database(configuration).

table: SSResponseFilters
important columns: 
    typename: varchar (values in format: <namespace>.<classname>)
    assemblyname: varchar (values in format: <assemblyname>) 
    site: varchar (possible values: public|storage)

This configuration may be done anywhere else also, eg in web.config

4) A custom Apphost class is used. Inside that class the filters are registered:

public class CustomAppHost : AppHostBase
{
    private List<Action<IHttpRequest, IHttpResponse, object>> responseFilterActions = null;

    public CustomAppHost(serviceType, serviceName, assemblies) : base(serviceName, assemblies)
    {       
        List<ICustomResponseFilter> rfList = GetListOfConfiguredResponseFilters(serviceType); 
        /*In GetListOfConfiguredResponseFilters method, serviceType is taken from config and it determines which site we are in currently and retrieves the configured types explained in above step for that site. We need to use reflection here.*/
        this.responseFilterActions = GetActionsListFromResponseFilters(rfList); 
        /*GetActionsListFromResponseFilters method calls the GetAction method of the filters and generates a list of Action types and returns that list*/
    }

    public override void Configure(Funq.Container container)
    {
        // other code ..

        EndpointHost.ResponseFilters.AddRange(responseFilterActions);
    }
}

5) In every site, the dll containing the interface is mandatory. Whenever a new responsefilter is to be plugged in, we may write a dll containing the class that implements the interface and make an entry in the DB for the filter. The response filter dlls may be used only wherever required. Service stack dll(AppHost) remains same in all sites.

Upvotes: 0

Related Questions