我是ServiceStack.Text的新手,并且很难找到如何在我的项目中使用它。我做了一个nuget安装但是如何将它添加到我的格式集合中,所以它默认使用它?
我正在使用Asp.Net Core windows项目。
我不知道如何使用这个DLL我读到它需要一些自定义格式化程序,所以我用它添加它:
services.AddMvc().AddMvcOptions(options => {
options.InputFormatters.Clear();
options.InputFormatters.Add(new ServiceStackTextFormatter());
});
这是实际的课程:
public class ServiceStackTextFormatter : MediaTypeFormatter
{
//Uses ISO8601 date by default
private DateHandler _dateHandler = DateHandler.ISO8601;
public ServiceStackTextFormatter(DateHandler dateHandler)
: this()
{
_dateHandler = dateHandler;
}
public ServiceStackTextFormatter()
{
SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
SupportedEncodings.Add(new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true));
//TODO: Add XHR Header mapping. Will add this after a discussion with aspnetwebstack team: See: http://aspnetwebstack.codeplex.com/discussions/350758
}
public override Task<object> ReadFromStreamAsync(Type type, System.IO.Stream stream, HttpContent content, IFormatterLogger formatterLogger)
{
return Task.Factory.StartNew(() =>
{
using (var scope = JsConfig.BeginScope())
{
scope.DateHandler = _dateHandler;
JsConfig.DateHandler = _dateHandler;
var result = JsonSerializer.DeserializeFromStream(type, stream);
return result;
}
});
}
public override Task WriteToStreamAsync(Type type, object value, System.IO.Stream stream, HttpContent content, TransportContext transportContext)
{
return Task.Factory.StartNew(() =>
{
using (var scope = JsConfig.BeginScope())
{
scope.DateHandler = _dateHandler;
JsonSerializer.SerializeToStream(value, type, stream);
}
});
}
public override bool CanReadType(Type type)
{
if (type == null)
{
throw new ArgumentNullException("type");
}
return true;
}
public override bool CanWriteType(Type type)
{
if (type == null)
{
throw new ArgumentNullException("type");
}
return true;
}
}
但该项目给了我错误
cannot convert from 'Helpers.ServiceStackTextFormatter' to 'Microsoft.AspNetCore.Mvc.Formatters.IInputFormatter'
我有点沮丧,因为在任何地方都没有入门指南。我如何告诉.Net默认使用此DLL?
感谢任何帮助。