[英]Data Not getting Updated - Ninject Scope in WebAPI
I am working in web api with ninject as dependency resolver 我正在使用ninject作为依赖解析器的Web API
Scenario: 场景:
I need to generate a report as pdf which includes report data and the organisation address as report header. 我需要生成一个PDF格式的报告,其中包括报告数据和组织地址作为报告标题。
Current Implementation: 当前实施:
I have 2 controllers namely OrganisationController and ReportController. 我有2个控制器,分别是OrganisationController和ReportController。
Problem: 问题:
While creating pdf, i access organisation data for the header. 创建pdf时,我访问标头的组织数据。 Pdf generated correctly and it includes organisation data as well.
Pdf正确生成,并且还包括组织数据。 After i updated organisation data using organisation controller put method.
在我使用组织控制器放置方法更新组织数据之后。 When i create report again, it shows the old organisation data.
当我再次创建报告时,它显示了旧的组织数据。
My suspects: 我的嫌疑犯:
MyCode: 我的代码:
ninject register code: 注入寄存器代码:
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IOrganisationDetailLogic>().To<OrganisationDetailLogic>();
kernel.Bind<IOrganisationDetailRepository>().To<OrganisationDetailRepository>();
kernel.Bind<IReport>().To<Report1>();
kernel.Bind<PdfMediaTypeFormatter>().ToSelf();
}
Report class: 报告类别:
public class Report1 : IReport
{
private readonly IOrganisationDetailLogic _organisationLogic;
public Report1(IOrganisationDetailLogic organisationLogic)
{
_organisationLogic = organisationLogic;
}
public async Task<MemoryStream> Create(object model)
{
MemoryStream stream = null;
Document document = new Document();
//Report Header
SetHeader(section);
//Report Data here
//Footer
SetFooter(section);
//Render as PDF
return stream;
}
private void SetHeader(Section section)
{
//Here we are getting organisation data
var organisationDetail = _organisationLogic.GetActiveOrganisations().First();
}
}
Please help where i missed. 请帮助我错过的地方。 When i initiated _organisationLogic in the SetHeader itself, its working correct
当我在SetHeader本身中启动_organisationLogic时,它的工作正常
Update: I am adding my custom formatter class. 更新:我正在添加我的自定义格式化程序类。
public class PdfMediaTypeFormatter : MediaTypeFormatter
{
private readonly string mediaType = "application/pdf";
Func<Type, bool> typeisIPdf = (type) => typeof(IPdf).IsAssignableFrom(type);
Func<Type, bool> typeisIPdfCollection = (type) => typeof(IEnumerable<IPdf>).
IsAssignableFrom(type);
private readonly IReport _report;
public PdfMediaTypeFormatter(IReport report)
{
SupportedMediaTypes.Add(new MediaTypeHeaderValue(mediaType));
MediaTypeMappings.Add(new UriPathExtensionMapping("pdf", new MediaTypeHeaderValue(mediaType)));
this._report = report;
}
public async override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
{
var memoryStream = await _report.Create(value);
var bytes = memoryStream.ToArray();
await writeStream.WriteAsync(bytes, 0, bytes.Length);
}
//other methods skipped
}
I read that constructor injection not support in custom formatter please suggest 我读到自定义格式化程序不支持构造函数注入,请提出建议
I suspect the scope is what is causing your issue. 我怀疑范围是引起您问题的原因。 Try adding .InRequestScope() to relevant registrations to ensure a new instance is a created per request.
尝试将.InRequestScope()添加到相关注册中,以确保为每个请求创建一个新实例。
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IOrganisationDetailLogic>().To<OrganisationDetailLogic>().InRequestScope();
kernel.Bind<IOrganisationDetailRepository>().To<OrganisationDetailRepository>().InRequestScope();
kernel.Bind<IReport>().To<Report1>();
kernel.Bind<PdfMediaTypeFormatter>().ToSelf();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.