问题描述
不确定我是否在这里遗漏了什么.我在单元测试中使用 AppHostHttpListenerBase 来测试服务,并在其构造函数中为 handlerPath 参数传递api".我在/hello/{Name} 注册了一个服务,我使用的是 servicestack 3.9.17 版.
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.
在我的 appHost 类的 Config 方法中,如果我访问
Within the Config method of my appHost class if I access
EndpointHostConfig.Instance.ServiceStackHandlerFactoryPath
EndpointHostConfig.Instance.ServiceStackHandlerFactoryPath
它返回api"
一旦我回到单元测试,同样的调用返回空
Once I am back in the unit test the same call returns null
如果我尝试使用/hello/test 调用该服务,它会起作用.如果我使用/api/hello/test 它会失败
If I try and call the service with /hello/test it works.If I use /api/hello/test it fails
看起来 AppHostHttpListenerBase 正在丢失 handlerPath ?
It appears that the AppHostHttpListenerBase is loosing the handlerPath ?
这听起来像是一个错误还是我遗漏了什么?
Does this sound like a bug or am I missing something ?
下面是代码
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}");
}
}
}
推荐答案
如果您希望处理程序根路径为 /api
,则需要将其添加到侦听器 url,即:
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/");
这篇关于servicestack AppHostHttpListenerBase 处理程序路径参数不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!