Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Anders Brännmark 226 posts 277 karma points
    Oct 30, 2023 @ 14:37
    Anders Brännmark
    0

    Null exception on get_connectionstring in miniprofiler, scoped EF context in Contenment datalist?

    Getting a null exception using a scoped EF core context in Contentment datalist.

    Using this setup. EFCoreContext is wired up in the web project.

    public void ConfigureServices(IServiceCollection services)
        {
            services.AddUmbraco(_env, _config)
                .AddBackOffice()
                .AddWebsite()
                .AddDeliveryApi()
                .AddComposers()
                .Build();
            services.AddUmbracoEFCoreContext<CommerceContext>(
                _config.GetConnectionString("umbracoDbDSN"),
                _config.GetConnectionStringProviderName("umbracoDbDSN"),
                (options, providerName, connectionString) =>
                {
                    options.UseSqlServer(connectionString);
                });
        }
    

    The datalist in my core project

    public class CategoriesDataListSource : IDataListSource
        {
            private readonly IServiceScopeFactory _scopeFactory;
            public CategoriesDataListSource(IServiceScopeFactory scopeFactory)
            {
                _scopeFactory = scopeFactory;
            }
            public IEnumerable<DataListItem> GetItems(Dictionary<string, object> config)
            {
                var items = new List<DataListItem>();
                using (var scope = _scopeFactory.CreateScope())
                {
                    var commerceService = scope.ServiceProvider.GetRequiredService<ICommerceService>();
                    var dataItems = commerceService.GetAllCategories();
                    foreach (var dataItem in dataItems)
                    {
                        items.Add(new DataListItem
                        {
                            Name = dataItem.Name,
                            Value = dataItem.Id.ToString(),
                            Icon = Icon
                        });
                    }
                }
    
                return items;
            }
    }
    

    The service:

    private readonly IEFCoreScopeProvider<CommerceContext> _scopeProvider;
    private readonly ILogger<CommerceService> _logger;
    private readonly IServiceProvider _serviceProvider;
    
    public CommerceService(IEFCoreScopeProvider<CommerceContext> scopeProvider, ILogger<CommerceService> logger, IServiceProvider serviceProvider)
    {
        _scopeProvider = scopeProvider;
        _logger = logger;
        _serviceProvider = serviceProvider;
    }
    
    public IEnumerable<Category> GetAllCategories()
    {
        using (var scope = _scopeProvider.CreateScope())
        {
            var repo = _serviceProvider.GetRequiredService<ICategoryRepository>();
            return repo.GetAllAsync().Result;
        }
    }
    

    The repo:

    public interface ICategoryRepository : ICommerceRepository<Category>
    {
    }
    public class CategoryRepository : CommerceRepository<Category>, ICategoryRepository
    {
        public CategoryRepository(CommerceContext commerceContext) : base(commerceContext)
        {
    
        }
    }
    

    Results in an exception when profiledDbConnection.get_connectionstring fires

      at StackExchange.Profiling.Data.ProfiledDbConnection.get_ConnectionString()
       at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.get_ConnectionString()
       at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerConnection.get_IsMultipleActiveResultSetsEnabled()
       at Microsoft.EntityFrameworkCore.SqlServer.Query.Internal.SqlServerCompiledQueryCacheKeyGenerator.GenerateCacheKey(Expression query, Boolean async)
       at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query, CancellationToken cancellationToken)
       at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken)
       at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetAsyncEnumerator(CancellationToken cancellationToken)
       at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.System.Collections.Generic.IAsyncEnumerable<TEntity>.GetAsyncEnumerator(CancellationToken cancellationToken)
       at System.Runtime.CompilerServices.ConfiguredCancelableAsyncEnumerable`1.GetAsyncEnumerator()
       at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.<ToListAsync>d__65`1.MoveNext()
       at 
    

    Anyone seen this before? Worked before upgrade to latest of Umbraco, UI Builder and latest Contentment...

  • SC 12 posts 112 karma points
    Dec 22, 2023 @ 14:53
    SC
    0

    We came across the same thing. Any solution to it?

Please Sign in or register to post replies

Write your reply to:

Draft