0

我正在使用 servicestack v3。我在 IIS 中有两个网站(公共服务和存储服务),其中部署了 servicestack 服务。在公共服务网站中,我们需要使用某些响应过滤器。而存储服务网站不需要它们。

我想知道是否可以在 web.config 中注册响应过滤器,而不是在 AppHost.Configure 方法中,以便仅在需要它们的网站中轻松注册它们并使用相同的 servicestack(AppHost) dll。我不希望有两个不同代码版本的 servicestack(Apphost) dll。Apphost 代码使用 C# 编写,实际服务使用 F# 编写。

谢谢。

4

1 回答 1

0

最终,我按照以下步骤创建了可配置的插件响应过滤器:

1) 创建包含 ICustomResponseFilter 接口的 dll:

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

2)创建的dll包含要注册的实际响应过滤器的类,该过滤器实现上述接口:

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

3) 将过滤器的类型名称存储在数据库(配置)中。

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

此配置也可以在其他任何地方完成,例如在 web.config

4) 使用自定义 Apphost 类。在该类中注册了过滤器:

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) 在每个站点中,包含接口的 dll 都是强制性的。每当要插入新的响应过滤器时,我们可以编写一个包含实现该接口的类的 dll,并在数据库中为过滤器创建一个条目。响应过滤器 dll 只能在需要的地方使用。服务堆栈 dll(AppHost) 在所有站点中保持不变。

于 2017-03-01T11:37:26.727 回答