servicestack,C#,servicestack" /> servicestack,C#,servicestack" />

C# ServiceStack GetRequestFilterAttributes NullReference

C# ServiceStack GetRequestFilterAttributes NullReference,c#,servicestack,C#,servicestack,我正在尝试为ServiceStack使用新的API方法,并正在构建一个测试控制台应用程序来承载它。到目前为止,我已经有了实例化请求DTO的路由,但在请求到达我的服务的任何方法之前,我得到了以下异常: 错误代码NullReferenceException 消息对象引用未设置为对象的实例。 位于的ServiceStack.WebHost.Endpoints.Utils.FilterAttributeCache.GetRequestFilterAttributes(类型requestDtoType)处

我正在尝试为ServiceStack使用新的API方法,并正在构建一个测试控制台应用程序来承载它。到目前为止,我已经有了实例化请求DTO的路由,但在请求到达我的服务的任何方法之前,我得到了以下异常:

错误代码NullReferenceException
消息对象引用未设置为对象的实例。
位于的ServiceStack.WebHost.Endpoints.Utils.FilterAttributeCache.GetRequestFilterAttributes(类型requestDtoType)处的堆栈跟踪
ServiceStack.WebHost.Endpoints.EndpointHost.ApplyRequestFilters(IHttpRequest httpReq、IHttpResponse httpRes、Object requestDto)位于
ServiceStack.WebHost.Endpoints.RestHandler.ProcessRequest(IHttpRequest httpReq、IHttpResponse httpRes、String operationName)
下面是我使用IReturn和Service的测试服务(此时,我只尝试返回硬编码的结果以查看它是否正常工作)

[DataContract]
公共类所有帐户:IReturn
{
公共所有账户()
{
}
}
[数据合同]
公共类AccountTest:IReturn
{
公共帐户测试()
{
这个。Id=4;
}
[数据成员]
公共int Id{get;set;}
}
公共类AccountService:服务
{
公共会计服务()
{
}
任何公共对象(AccountTest测试)
{
回复“你好”;
}
任何公共对象(AllAccounts请求)
{
var ret=new List{new Account(){Id=3};
返回ret;
}
}

所有ServiceStack引用都来自NuGet。无论哪条路线,我都会遇到同样的错误。有什么建议吗?

查看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继承并配置(本例中不配置任何内容)

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

谢谢,这确实有效。对于那些看到类似问题的人,问题是我的AppHost初始值设定项没有引用包含服务的程序集。我的解决方案有两个项目,其中一个包含AppHost,另一个包含服务端点。所以在我的例子中,我必须像这样初始化AppHost:public AppHost():base(“测试控制台”,typeof(AccountService.Assembly){}
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; }
}
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;
    }
}