Git Product home page Git Product logo

Comments (7)

andreujuanc avatar andreujuanc commented on July 27, 2024 1

I'm using 2.1.1 and it does work properly.
What's your setup like?

from opencqrs.

asokalskyy avatar asokalskyy commented on July 27, 2024 1

:) I got this issue during xUnit integration tests creation first. To get integration things work right I have injected all in test constructor:

    public SomeTestConstructor()
    {
        IConfiguration configuration = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json", false, true)
            .Build();

        var service = new ServiceCollection();

        service
            .AddOpenCqrs(typeof(TestCommand))
            .AddSqlServerProvider(configuration)
            .AddServiceBusProvider(configuration);

        _serviceProvider = service.BuildServiceProvider();

        var builder = new AppStub(_serviceProvider);
        builder
            .UseOpenCqrs()
            .EnsureDomainDbCreated();

        _testItemId = Guid.NewGuid();
    }

When I got the error i have tried to inject the IHttpContextAccessor by injecting it to service:

services.AddHttpContextAccessor();

The issue was not resolved. So I have cloned the repository, and used service locator for resolver:

public class HandlerResolver : IHandlerResolver
{
    private readonly IServiceProvider _serviceProvider;

    public HandlerResolver(IServiceProvider serviceProvider)
    {
        _serviceProvider = serviceProvider;
    }

    public THandler ResolveHandler<THandler>()
    {
        var handler = _serviceProvider.GetService<THandler>();

        if (handler == null)
            throw new HandlerNotFoundException(typeof(THandler));

        return handler;
    }
}

Also I have removed Resolver class and interface, changing references to IServiceProvider directly.
Maybe this was not the best solution, but this was the hot fix to make things work. Hopefully MS will do solve lazy object resolving issue sometimes. :)

from opencqrs.

asokalskyy avatar asokalskyy commented on July 27, 2024 1

Emm... I just forgot to clarify, that was really surprised, when found the error in Web API response. This was the main reason WHY I used service locator pattern for IoC container.

Startup file of API:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddAutoMapper();
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        // In production, the Angular files will be served from this directory
        services.AddSpaStaticFiles(configuration =>
        {
            configuration.RootPath = "ClientApp/dist";
        });

        services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(o =>
            {
                o.Authority = Configuration["Authentication:AADInstance"]
                              + Configuration["Authentication:TenantId"];
                o.Audience = Configuration["Authentication:ClientId"];
                o.TokenValidationParameters = new TokenValidationParameters
                {
                    RoleClaimType = ClaimTypes.Role,
                    ValidateIssuer = true
                };
            });

        services.AddDbContext<EfCoreContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("SqlConnection"),
                db => db.MigrationsAssembly("SomeProject.Web")
                )
            );

        services.Configure<AADOptions>(Configuration.GetSection("Authentication"));

        services.AddTransient<AADUserSyncService>();
        services.AddTransient<GraphApiUserService>();


        services.AddOpenCqrs(typeof(CreateFeedbackCommand))
            .AddSqlServerProvider(Configuration)
            .AddServiceBusProvider(Configuration);
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseSpaStaticFiles();

        app.UseAuthentication();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller}/{action=Index}/{id?}");
        });

        app.UseSpa(spa =>
        {
            // To learn more about options for serving an Angular SPA from ASP.NET Core,
            // see https://go.microsoft.com/fwlink/?linkid=864501

            spa.Options.SourcePath = "ClientApp";

            if (env.IsDevelopment())
            {
                spa.UseAngularCliServer(npmScript: "start");
            }
        });

        app.UseOpenCqrs().EnsureDomainDbCreated();
    }
}

from opencqrs.

lucabriguglia avatar lucabriguglia commented on July 27, 2024

I'll have a look as soon as I can, thanks.

from opencqrs.

lucabriguglia avatar lucabriguglia commented on July 27, 2024

This is a know bug (#27) but it should happen only on console apps.

from opencqrs.

lucabriguglia avatar lucabriguglia commented on July 27, 2024

Thanks @asokalskyy I'll fix this issue in the next release.

from opencqrs.

lucabriguglia avatar lucabriguglia commented on July 27, 2024

Fixed in Version 5.3

from opencqrs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.