ServiceStack服务元数据不显示任何操作

时间:2013-07-05 21:41:46

标签: asp.net-mvc-4 routing servicestack

我第一次在一个以ASP.NET MVC开始的全新项目中使用ServiceStack。我在root上托管ServiceStack API,所以我的web.config看起来像这样:

  <system.web>
    <httpHandlers>
      <add path="*" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" />
    </httpHandlers>
  </system.web>
  <system.webServer>
    <handlers>
      <add path="*" name="ServiceStack.Factory" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true" />
    </handlers>
  </system.webServer>

和我的App_Start / RouteConfig.cs:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("*");
}

这是我的服务:

[Route("/catalogs/{ID}")]
[DataContract]
public class CatalogRequest : IReturn<Catalog>
{
    [DataMember]
    public int ID { get; set; }
}

[DefaultView("Catalogs")]
public class CatalogService : Service
{
    public object Get(CatalogRequest request)
    {
        return (request.ID == 9999) ? new Catalog() { ID = 9999 } : null;
    }
}

我使用以下内容进行测试:

public class TestAppHost : AppHostHttpListenerBase
{
    public TestAppHost() : base("TestService", typeof(TestAppHost).Assembly) { }

    public override void Configure(Funq.Container container)
    {
        IoC.Configure(container); // IoC is where all Funq configuration is done
    }
}

在我的VisualStudio单元测试中,我启动了这样的AppHost:

[TestClass]
public class TestHelper
{
    public const string TEST_HOST_URL = "http://127.0.0.1:8888/";

    private static TestAppHost __AppHost;

    [AssemblyInitialize]
    public static void Initialize(TestContext context)
    {
        // Start the test app host.
        __AppHost = new TestAppHost();
        __AppHost.Init();
        __AppHost.Start(TEST_HOST_URL);
    }

    [AssemblyCleanup]
    public static void Cleanup()
    {
        __AppHost.Dispose();
        __AppHost = null;
    }
}

当我进行测试时:

[TestMethod]
public void RequestCatalogByID()
{
    var client = new JsonServiceClient(TestHelper.TEST_HOST_URL);
    var request = new CatalogRequest() { ID = 9999 };
    var response = client.Get(request);

    Assert.IsNotNull(response);
}

即使网址似乎正确,我也会收到“未找到”异常:http://127.0.0.1:8888/catalogs/9999

将浏览器指向http://127.0.0.1:8888/metadata会显示没有操作的元数据页面。

我做错了什么?

1 个答案:

答案 0 :(得分:2)

请注意,您在AppHost Base构造函数中传递的程序集应该是您的所有服务实现所在的位置(即不是您的AppHost),所以请尝试:

public class TestAppHost : AppHostHttpListenerBase
{
    public TestAppHost() : base("TestService", typeof(CatalogService).Assembly){}
    ...
}