2

ここで何かが欠けているかどうかわかりません。単体テストで AppHostHttpListenerBase を使用してサービスをテストし、そのコンストラクターで handlerPath パラメーターに「api」を渡します。/hello/{Name} に登録されたサービスがあり、サービススタックのバージョン 3.9.17 を使用しています。

アクセスする場合、 appHost クラスの Config メソッド内

EndpointHostConfig.Instance.ServiceStackHandlerFactoryPath

「api」を返します

単体テストに戻ると、同じ呼び出しで null が返されます

/hello/test でサービスを呼び出そうとすると、動作します。/api/hello/test を使用すると失敗します

AppHostHttpListenerBase が handlerPath を失っているように見えますか?

これはバグのように聞こえますか、それとも何か不足していますか?

以下はコードです

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using ServiceStack.ServiceClient.Web;
using ServiceStack.ServiceInterface;
using ServiceStack.Text;
using ServiceStack.WebHost.Endpoints;

namespace Bm.Tests
{
    /// <summary>
    /// Test self hosting for unit tests
    /// </summary>
    [TestFixture]
    public class TestService
    {
    private TestServiceAppHost _apphost;
    private const string HOST_URL = @"http://localhost:1337/";
    [TestFixtureSetUp]
    public void TestFixtureSetUp()
    {
        _apphost = new TestServiceAppHost();
        _apphost.Init();
        _apphost.Start(HOST_URL);

    }

    [Test]
    public void TestHelloServiceJson()
    {

        var prefix = EndpointHostConfig.Instance.ServiceStackHandlerFactoryPath;
        Assert.AreEqual("api", prefix, "Should be api");

        var client = new JsonServiceClient(HOST_URL);
        var response = client.Send<HelloResponseTest>(new HelloTest() { Name = "Todd" });
        Assert.AreEqual("Hello, Todd", response.Result);
    }


    [TestFixtureTearDown]
    public void TestFixtureTearDown()
    {
        _apphost.Stop();
        _apphost.Dispose();
    }

    }

    public class HelloTest
    {
    public string Name { get; set; }
    }

    public class HelloResponseTest
    {
    public string Result { get; set; }
    }

    public class HelloServiceTest : ServiceBase<HelloTest>
    {
    protected override object Run(HelloTest request)
    {
        return new HelloResponseTest { Result = "Hello, " + request.Name };
    }
    }

    //Define the Web Services AppHost
    public class TestServiceAppHost : AppHostHttpListenerBase
    {
    public TestServiceAppHost() : base("testing HttpListener", "api", typeof(HelloServiceTest).Assembly) { }

    public override void Configure(Funq.Container container)
    {
        // this works and returns api
        var prefix = EndpointHostConfig.Instance.ServiceStackHandlerFactoryPath;
        Routes
                .Add<HelloTest>("/hello")
                .Add<HelloTest>("/hello/{Name}");
    }
    }
}
4

1 に答える 1

1

ハンドラのルート パスが/api必要な場合は、それをリスナーの URL に追加する必要があります。

_apphost.Start("http://localhost:1337/api/");
于 2012-09-25T20:37:31.357 に答える