Posted 28 January 2021, 5:37 pm EST
I need to be able to use DependencyInjection in the GetCustomStore routine used in the app.UseReporting of Startup.cs. Is there a way to do this?
Using ActiveReports 15 & Net.core 3.1
Forums Home / ActiveReports / ActiveReports v7+
Posted by: rgreen on 28 January 2021, 5:37 pm EST
Posted 28 January 2021, 5:37 pm EST
I need to be able to use DependencyInjection in the GetCustomStore routine used in the app.UseReporting of Startup.cs. Is there a way to do this?
Using ActiveReports 15 & Net.core 3.1
Posted 28 January 2021, 11:07 pm EST
Hello,
You can use the CustomStore as follow:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseReporting(settings =>
{
settings.UseCustomStore(GetReport);
settings.UseCompression = true;
});
app.UseMvc();
}
private object GetReport(string arg)
{
GrapeCity.ActiveReports.PageReport def = new GrapeCity.ActiveReports.PageReport();
//load the report layout
def.Load(new System.IO.FileInfo(Application.StartupPath + "\RuntimeBinding.rdlx"));
return def.Report;
}
Hope it helps.
Thanks,
Mohit
Posted 29 January 2021, 10:06 am EST
Is there a way to pass another variable with the GetReport method?
Posted 1 February 2021, 12:18 am EST
Hello,
No, there is no way to pass the another variable. Can you please tell which variable you want to pass in the GetReport method and share the user case so that I can help you accordingly.
Thanks,
Mohit
Posted 1 February 2021, 10:48 am EST
Hello,
What I really need to do is get the dependency from the current scope in GetCustomStore.
Posted 1 February 2021, 11:39 am EST
I figured it out.
In Startup.cs ConfigureServices add HttpContextAccessor and register your report.
services.AddHttpContextAccessor();
services.AddTransient<TestReport>();
In Startup.cs Configure, add in a parameter for the factory.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IReportFactory reportFactory)
For the useReport in Startup.cs Configure, change the UseCustomStore to accept reportFactory.
app.UseReporting(settings =>
{
settings.UseEmbeddedTemplates(EmbeddedReportsPrefix, Assembly.GetAssembly(GetType()));
settings.UseCompression = true;
settings.UseCustomStore((name) => GetReport(reportFactory, name));
});
In the factory class, add IHttpContextAccessor. Use this to get the correct report.
public Object GetReport(String reportName)
{
var reportType = Type.GetType($"Namespace.{reportName},Assembly");
return report = contextAccessor.HttpContext.RequestServices.GetService(reportType);
}