4

我正在尝试对 ServiceStack 使用新的 API 方法,并且正在构建一个测试控制台应用程序来托管它。到目前为止,我有实例化请求 DTO 的路由,但是在请求到达我的服务的 Any 方法之前,我得到了这个异常:

Error Code NullReferenceException 
Message Object reference not set to an instance of an object. 
Stack Trace at ServiceStack.WebHost.Endpoints.Utils.FilterAttributeCache.GetRequestFilterAttributes(Type requestDtoType) at 
ServiceStack.WebHost.Endpoints.EndpointHost.ApplyRequestFilters(IHttpRequest httpReq, IHttpResponse httpRes, Object requestDto) at 
ServiceStack.WebHost.Endpoints.RestHandler.ProcessRequest(IHttpRequest httpReq, IHttpResponse httpRes, String operationName)

下面是我使用 IReturn 和 Service 的测试服务(此时我只是试图返回硬编码的结果以查看它的工作)

[DataContract]
public class AllAccounts : IReturn<List<Account>>
{
    public AllAccounts()
    {

    }
}
[DataContract]
public class AccountTest : IReturn<string>
{
    public AccountTest()
    {
        this.Id = 4;
    }
    [DataMember]
    public int Id { get; set; }
}

public class AccountService : Service
{
    public AccountService()
    {
    }

    public object Any(AccountTest test)
    {
        return "hello";
    }
    public object Any(AllAccounts request)
    {
        var ret = new List<Account> {new Account() {Id = 3}};
        return ret;
    }
}

所有 ServiceStack 引用都来自 NuGet。我在任何一条路线上都遇到同样的错误。有什么建议么?

4

1 回答 1

2

查看您的 AppHost 代码和 Configure() 方法中的代码可能会有所帮助。您在上面的代码中提供的任何内容都没有突出显示。下面是我如何使用您提供的代码/类设置一个简单的控制台应用程序。

初始化并启动 ServiceStack AppHost

class Program
{
    static void Main(string[] args)
    {
        var appHost = new AppHost();
        appHost.Init();
        appHost.Start("http://*:1337/");
        System.Console.WriteLine("Listening on http://localhost:1337/ ...");
        System.Console.ReadLine();
        System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
    }
}

从 AppHostHttpListenerBase 继承并配置(不为本示例配置任何内容)

public class AppHost : AppHostHttpListenerBase
{
    public AppHost() : base("Test Console", typeof(AppHost).Assembly) { }

    public override void Configure(Funq.Container container)
    {
    }

}

Dto/请求类

public class Account
{
    public int Id { get; set;  }
}

[Route("/AllAccounts")]
[DataContract]
public class AllAccounts : IReturn<List<Account>>
{
    public AllAccounts()
    {

    }
}

[Route("/AccountTest")]
[DataContract]
public class AccountTest : IReturn<string>
{
    public AccountTest()
    {
        this.Id = 4;
    }
    [DataMember]
    public int Id { get; set; }
}

处理您的请求的服务代码 - URLS: localhost:1337/AllAccounts&localhost:1337/AccountTest

public class AccountService : Service
{
    public AccountService()
    {
    }

    public object Any(AccountTest test)
    {
        return "hello";
    }
    public object Any(AllAccounts request)
    {
        var ret = new List<Account> { new Account() { Id = 3 } };
        return ret;
    }
}
于 2013-04-16T15:27:40.200 回答