c# – ServiceStack GetRequestFilterAttributes NullReference
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – ServiceStack GetRequestFilterAttributes NullReference,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2627字,纯文字阅读大概需要4分钟。
内容图文
![c# – ServiceStack GetRequestFilterAttributes NullReference](/upload/InfoBanner/zyjiaocheng/778/2225bf4214124a9abafa009f9dc57a1d.jpg)
我正在尝试使用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.我对任何一条路线都有同样的错误.有什么建议?
解决方法:
查看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和Configure(不为此示例配置任何内容)
public class AppHost : AppHostHttpListenerBase
{
public AppHost() : base("Test Console", typeof(AppHost).Assembly) { }
public override void Configure(Funq.Container container)
{
}
}
Dto / Request类
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&本地主机: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;
}
}
内容总结
以上是互联网集市为您收集整理的c# – ServiceStack GetRequestFilterAttributes NullReference全部内容,希望文章能够帮你解决c# – ServiceStack GetRequestFilterAttributes NullReference所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。