Autofac and autofac Chinese documents
When Autofac provides the previous methods, it also provides five events. Let's take a look at these events.
1. Five Major Events
builder.RegisterType<Person>().As<IPerson>() .OnRegistered(r => Console.WriteLine("OnRegistered")) .OnPreparing(r => Console.WriteLine("OnPreparing")) .OnActivating(r => Console.WriteLine("OnActivating")) .OnActivated(r => Console.WriteLine("OnActivated")) .OnRelease(r => Console.WriteLine("OnRelease"));Console.WriteLine("---------------");var container = builder.Build();Console.WriteLine("----------------------");var person = container.Resolve<IPerson>();Console.WriteLine("--------------------------------");person.Self();
Take a look at the results:
Well, first of all, I have to explain that the output sequence is irrelevant to the sequence in which I write events. It doesn't mean that I write events first, and the output is in front. from this figure, we can see that when an object is created, the constructor is called in the middle.
In fact, according to the general understanding, I think the calling of the constructor should be after the OnActivating event. But here I go to the front and I don't know if my dll is faulty.
Now that you have this question but cannot find the answer, you have to go to the source code.
First, let's see what OnActivating does.
This method registers events and registers handler to the ActivatingHandlers set of RegistrationData.
In the Excute (posted in the previous article) method, there is an Activate method. The method is as follows:
In this method, we can see the execution sequence of several events. For more specific source code, I will not post it. There are a lot of code. If you are interested, you can check it out by yourself.
Note that I use a constructor without parameters. If the constructor with parameters is IAnimal and IGo, The OnActivating method of IAnimal and IGo is used, it will be executed before the IPerson constructor.
builder.RegisterType<Dog>().As<IAnimal>() .OnActivating(r => Console.WriteLine("IAnimal - OnActivating")) .OnActivated(r => Console.WriteLine("IAnimal - OnActivated")) .OnRelease(r => Console.WriteLine("IAnimal - OnRelease")) .OnRegistered(r => Console.WriteLine("IAnimal - OnRegistered")) .OnPreparing(r => Console.WriteLine("IAnimal - OnPreparing"));builder.RegisterType<Go>().As<IGo>() .OnActivating(r => Console.WriteLine("IGo + OnActivating")) .OnActivated(r => Console.WriteLine("IGo + OnActivated")) .OnRelease(r => Console.WriteLine("IGo + OnRelease")) .OnRegistered(r => Console.WriteLine("IGo + OnRegistered")) .OnPreparing(r => Console.WriteLine("IGo + OnPreparing"));builder.RegisterType<Person>().As<IPerson>() .OnActivating(r => Console.WriteLine("IPerson | OnActivating")) .OnActivated(r => Console.WriteLine("IPerson | OnActivated")) .OnRelease(r => Console.WriteLine("IPerson | OnRelease")) .OnRegistered(r => Console.WriteLine("IPerson | OnRegistered")) .OnPreparing(r => Console.WriteLine("IPerson | OnPreparing"));Console.WriteLine("---------------");var container = builder.Build();Console.WriteLine("----------------------");var person = container.Resolve<IPerson>();Console.WriteLine("--------------------------------");person.Self();
Now, the execution sequence is obvious.
Refer:
Autofac components, services, and automatic assembly Article 2
Autofac documentation