Dependency Injection with REST Service

2 posts, 0 answers
  1. Aleksandar
    Aleksandar avatar
    15 posts
    Member since:
    Nov 2012

    Posted 25 Apr 2018 Link to this post

    We have implemented a custom report resolver that returns an InstanceReportSource based on the report class name. The InstanceReportSource.ReportDocument is instantiated from our IoC container (Simple Injector) and all objects are injected into the report via a constructor as expected. Within each report we subscribe to the NeedDataSource event and retrieve the data from a repository instance that has been injected. However, when we attempt to retrieve data from the repository, the repository instance has already been disposed. The injected objects are scoped to the webapi request. I read in the forums that the processing of the report happens in a separate context. Could that be causing my issue?

    We've used this approach for years with the WCF report service and it has worked well. How can we configure this to work with the REST report service? We are now using the WPF report viewer.

     

     

  2. Katia
    Admin
    Katia avatar
    693 posts

    Posted 01 May 2018 Link to this post

    Hello Aleksandar,

    Reports will need real objects, therefore, the dependency injection through a custom constructor will not be suitable. Consider the following approach for passing the repository object to the report.

    custom resolver:
    public class MyResolver : IReportResolver
        {
           public Telerik.Reporting.ReportSource Resolve(string reportStringDescription)
            {       
               Report reportInstance =null;
               if(reportStringDescription.Contains("SampleReport"))
               {    
                   // create a repository object using the application context
                   var repository = new GetRepository(new ApplicationDbContext());
                   // here you can pass a real object to the report
                   reportInstance=new Report1(repository);
               }
                return new InstanceReportSource { ReportDocument = reportInstance};
            }
        }

    report:
    public partial class Report1 : Telerik.Reporting.Report
        {
          public Report1()
            {
                InitializeComponent();
            }
           public Report1(repository)
            {
                InitializeComponent();
                this.DataSource = repository.GetData();
            }
        }


    Regards,
    Katia
    Progress Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top