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();
    }
}

これはかなり簡単です。

ここで、Web サイト プロジェクトから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。-属性では機能しRestServiceAttributeません...

于 2012-08-20T07:33:44.513 に答える