3. Resolving Services and resolvingservices

Source: Internet
Author: User

3. Resolving Services and resolvingservices

When you complete component registration, after exposing a component as an appropriate service, you can resolve the service through the sub-lifecycle domain of the container or container (After you have your components registered with appropriate services exposed, you can resolve services from the built container and child lifetime scopes ). You can use the Resolve () method to parse the service.

var builder = new ContainerBuilder();builder.RegisterType<MyComponent>.As<IService>();var container = builder.Build();using(var scope = container.BeginLifetimeScope()){
  var service = scope.Resolve<IService>(); }

You may have noticed that the sub-lifecycle domain of the container is used to parse the service rather than directly using the container to pick up the service-you should also resolve the service in this way.

Although the container can also be used directly to parse the service, if you do so, the application memory may leak. Therefore, we recommend that you always try to resolve the service in the Self-lifecycle domain, so that autofac can help you properly process object destruction and garbage collection. You will learn more details in future chapters.

When parsing a service, autofac automatically connects to the entire dependency layer of the service and automatically component the dependencies required by the Service. If your program contains a missing cyclic dependency or dependency, the program will throw a DependencyResolutionException.

Suppose you have a service, but you cannot determine whether it has been registered to the container. You can try to use the ResolveOptional () or TryResolve () method for condition parsing:

// If the IService service is already registered, it will be correctly parsed. // If the IService service is not registered, this method returns nullvar service = scope. resolveOptional <IService> (); // If IProvider is registered, the provider obtains the object. // If IProvider is not registered, then you can perform other operations IProvider provider = nullif (scope. tryResolve <IProvider> (out provider) {// The service is successfully parsed}

3.1 PASS Parameters to Resolve)

Continue tomorrow...

 

PS: this series of blogs is a translation of autofac English documents. It aims to improve your English Reading Ability and help people in need. The original Article address is http://autofac.readthedocs.org/en/latest/getting-started/index.html.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.