.net-corereporting-servicesdevexpressreporting

Using DevExpress XtraReport Constructor with parameter


I am creating reports with .NET Core Web API. I am using object data source as data source in my reports. I need to access domain services to get the data for these reports. I use DI with Autofac in the project. How can I do this with Devexpress?


Solution

  • I would like to share the solution I have found for those who have the same problem as me.

    public class CustomWebDocumentViewerReportResolver : IWebDocumentViewerReportResolver
     {
         private readonly IServiceProvider _serviceProvider;
    
         public CustomWebDocumentViewerReportResolver(IServiceProvider serviceProvider)
         {
             _serviceProvider = serviceProvider;
         }
    
         public XtraReport Resolve(string reportEntry)
         {
             switch (reportEntry)
             {
                 case "Report1":
                     return ActivatorUtilities.CreateInstance<Report1>(_serviceProvider);
                 default:
                     Type t = Type.GetType(reportEntry);
                     return typeof(XtraReport).IsAssignableFrom(t) ?
                         (XtraReport)Activator.CreateInstance(t) :
                         null;
             }
         }
     }
    
    private readonly ITest1 _test1;
    private readonly ITest2 _test2;
    
    public Report1(ITest1 test1, ITest2 test2)
    {
        InitializeComponent();
        _test1 = test1;
        _test2 = test2;
    }