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:Projectsmy-appsrcmy-app.InfrastructureRepositoriesNotificationRepository.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:Projectsmy-appsrcmy-app.ApplicationServicesMessagingServicesNotificationReadingService.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
2
Answers
Migrating to .NET 7 resolved all the problems.
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.