servicestack AppHostHttpListenerBase handlerpath p

2019-04-13 18:31发布

问题:

not sure if I am missing something here. I am using the AppHostHttpListenerBase in a unit test to test a service and in its constructor I pass "api" for the handlerPath parameter. I have a service registered at /hello/{Name} and am using version 3.9.17 of servicestack.

Within the Config method of my appHost class if I access

EndpointHostConfig.Instance.ServiceStackHandlerFactoryPath

it retrurns "api"

Once I am back in the unit test the same call returns null

If I try and call the service with /hello/test it works. If I use /api/hello/test it fails

It appears that the AppHostHttpListenerBase is loosing the handlerPath ?

Does this sound like a bug or am I missing something ?

below is the code

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

回答1:

If you want the handler root path to be /api you need to add that to the listener url, i.e:

_apphost.Start("http://localhost:1337/api/");