We have a microservice architecture and share some code between our services using a nuget package called "HC.Framework".
When i try to build a solution that is using this nuget package (that has all the configs for NSB and stuff) i get the following exception:
1>MSBUILD : error : SqlPersistenceTask: An unhandled exception occurred:
1>MSBUILD : error : Exception:
1>MSBUILD : error : Failed to resolve assembly: 'HC.Framework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
1>MSBUILD : error : StackTrace:
1>MSBUILD : error : at Mono.Cecil.BaseAssemblyResolver.Resolve(AssemblyNameReference name, ReaderParameters parameters)
1>MSBUILD : error : at Mono.Cecil.DefaultAssemblyResolver.Resolve(AssemblyNameReference name)
1>MSBUILD : error : at Mono.Cecil.MetadataResolver.Resolve(TypeReference type)
1>MSBUILD : error : at Mono.Cecil.TypeReference.Resolve()
1>MSBUILD : error : at CecilExtensions.FindInTypeHierarchy[T](TypeDefinition type, Func`2 search)
1>MSBUILD : error : at SagaDefinitionReader.TryGetCoreSagaDefinition(TypeDefinition type, SagaDefinition& definition)
1>MSBUILD : error : at AllSagaDefinitionReader.GetSagas(Action`2 logger)
1>MSBUILD : error : at SagaWriter.WriteScripts(BuildSqlDialect dialect)
1>MSBUILD : error : at ScriptGenerator.Generate()
1>MSBUILD : error : at InnerTask.Execute()
1>MSBUILD : error : at NServiceBus.Persistence.Sql.SqlPersistenceScriptBuilderTask.Execute()
1>MSBUILD : error : Source:
1>MSBUILD : error : NServiceBus.Persistence.Sql.ScriptBuilderTask
1>MSBUILD : error :
I'm not sure what the issue is here? Below is a snippet of a saga class:
using System.Threading.Tasks;
using NServiceBus;
using HC.Swatson.Processor.Saga.Command;
using HC.Framework.NSB;
using HC.Swatson.Application.Command;
using HC.Integration.ReplyMessages.Sendgrid;
namespace HC.Swatson.Processor.Saga
{
public class SendWrongPredictionEmailSD : BaseSagaData<SendWrongPredictionEmailCMD>
{
public SendWrongPredictionEmailSD() : base(totalSteps: 2) { }
}
public class SendWrongPredictionEmailSAGA :
Saga<SendWrongPredictionEmailSD>,
IAmStartedByMessages<SendWrongPredictionEmailCMD>,
IHandleMessages<ReplyMessage<SendWrongPredictionEmail2CMD>>,
IHandleMessages<EmailHasBeenSentRM>
{
...
We also specify the following configuration in the HC.Framework package:
[assembly: SqlPersistenceSettings(
MsSqlServerScripts = true,
MySqlScripts = false,
OracleScripts = false,
PostgreSqlScripts = false,
ProduceOutboxScripts = true,
ProduceSagaScripts = true,
ProduceSubscriptionScripts = true,
ProduceTimeoutScripts = true
)]
The only maybe helpfull info i can think of is that we are upgrading from .net5 to 6, this might be related?
Any ideas on why this might ocure is welcome!
EDIT:
So after some more looking into it. If i run the scripts manually with the dotnet tool the scripts generate fine. This seems to be a issue/bug with NSB so i've openend a ticket with them.
Just install NServiceBus in your project where you want to use it
Related
We are working on creating a suite of automated playwright tests for our app through specflow. I have been trying to come up with a way to use specflow hooks to seed data before/clean up data after each test so that we can ensure that no tests are interfering with each other. Elsewhere in our program, functions are able to receive the DbContextFactory through dependency injection:
public async Task<ClientPayload> AddClientAsync(AddClientInput input, [Service] IDbContextFactory<ProjectDbContext> dbContextFactory, [Service] IMapper mapper, CancellationToken cancellationToken)
{
return new(await this.AddEntityAsync<AddClientInput, Client>(input, dbContextFactory, mapper, cancellationToken));
}
But trying to do this in a specflow hooks file like this:
public class ActivitiesHooks
{
private readonly IDbContextFactory<ProjectDbContext> myDbContextFactory;
public ActivitiesHooks([Service] IDbContextFactory<ProjectDbContext> _myDbContextFactory)
{
this.myDbContextFactory = _myDbContextFactory;
}
[BeforeScenario, Scope(Scenario = "Editing an Activity")]
public void BeforeEditingAnActivity()
{
using var dbContext = ProjectDbContextFactory.CreateDbContext();
var activityToUpdate = new Activity { ... };
dbContext.Set<Activity>().Add(activityToUpdate);
dbContext.SaveChanges();
}
generates a strange error when I try to run the actual test (It builds just fine):
Error Message:
BoDi.ObjectContainerException : Interface cannot be resolved: Microsoft.EntityFrameworkCore.IDbContextFactory`1[[Project.Framework.EntityFramework.ProjectDbContext, Project.Framework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] (resolution path: Project.Specs.Hooks.ActivitiesHooks)
TearDown : BoDi.ObjectContainerException : Interface cannot be resolved: Microsoft.EntityFrameworkCore.IDbContextFactory`1[[Project.Framework.EntityFramework.ProjectDbContext, Project.Framework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] (resolution path: Project.Specs.Hooks.ActivitiesHooks)
The hooks file is not using BoDi, although it is in use elsewhere in the code, so I'm not sure why that's included in the error. I've also tried inject just the DbContext, not the DbContextFactory, and that generates an identical error. Is there a fix for this, or is there a better way to use DbContext to seed data for tests?
I try to add-migration to one of my contexts but i find this error
Autofac.Core.DependencyResolutionException: An exception was thrown while activating weno.Infrastructure.Data.DataContext.AppDbContext.
---> Autofac.Core.DependencyResolutionException: An exception was thrown while invoking the constructor 'Void .ctor(Microsoft.EntityFrameworkCore.DbContextOptions`1
[weno.Infrastructure.Data.DataContext.AppDbContext], MediatR.IMediator)' on type 'AppDbContext'.
---> System.TypeLoadException: Method 'AppendIdentityWhereCondition' in type 'MySql.EntityFrameworkCore.MySQLUpdateSqlGenerator'
from assembly 'MySql.EntityFrameworkCore, Version=5.0.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d'
does not have an implementation.
at MySql.EntityFrameworkCore.Extensions.MySQLServiceCollectionExtensions.<>c.<AddEntityFrameworkMySQL>b__0_3(ServiceCollectionMap m)
I have DefaultInfrastructureModule code like this:
builder.RegisterGeneric(typeof(AppRepository<>))
.As(typeof(IRepository<>))
.As(typeof(IReadRepository<>))
.InstancePerLifetimeScope();
builder.RegisterGeneric(typeof(AccountingRepository<>))
.As(typeof(IRepository<>))
.As(typeof(IReadRepository<>))
.InstancePerLifetimeScope();
.
.
.
}
And my AppDbContext like this:
public class AppDbContext : DbContext
{
private readonly IMediator? _mediator;
public AppDbContext(DbContextOptions<AppDbContext> options, IMediator? mediator)
: base(options)
{
_mediator = mediator;
}
public DbSet<ToDoItem> ToDoItems => Set<ToDoItem>();
public DbSet<Project> Projects => Set<Project>();
.
.
.
}
How to fix this exception,
Note that I want to dealing with 3 source Database.
Finally I fix it in my case with change MySql.EntityFrameworkCore package to Pomelo.EntityFrameworkCore.MySql and it's worked successfully.
hello I'm getting this eroor while creating dbcontextfile in mvc
"Severity Code Description Project File Line Suppression State
Error CS0311 The type 'Pulse.Data.Entity.WHUserClaim' cannot be used as type parameter 'TUserClaim' in the generic type or method 'IdentityDbContext'. There is no implicit reference conversion from 'Pulse.Data.Entity.WHUserClaim' to 'Microsoft.AspNetCore.Identity.IdentityUserClaim'. Pulse.Data D:\projects\Pulse\Pulse.Data\AppDbContext.cs 16 Active
"
public class AppDbContext : IdentityDbContext<WHUser, WHRole, long, WHUserClaim, WHUserRole, WHUserLogin, WHRoleClaim, WHUserToken>
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
the name space which i was using in entity table for WHUserRole WHUserLogin WHUserClaim was incorrect after replacing using Microsoft.AspNet.Identity.EntityFramework to using Microsoft.AspNetCore.Identity; it work properly :)
I have problem with registration of System.Data.Entity.DbContext in Autofac.
Constructors of my classes are:
public DeviceService(IDevice device)
{
_device = device;
}
public Device(DbContext con)
{
_localDb = con;
}
I tried this :
_container = new ContainerBuilder();
_container.RegisterType<DbContext>().As<DbContext>().InstancePerLifetimeScope();
_container.RegisterType<Device>().As<IDevice>();
_container.RegisterType<DeviceService>().As<IDeviceService>();
_dependencyContainer = _container.Build();
But It didn't work because when I tried resolve _dependencyContainer.Resolve<IDeviceService>(), I got this exception:
An exception of type 'Autofac.Core.DependencyResolutionException'
occurred in Autofac.dll but was not handled in user code
Additional information: An exception was thrown while activating
WpfApplication2.DeviceService -> WpfApplication2.Device ->
System.Data.Entity.DbContext.
I can't find solution in Google. What is the solution ?
I found out that injection of System.Data.Entity.DbContext by Autofac is imposible. But you can inject inherited object of that DbContext -> MyContext: DbContext ->
_container.RegisterType<MyContext>().AsSelf()
I built my solution properly in Visual Studio, but when I build with TFS, I have several errors such as:
path\file.cs (8, 47)
path\file.cs(8,47): Error CS1002: ; expected
path\file.cs (8, 85)
path\file.cs(8,85): Error CS1519: Invalid token '(' in class, struct, or interface member declaration
Screen:
namespace path
{
public class file : Ifile
{
public IContactService ContactService => Locator.GetService<IContactService>();
public IAddressService AddressService => Locator.GetService<IAddressService>();
}
}
Thank you for helping me!
public IContactService ContactService => Locator.GetService<IContactService>();
This is an expression-bodied property and is implemented only in CSC 6 and later. Your TFS is probably running CSC 5 or earlier. You have to change it to:
public IContactService ContactService
{
get { return Locator.GetService<IContactService>(); }
}