ServiceStack GetRequestFilterAttributes NullReference

本文关键字:NullReference GetRequestFilterAttributes ServiceStack | 更新日期: 2023-09-27 18:09:27

我正在尝试为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和服务(在这一点上,我只是试图返回硬编码的结果,看看它的工作)

[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。我得到相同的错误与任何一个路线。有什么建议吗?

ServiceStack GetRequestFilterAttributes NullReference

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

初始化并启动ServiceStack

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和Configure(在本例中不配置任何内容)

public class AppHost : AppHostHttpListenerBase
{
    public AppHost() : base("Test Console", typeof(AppHost).Assembly) { }
    public override void Configure(Funq.Container container)
    {
    }
}

Dto/Request classes

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

处理请求的服务代码- url: 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;
    }
}