0

I am using .NET 6 and PostgreSQL database.

I have a hosted service, which periodically calls repository's function and is querying database entities using EF Core as so:

public class NotificationRepository : INotificationRepository
    {
        public async Task<List<Notification>> GetUnreadDeviceNotifications()
        {
            using var context = new MyDbContext();
            return await context.Notifications.Where(x => !x.IsSentTimestamp.HasValue && x.ChannelId == 8).ToListAsync();
        }
    }

As soon as I deploy on linux machine and every time service calls GetUnreadDeviceNotifications I get this error:

Aug 07 12:50:46 my-app my-app.Web[1432629]: System.IO.FileNotFoundException: Could not load file or assembly 'Npgsql, Version=6.0.8.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7'. The system cannot find the file specified.
Aug 07 12:50:46 my-app my-app.Web[1432629]: File name: 'Npgsql, Version=6.0.8.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7'
Aug 07 12:50:46 my-app my-app.Web[1432629]:    at Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure.Internal.NpgsqlOptionsExtension.ExtensionInfo.GetServiceProviderHashCode()
Aug 07 12:50:46 my-app my-app.Web[1432629]:    at Microsoft.EntityFrameworkCore.DbContextOptions.GetHashCode()
Aug 07 12:50:46 my-app my-app.Web[1432629]:    at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd[TArg](TKey key, Func`3 valueFactory, TArg factoryArgument)
Aug 07 12:50:46 my-app my-app.Web[1432629]:    at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.GetOrAdd(IDbContextOptions options, Boolean providerRequired)
Aug 07 12:50:46 my-app my-app.Web[1432629]:    at Microsoft.EntityFrameworkCore.DbContext.get_ContextServices()
Aug 07 12:50:46 my-app my-app.Web[1432629]:    at Microsoft.EntityFrameworkCore.DbContext.get_Model()
Aug 07 12:50:46 my-app my-app.Web[1432629]:    at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityType()
Aug 07 12:50:46 my-app my-app.Web[1432629]:    at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityQueryable()
Aug 07 12:50:46 my-app my-app.Web[1432629]:    at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.System.Linq.IQueryable.get_Provider()
Aug 07 12:50:46 my-app my-app.Web[1432629]:    at System.Linq.Queryable.Where[TSource](IQueryable`1 source, Expression`1 predicate)
Aug 07 12:50:46 my-app my-app.Web[1432629]:    at my-app.Infrastructure.Repositories.NotificationRepository.GetUnreadDeviceNotifications() in C:\Projects\my-app\src\my-app.Infrastructure\Repositories\NotificationRepository.cs:line 22
Aug 07 12:50:46 my-app my-app.Web[1432629]:    at my-app.Application.Services.MessagingServices.NotificationReadingService.DoWork(Object state) in C:\Projects\my-app\src\my-app.Application\Services\MessagingServices\NotificationReadingService.cs:line 35

These are dependencies I have:

  <ItemGroup>
    <PackageReference Include="Dapper" Version="2.0.123" />
    <PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="10.0.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.3" />
    <PackageReference Include="Npgsql" Version="6.0.3" />
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.3" />
  </ItemGroup>

P.S. It seems that it is working fine on Windows

6
  • 1
    You have referenced version 6.0.3 but it's looking for 6.0.8, and not finding it.
    – Yogi
    Commented Aug 7, 2023 at 13:01
  • I kinda understand that, but why is it looking for 6.0.8 then? I have tried updating it to 6.0.8 but it got even worse Commented Aug 7, 2023 at 13:04
  • You can try removing the references and add them again. Also try text search to find out where it's picking. Hope you are using nuget package manager for adding references.
    – Yogi
    Commented Aug 7, 2023 at 13:06
  • That's what I already did.. I've searched for '6.0.8' and found nothing. I also re-installed most of the packages. Commented Aug 7, 2023 at 13:08
  • Also, if I update my Npgsql package to 6.0.9 it gives the same error, but with 6.0.9 version Commented Aug 7, 2023 at 13:15

2 Answers 2

1

Actually I have a project with the following configuration:

Project Config

Program.cs or StartUp.cs

My Hosted Service

I can connect to the database and get query results.

Remember to declare in your Startup.cs or Program.cs your repository services. Another one important thing to note, is that you cannot access to the repository instance from the Hosted Service, because it's a Singleton, so the best approach to reach your repository is to use an IServiceProvider instance. My English speaking is not good at all.

0

Migrating to .NET 7 resolved all the problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.