-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Closed
Closed
Copy link
Labels
area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesquestion
Description
Describe the bug
In my project that was Core 2.2 i have standard service for returning Razor View as string (i needed it to generate pdf in my client written in WPF):
public class RaportService : IRaportService
{
private readonly IProjectRepository projectRepository;
private readonly IRazorViewEngine razorViewEngine;
private readonly ITempDataProvider tempDataProvider;
private readonly IServiceProvider serviceProvider;
public RaportService(
IProjectRepository projectRepository,
IRazorViewEngine razorViewEngine,
ITempDataProvider tempDataProvider,
IServiceProvider serviceProvider)
{
this.projectRepository = projectRepository;
this.razorViewEngine = razorViewEngine;
this.tempDataProvider = tempDataProvider;
this.serviceProvider = serviceProvider;
}
public async Task<string> GenerateProjectRaport(int projectId)
{
var project = await this.projectRepository.GetProjectWithTasksAsync(projectId)
?? throw new EntityNotFoundException();
var httpContext = new DefaultHttpContext { RequestServices = this.serviceProvider };
var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
using (var stringWriter = new StringWriter())
{
string viewPath = "~/wwwroot/RaportTemplate.cshtml";
var viewResult = this.razorViewEngine.GetView(viewPath, viewPath, false);
var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary())
{
Model = new RaportViewModel
{
Project = project,
ProjectTasks = project.Tasks.ToList(),
ProjectEndedTasks = project.EndedTasks.ToList(),
}
};
var viewContext = new ViewContext(
actionContext,
viewResult.View,
viewDictionary,
new TempDataDictionary(actionContext.HttpContext, this.tempDataProvider),
stringWriter,
new HtmlHelperOptions());
await viewResult.View.RenderAsync(viewContext);
return stringWriter.ToString();
}
}
}In Core 2.2 everything work fine. When i updated my project to Core 3.0 i get this error (i didnt add new injection into my Startup.cs):
StackTrace:
{
"stackTrace":" at MediatR.Internal.RequestHandlerBase.GetHandler[THandler](ServiceFactory factory)\r\n
at MediatR.Internal.RequestHandlerWrapperImpl`2.<>c__DisplayClass0_0.<Handle>g__Handler|0()\r\n at MediatR.Pipeline.RequestPostProcessorBehavior`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)\r\n
at MediatR.Pipeline.RequestPreProcessorBehavior`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)\r\n
at TaskManager.Api.Controllers.RaportController.GetProjectRaport(Int32 projectId) in C:\\Users\\Michał\\Source\\Repos\\michasacuer\\TaskManager\\Src\\Web\\TaskManager.Api\\Controllers\\RaportController.cs:line 12\r\n at lambda_method(Closure , Object )\r\n
at Microsoft.Extensions.Internal.ObjectMethodExecutorAwaitable.Awaiter.GetResult()\r\n
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)\r\n
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)\r\n
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)\r\n
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)\r\n at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)\r\n
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextExceptionFilterAsync>g__Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)",
"message":"Error constructing handler for request of type MediatR.IRequestHandler`2[TaskManager.Application.Raport.Queries.GetProjectRaport.GetProjectRaportQuery,System.String]. Register your handlers with the container. See the samples in GitHub for examples."}Even when i register that IRazorViewEngine interface and class in Startup it still throwing, thtat he need something that calls IRazorPageFactoryProvider but i dont know, what is an implementation of it.
So how to get Razor View Engine working with .NET Core 3.0?
Further technical details
- ASP.NET Core version:
Core 3.0.1
Metadata
Metadata
Assignees
Labels
area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesquestion