The rendering mechanism searches the appsettings file in the wrong place

Hi team,

I have a service and it's constructor has the following definitions:

public class Data
        {
            #region “Custom properties / variables”
            [Inject]
            ValuraSecurityDataContext valuraSecurityDataContext { get; set; }

            [Inject]
            ValuraDataContext valuraDataContext { get; set; }

            #endregion

            #region “Startup processes”
            public Data()
            {
#if DEBUG
                var config = new ConfigurationBuilder()
                            .AddJsonFile("appsettings.Development.json")
                            .Build();
#else
                var config = new ConfigurationBuilder()
                            .AddJsonFile("appsettings.json")
                            .Build();
#endif

                var optionsBuilder1 = new DbContextOptionsBuilder<ValuraSecurityDataContext>();
                optionsBuilder1.UseSqlServer(config.GetConnectionString("ValuraSecurityDataConnection"));
                valuraSecurityDataContext = new ValuraSecurityDataContext(optionsBuilder1.Options);

                var optionsBuilder2 = new DbContextOptionsBuilder<ValuraDataContext>();
                optionsBuilder2.UseSqlServer(config.GetConnectionString("ValuraDataConnection"));
                valuraDataContext = new ValuraDataContext(optionsBuilder2.Options);
            }
...
...
}

When I inject this service into a page the following errors occur and the page cannot be rendered at design time. But the app runs without error.

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.IO.FileNotFoundException: The configuration file 'appsettings.Development.json' was not found and is not optional. The expected physical path was 'C:\Program Files\Radzen Blazor Studio\resources\Radzen.Server\appsettings.Development.json'.
   at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean reload)
   at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList`1 providers)
   at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
   at Valura.Utilities.Data..ctor()
   at ValuraApplication.Pages.ApplicationUsers..ctor()
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean wrapExceptions)
   --- End of inner exception stack trace ---
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean wrapExceptions)
   at Microsoft.AspNetCore.Components.DefaultComponentActivator.CreateInstance(Type componentType)
   at Microsoft.AspNetCore.Components.ComponentFactory.InstantiateComponent(IServiceProvider serviceProvider, Type componentType)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.InstantiateChildComponentOnFrame(RenderTreeFrame& frame, 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, ArrayRange`1 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)
   at Radzen.Server.Router.OnRouteChanged(Object sender, RouteChangedEventArgs e)
   at Radzen.Server.RouterService.Route(Type component, Type defaultLayout)
   at Radzen.Server.CircuitHost.AddComponent(Type layout, Type page)
   at Radzen.Server.ProjectServer.Render(String fileName, String source)
   at Radzen.Server.ProgramController.Render(RenderRequest request)

The rendering mechanism searches the appsettings file in the wrong place. Can you check out it, please?

I am afraid we can't support such code in design time as the current directory during design time cannot be set that easily (hence the error you are getting).

You can do one of the following:

  1. Inject IConfiguration in your component/service. This is the recommended approach to work with appsettings.
  2. Use the preprocessor to hide this code from the design time build.
1 Like