Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.Net Core 3.1 Migration #7

Open
abhishek7911 opened this issue Jun 27, 2020 · 5 comments
Open

.Net Core 3.1 Migration #7

abhishek7911 opened this issue Jun 27, 2020 · 5 comments

Comments

@abhishek7911
Copy link

I have try to convert in .net Core 3.1 but some how it's not working with existing architecture.

@mehmetozkaya
Copy link
Member

yes I think it is required major changes for migrating new version. For now, I am waiting .net 5 to upgrade new version because this repository needs to huge refactoring. That's why lets leave it as it is since .net 5 is raised.

@abhishek7911
Copy link
Author

ok thanks for your quick response i have migrated every thing login is working fine but getting error in DI. i have take reference from "https://docs.microsoft.com/en-us/dotnet/architecture/microservices/microservice-ddd-cqrs-patterns/microservice-application-layer-implementation-web-api" but still it's not fixed. getting 404 while requesting API controller. can you help with this part?

please review below changes.

  1. startup
 services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

                var container = new ContainerBuilder();
                container.Populate(services);

                container.RegisterModule(new MediatorModule());
                container.RegisterModule(new InfrastructureModule());
                container.RegisterModule(new ApplicationModule());

                return new AutofacServiceProvider(container.Build());

  1. Mediator Module
public class MediatorModule : Autofac.Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterAssemblyTypes(typeof(CreateProductCommandHandler).GetTypeInfo().Assembly)
               .AsClosedTypesOf(typeof(IRequestHandler<,>));

            // Register the Command's Validators (Validators based on FluentValidation library)
            builder.RegisterAssemblyTypes(typeof(CreateProductRequestValidator).GetTypeInfo().Assembly)
                .Where(t => t.IsClosedTypeOf(typeof(IValidator<>)))
                .AsImplementedInterfaces();           

        }
    }
  1. InfrastructureModule
 public class InfrastructureModule
        :Autofac.Module
    {

        protected override void Load(ContainerBuilder builder)
        {
            builder.Register<ServiceFactory>(context =>
            {
                var componentContext = context.Resolve<IComponentContext>();
                return t => { object o; return componentContext.TryResolve(t, out o) ? o : null; };
            });

            builder.RegisterType<ProductRepository>().As<IProductRepository>().InstancePerDependency();
            builder.RegisterType<CategoryRepository>().As<ICategoryRepository>().InstancePerDependency();
            builder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>)).InstancePerDependency();
            builder.RegisterGeneric(typeof(EnumRepository<>)).As(typeof(IEnumRepository<>)).InstancePerDependency();
            builder.RegisterGeneric(typeof(RepositoryBase<,>)).As(typeof(IRepositoryBase<,>)).InstancePerDependency();

            builder.RegisterGeneric(typeof(LoggerAdapter<>)).As(typeof(IAppLogger<>)).InstancePerDependency();


            builder.RegisterAssemblyTypes(typeof(IMediator).GetTypeInfo().Assembly)
                .AsImplementedInterfaces();


            builder.RegisterGeneric(typeof(TransactionBehaviour<,>)).As(typeof(IPipelineBehavior<,>));

        }
    }
  1. Application Module
 public class ApplicationModule
        : Autofac.Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterType<ProductService>().As<IProductService>().InstancePerLifetimeScope();
            builder.RegisterType<CategoryService>().As<ICategoryService>().InstancePerLifetimeScope();
            builder.RegisterType<CustomerService>().As<ICustomerService>().InstancePerLifetimeScope();
        }
    }

@mehmetozkaya
Copy link
Member

yes the article is very good reference to take for this repo refactoring. Actually I cant see any problem with registering classes into Autofac. When you get the 404 error, is this makes problem to cant resolve any interfaces ?
Since I cant simulate the error with existing codebase, its hard to find solution with this way :/

@abhishek7911
Copy link
Author

it resolved on the same day. as we discussed it's DI issues. after that i followed below autofac documentation, for configuring DI and it's working fine.
https://autofaccn.readthedocs.io/en/latest/integration/aspnetcore.html

@mehmetozkaya
Copy link
Member

great news. so if you want to send pull request I would like to review and merge it. For frontend part we will check later. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants