AUTOFAC Official document (11) "Use Life cycle scope"

Source: Internet
Author: User
Create a new lifecycle scope

You can create a life cycle scope by invoking the Beginlifetimescope () method from the root container on any existing lifecycle scope. Lifecycle scopes are can be destroyed, they track the disposition of components, so make sure you always call "Dispose ()" or wrap them in a "using" statement.

using (var scope = container. Beginlifetimescope ())
{
  //from the scope that is the child of the root container to resolve the service
  var service = scope. Resolve<iservice> ();

  You can also create nested scopes ...
  using (var Unitofworkscope = scope. Beginlifetimescope ())
  {
    var anotherservice = unitofworkscope.resolve<iother> ();
  }
}
Mark life cycle Scope

In some cases, you want to share services across a unit of work, but you do not want to share them globally, as in the case of single mode. A common example in a Web application is the "Per-request" lifecycle. (You can read more about the per-request scope in the instance Scope topic.) In this case, you need to mark the lifecycle scope and register the service as Instancepermatchinglifetimescope ().

For example, suppose you have a component that sends e-mail messages. Logical transactions in the system may need to send multiple e-mail messages, so you can share the component between parts of a logical transaction. However, you do not want the e-mail component to become a global single example. Your settings may look like this:

Register your transaction-level shared components as instancepermatchinglifetimescope and provide them with a "known tag" that you will use when starting a new transaction.
var builder = new Containerbuilder (); Builder. Registertype<emailsender> (). As<iemailsender> ().

Instancepermatchinglifetimescope ("Transaction");
Both the order processor and the receipt manager need to send e-mail notifications. Builder. Registertype<orderprocessor> ().
As<iorderprocessor> (); Builder. Registertype<receiptmanager> ().

As<ireceiptmanager> (); var container = Builder.


Build (); Use the label to create the transaction scope using (var TransactionScope = container. Beginlifetimescope ("transaction")) {using (var Orderscope = Transactionscope.beginlifetimescope ()) {//This resolves a iemai
    Lsender is used, but Iemailsender will "live" within the scope of the parent transaction, and because the token is shared between any child nodes in the transaction scope.
    var op = orderscope.resolve<iorderprocessor> (); Op.
  ProcessOrder (); using (var receiptscope = Transactionscope.beginlifetimescope ()) {//This will also parse a iemailsender use, but it will find the existing Iemailsen within the parent scope Der, and use it.
    This will be the same instance used by the order processor.
  var rm = receiptscope.resolve<ireceiptmanager> ();  Rm.
  Sendreceipt (); }
}

Again, you can read more about the scope of markup and per-request in the instance scope topic. add registration to life cycle scope

AUTOFAC allows you to add registrations "at any time" when creating a lifecycle scope. This can help you when you need some kind of "spot welding" limited registration overlay, or you usually just need something that you do not want to register on a global scale. This is done by passing a lambda to the beginlifetimescope () that uses containerbuilder and adds a registration.

using (var scope = container. Beginlifetimescope (
  builder =>
  {
    Builder. Registertype<override> (). As<iservice> ();
    Builder. Registermodule<mymodule> ();
  }
) {
  //additional registration will only be available within this lifecycle.
}

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.