我正在尝试在MonoDroid项目中使用ServiceStack进行Json序列化/反序列化.
我已经构建了ServiceStack以在MonoDroid环境中运行它,但现在我遇到了JSON反序列化的问题.
示例代码:
public class Track
{
public string Id { get; set; }
public string Title { get; set; }
public string Artist { get; set; }
public string Hash { get; set; }
public string Type { get; set; }
}
}
...
var track = new Track { Id = "1", Artist = "artist name", Hash = "654874", Title = "song title", Type = "mp3", };
var json = ServiceStack.Text.JsonSerializer.SerializeToString(track);
var track1 = ServiceStack.Text.JsonSerializer.DeserializeFromString<Track>(json);
Run Code Online (Sandbox Code Playgroud)
在反序列化时,我有一个例外:未处理的异常:
System.ArgumentNullException: Argument …Run Code Online (Sandbox Code Playgroud) 在Redis文档中,当您调用Get操作并且键不存在时,它应该返回Nil.(来源:http://redis.io/commands/get)
ServiceStack客户端如何处理这个问题?它是否为null,或者我是否需要使用Exists,ContainsKey?
是否有另一种方法来检查密钥是否存在并使用一个请求返回值?
我引用了ServiceStack.dll ver-3.9.4
包含AppHost.cs中的代码
SetConfig(new EndpointHostConfig {
EnableFeatures = Feature.All.Remove(Feature.Html),
});
我得到下面的错误,无法编译
'ServiceStack.ServiceHost.Feature'不包含'Remove'的定义,也没有扩展方法'Remove'接受类型为'ServiceStack.ServiceHost.Feature'的第一个参数'(你是否缺少using指令或程序集引用) ?)
尝试包括许多指令,但我仍然无法通过这个
使用Funq;
使用ServiceStack.WebHost.Endpoints;
使用ServiceStack.ServiceHost;
使用ServiceStack.WebHost.Endpoints.Support;
使用ServiceStack.Common.Support;
使用ServiceStack.Common.Extensions;
使用ServiceStack.Common.Utils;
使用ServiceStack.Common.Web;
使用ServiceStack.Logging.Support.Logging;
使用ServiceStack.CacheAccess;
使用ServiceStack.CacheAccess.Providers;
使用ServiceStack.OrmLite;
使用ServiceStack.WebHost;
我正在尝试从asp.net mvc4 webapi框架迁移到servicestack框架.我在webapi中有一个delegatingHandler,它在servicestack中等同于什么?
这是我将验证我的请求并返回自定义响应而不再进一步的地方.
我的代表处理者
public class xyzDH : DelegatingHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
int maxLengthAllowed = 200;
long? contentLen = request.Content.Headers.ContentLength;
if (contentLen > maxLengthAllowed)
{
var defaultResponse = ResponseHelper.GetBaseResponse("Content Lenght Issue", true, UploadLogSizeIssue);
return Task<HttpResponseMessage>.Factory.StartNew(() =>
{
var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(defaultResponse.ToString(), Encoding.UTF8, "message/http")
};
return response;
});
}
return base.SendAsync(request, cancellationToken);
}
}
Run Code Online (Sandbox Code Playgroud) [Default]数据注释与ORMLite一起使用.但是,它不适用于响应的默认值.是否有类似于[Default]响应DTO 的属性?
考虑以下代码:
[Route("api/hello")]
public class Hello {
public string Ping { get; set; }
}
public class HelloResponse {
public ResponseStatus ResponseStatus { get; set; }
[Default(typeof(string), "(nothing comes back!)")]
public string Pong { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我希望Response DTO Pong属性具有默认值"(没有回来!)"而不是null.
我想在OrmLite中为数据库插入或更新创建一个钩子.
假设我想CreateDate为每个插入数据库的记录写一个a,LastUpdateDate为没有数据库触发器的每个更新写一个或类似的东西.我看不出这样做,如果我打电话的方式Db.Save<Anything>(anyList)与anyList包含新增和更改的对象.
我知道NHibernate确实提供了拦截器,以便在每个对象被持久化之前为其注入自定义逻辑.有没有办法在ServiceStack.OrmLite中实现这一点?
我使用ServiceStack版本3.9.71,我找不到"添加"方法.维基上的文档是否已过时?我该怎么办才能让它发挥作用?
public override void Configure(Container container)
{
this.ServiceExceptionHandler.Add((httpReq, request, exception) => {
return DtoUtils.CreateErrorResponse(request, exception);
});
}
Run Code Online (Sandbox Code Playgroud) 在阅读了关于这个主题的几个不同的SO帖子和文档后,我仍然感到困惑,无法通过我的服务端点上的RequestBindingException.例如,我想使用POSTMAN将以下xml发布到服务.
<LeadApplications>
<LeadApplication>
<Email>daffy.duck@example.com</Email>
<FirstName>Joey</FirstName>
<MiddleName>Disney</MiddleName>
<LastName>Duck</LastName>
<Street1>1 Disneyland Street</Street1>
<Street2>2 Disneyland Street</Street2>
<City>PAUMA VALLEY</City>
<State>CA</State>
<Zip>92503</Zip>
</LeadApplication>
</LeadInformations>
[Restrict(RequestAttributes.Xml)]
public class LeadData : IRequiresRequestStream
{
public Stream RequestStream { get; set; }
}
public object Post(ServiceModel.Types.DirectApi.Legacy.LeadData request)
{
return null;
}
Routes.Add<ServiceModel.Types.DirectApi.Legacy.LeadData>("/Leads/LeadData/", "POST", "LMS - DirectApi")
Run Code Online (Sandbox Code Playgroud)
我希望这将是克服.NET反序列化属性排序问题的最佳方法.
原始请求/响应
POST http://localhost/LO.Leads.Receiver/api/Leads/LeadData/ HTTP/1.1
Host: localhost
Connection: keep-alive
Content-Length: 381
Origin: chrome-extension://aejoelaoggembcahagimdiliamlcdmfm
User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2289.0 Safari/537.36
Content-Type: application/xml
Accept: …Run Code Online (Sandbox Code Playgroud) 我最近将ServiceStack软件包升级到v4.0.46(从v4.0.36开始),我们的应用程序中有一些区域使用ResolveService来调用数据库事务中的另一个服务.以前这一切都运行良好,但升级到v4.0.46后,我们收到此错误:
连接必须有效且开放
调用者看起来像这样:
public class DeleteItemService: CustomerServiceBase
{
public object Post(DeleteItem request)
{
WriteDb(conn => {
using (var service = ResolveService<DeleteDocumentsService>()) {
service.Post(new DeleteDocumentsRequest {
Ids = ids.ToArray()
});
}
conn.Delete<Item>(request.Id);
});
return RespondSuccess<ResponseBase>();
}
}
Run Code Online (Sandbox Code Playgroud)
DeleteDocumentsService看起来有点像这样
public class DeleteDocumentsService: CustomerServiceBase
{
public ILog Log { get; set; }
public PrivateStorage PMStorage { get; set; }
public ResponseBase Post(DeleteDocumentsRequest request)
{
WriteDb(conn => {
var link = conn.Select<DocumentLink>(l => l.DocumentStorageId == item.Id).FirstOrDefault();
conn.Delete<DocumentStorage>(item.Id);
});
return RespondSuccess<ResponseBase>();
}
Run Code Online (Sandbox Code Playgroud)
WriteDb只是数据库事务的包装器,如下所示:
public void …Run Code Online (Sandbox Code Playgroud) 我正在使用服务堆栈来接受向后端数据库添加行的http请求,相当标准的东西.我现在遇到的问题是,有时,设备向服务发送数据,相隔毫秒发送相同的请求.这导致数据库约束错误,因为第一个请求仍在插入新行等
所以,我想我需要找到一种方法来检测重复请求,并忽略它或将HttpError抛回客户端.我现在的想法是将完整的原始POST数据存储到临时表中,并在处理完成后将其删除.在每个POST请求中,我都会在那里查找数据并忽略它是否重复.有没有更简单的方法来检测ServiceStack中的重复http请求?