简体   繁体   English

数据未更新-WebAPI中的Ninject范围

[英]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。

  • Organisation Controller have CRUD operation for organisation. 组织负责人对组织进行CRUD操作。
  • Report Controller has get method to get the report details based on id. 报表控制器具有基于ID获取报表详细信息的get方法。 Here it accepts application/json and application/pdf as Accept header 在这里它接受application / json和application / pdf作为Accept标头
  • For pdf i created custom PDF Formater and implemented the pdf with MigraDoc tool. 对于pdf,我创建了自定义PDF Formater,并使用MigraDoc工具实现了pdf。

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: 我的嫌疑犯:

  • Report1 constructor calls only one time. Report1构造函数仅调用一次。 So organisationLogic not reinitialized. 因此OrganisationLogic没有重新初始化。 It may due to incorrect dispose or ninject scope. 这可能是由于不正确的处置或注入范围引起的。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM