ServiceStack GetRequestFilterAttributes NullReference

时间:2013-04-16 01:14:40

标签: c# servicestack

我正在尝试使用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。我对任何一条路线都有同样的错误。有什么建议吗?

1 个答案:

答案 0 :(得分:2)

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

初始化并启动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和Configure继承(不为此示例配置任何内容)

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

处理您的请求的服务代码 - 网址:localhost:1337/AllAccounts&amp; 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;
    }
}