Hi,
I recently added a scoped service to my app which causes Radzen to raise an exception when I open the editor with a page that injects this service. The actual problem seems to be caused by the initialization code of my service. But what is even more strange: If I comment out this code, the error remains. It looks like Radzen is parsing the C# in the comments! If I replace the code in the comments with some text like "Hello World" - it all of a sudden works again.
Here is the code from my Program.cs:
builder.Services.AddScoped<TestService>(op =>
{
// builder.Services.
return new TestService();
});
Opening a page that injects the "TestService" will fail with this exception:
"The page cannot render":
Cannot provide a value for property 'TestService' on type 'MyRepoTestProj.Components.Pages.Index'. There is no registered service of type 'MyRepoTestProj.Services.TestService'.
at Microsoft.AspNetCore.Components.ComponentFactory.<>c__DisplayClass9_0.g__Initialize|1(IServiceProvider serviceProvider, IComponent component)
at Microsoft.AspNetCore.Components.ComponentFactory.InstantiateComponent(IServiceProvider serviceProvider, Type componentType, IComponentRenderMode callerSpecifiedRenderMode, Nullable1 parentComponentId) at Microsoft.AspNetCore.Components.RenderTree.Renderer.InstantiateChildComponentOnFrame(RenderTreeFrame[] frames, Int32 frameIndex, Int32 parentComponentId) at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.InitializeNewComponentFrame(DiffContext& diffContext, Int32 frameIndex) at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.InitializeNewSubtree(DiffContext& diffContext, Int32 frameIndex) at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.InsertNewFrame(DiffContext& diffContext, Int32 newFrameIndex) at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForRange(DiffContext& diffContext, Int32 oldStartIndex, Int32 oldEndIndexExcl, Int32 newStartIndex, Int32 newEndIndexExcl) at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.ComputeDiff(Renderer renderer, RenderBatchBuilder batchBuilder, Int32 componentId, ArrayRange1 oldTree, ArrayRange`1 newTree)
at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment renderFragment, Exception& renderFragmentException)
at Microsoft.AspNetCore.Components.RenderTree.Renderer.ProcessRenderQueue()
--- End of stack trace from previous location ---
at Radzen.Server.RemoteRenderer.HandleException(Exception exception)
at Microsoft.AspNetCore.Components.RenderTree.Renderer.ProcessRenderQueue()
at Microsoft.AspNetCore.Components.RenderTree.Renderer.AddToRenderQueue(Int32 componentId, RenderFragment renderFragment)
at Radzen.Server.Router.Render(Type layout, Type component, Boolean renderLayout)
at Radzen.Server.Router.OnRouteChanged(Object sender, RouteChangedEventArgs e)
at Radzen.Server.RouterService.Route(Type component, Type defaultLayout, Boolean renderLayout)
at Radzen.Server.CircuitHost.AddComponent(Type layout, Type page, Boolean renderLayout)
at Radzen.Server.ProjectServer.Render(String fileName, String source, Boolean renderLayout)
at Radzen.Server.ProgramController.Render(RenderRequest request)
If you remove the comment or replace the text of the comment with something that is not C#, it will work again!!
My actual problem is that I want to create a scoped service that has some initialization code:
builder.Services.AddScoped<MyApp.Services.MyService>(sp =>
{
var httpClientFactory = sp.GetRequiredService<IHttpClientFactory>();
var httpClient = httpClientFactory.CreateClient("MyAPI");
return new MyApp.Services.MyService(builder.Configuration["MyAPI:Url"], httpClient);
});
The two lines before the return also cause the exception. I commented them out and the problem was still there.
So this looks like two separate problems: The one is that you seem to parse code that is actually a comment and the other is that the code in the service constructor does not work with Radzen.
Can you confirm this issue?