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

ComponentRegistration Activating is not working as before #1077

Closed
ysmoradi opened this issue Feb 3, 2020 · 3 comments
Closed

ComponentRegistration Activating is not working as before #1077

ysmoradi opened this issue Feb 3, 2020 · 3 comments

Comments

@ysmoradi
Copy link

ysmoradi commented Feb 3, 2020

Is this for Autofac 5? Read the release notes! We are working on updating integration libraries but they're not all done. Please don't file issues about integration library incompatibility until you get the updated version which may not yet be published! If you really need it faster, a PR directly to the integration library will speed it up! (When the integration library is posted, file additional issues in that repo. THIS IS FOR CORE AUTOFAC ONLY.)

I've updated Signalr2 integration with a PR, please accept that (:

Try StackOverflow! If you have a usage question ("How do I...?" "Others have this working but I can't get it...") please try StackOverflow and tag your question autofac. We, along with a great community, monitor those questions.

I wasn't able to find something related to my issue

Have you read the docs?

Yes

This repo is for core Autofac only. If you are encountering issues with an integration package (MVC, Web API, etc.) please file it in the appropriate repo. Thanks!

my issue is directly related to autofac

When filing an issue, please include:

  • Assembly version information for all project dependencies (packages.config, project.json)
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Autofac" Version="4.9.4" />
  </ItemGroup>

</Project>
  • The stack trace and message of any exception(s) encountered.

There is no exception

  • If possible, a reproduction of the issue (ideally in a unit test form).

Just copy paste following codes:

class Program
    {
        static async Task Main(string[] args)
        {
            ContainerBuilder containerBuilder = new ContainerBuilder();

            containerBuilder.RegisterCallback(x =>
            {
                x.Registered += (sender, args) =>
                {
                    args.ComponentRegistration.Activating += ComponentRegistration_Activating;
                };
            });

            containerBuilder.RegisterGeneric(typeof(InMemoryRepository<>)).As(typeof(IRepository<>)).PropertiesAutowired(PropertyWiringOptions.PreserveSetValues);
            containerBuilder.RegisterType<CustomersController>().PropertiesAutowired(PropertyWiringOptions.PreserveSetValues);

            using var container = containerBuilder.Build();

            await container.Resolve<CustomersController>().AddNewCustomer();
        }

        static void ComponentRegistration_Activating(object sender, ActivatingEventArgs<object> e)
        {
            Console.WriteLine(e.Instance);
        }
    }

    public interface IRepository<TEntity>
        where TEntity : class
    {
        Task Add(TEntity entityToAdd, CancellationToken cancellationToken);
    }

    public class InMemoryRepository<TEntity> : IRepository<TEntity>
        where TEntity : class
    {
        public async Task Add(TEntity entityToAdd, CancellationToken cancellationToken)
        {

        }
    }

    public class Customer
    {

    }

    public class CustomersController
    {
        public Lazy<IRepository<Customer>> CustomersRepository { get; set; }

        public async Task AddNewCustomer()
        {
            await CustomersRepository.Value.Add(default, default);
        }
    }

In autofac 4.9.4, ComponentRegistration_Activating is being called in a way that is expected and you can see InMemoryRepository, but in autofac 5, you won't see that )-:

@tillig
Copy link
Member

tillig commented Feb 3, 2020

I'm guessing this is somehow related to the immutable container mechanism and a change in how/when events are fired. Is the callback executing after the components are registered? (I'm not able to debug into this at the moment, but that's where I'd start.) Off the top of my head I don't know what the updated code would be.

@ysmoradi
Copy link
Author

ysmoradi commented Feb 4, 2020

All other registrations are fine, only generic registrations are in trouble!

weelink added a commit to weelink/Autofac that referenced this issue Feb 7, 2020
…events after the 'ComponentRegistry' has been built
@weelink
Copy link
Contributor

weelink commented Feb 7, 2020

I looked briefly at this. The Activated seems to be working fine, the Registered event is likely the problem.

The Registered and RegistrationSourceAdded events are only raised from a IComponentRegistryBuilder. In this scenario a registration is added later in the process when no IComponentRegistryBuilder is available. It seems that is the reason the event isn't being raised.

I've created a PR to solve this problem , but it introduces a nasty design issue. I don't have a solution to this yet.

weelink added a commit to weelink/Autofac that referenced this issue Feb 7, 2020
…events after the 'ComponentRegistry' has been built
weelink added a commit to weelink/Autofac that referenced this issue Feb 12, 2020
…events after the 'ComponentRegistry' has been built
@alexmg alexmg closed this as completed in e938b26 Feb 12, 2020
@alexmg alexmg added this to the v5.1 milestone Feb 12, 2020
@tillig tillig mentioned this issue May 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants