2

基本上我有以下架构:

  • 网站项目 (1)
  • 领域项目 (2)
  • Api-项目 (3)

依赖项:

  • 1 使用 2 和 3
  • 3 次使用 2
  • 2 什么都不用

在我的 Api 项目中,我定义了一个具体的实现ServiceStack.Webhost.Endpoints.AppHostBase,例如ApiAppHost

public sealed class ApiAppHost : AppHostBase
{
    private ApiAppHost()
        : base("Description", typeof (ApiAppHost).Assembly) {}

    public override void Configure(Container container)
    {
        this.SetConfig(new EndpointHostConfig
        {
            ServiceStackHandlerFactoryPath = "api"
        });

        this.Routes.Add<Foo>("/foo", "POST");
    }

    public static void Initialize()
    {
        var instance = new ApiAppHost();
        instance.Init();
    }
}

这很简单。

现在我想从我的网站项目中查询我this.Routes(结合)以获取.EndpointHostConfig.ServiceStackHandlerFactoryPathFoo

如果不自己创建拦截器,我怎么能做到这一点?ServiceStack.Net 是否提供任何适合的东西?

4

1 回答 1

1

目前我正在做这样的事情

public static class AppHostBaseExtensions
{
    public static string GetUrl<TRequest>(this AppHostBase appHostBase)
    {
        var requestType = typeof (TRequest);

        return appHostBase.GetUrl(requestType);
    }

    public static string GetUrl(this AppHostBase appHostBase, Type requestType)
    {
        var endpointHostConfig = appHostBase.Config;
        var serviceStackHandlerFactoryPath = endpointHostConfig.ServiceStackHandlerFactoryPath;

        var serviceRoutes = appHostBase.Routes as ServiceRoutes;
        if (serviceRoutes == null)
        {
            throw new NotSupportedException("Property Routes of AppHostBase is not of type ServiceStack.ServiceHost.ServiceRoutes");
        }
        var restPaths = serviceRoutes.RestPaths;
        var restPath = restPaths.FirstOrDefault(arg => arg.RequestType == requestType);
        if (restPath == null)
        {
            return null;
        }

        var path = restPath.Path;
        var virtualPath = "~/" + string.Concat(serviceStackHandlerFactoryPath, path); // bad, i know, but combining with 2 virtual paths ...
        var absolutePath = VirtualPathUtility.ToAbsolute(virtualPath);

        return absolutePath;
    }
}

我知道这是错误的,因为有很多问题(路径组合,不考虑休息路径,不考虑休息路径和占位符),但它作为一个开始......

编辑:只有在Configure(Container)你的AppHostBase-implementation 中注册路由时,这才有效。它不适用于RestServiceAttribute-attribute ...

于 2012-08-20T07:33:44.513 回答