{"id":521,"date":"2019-03-02T21:50:25","date_gmt":"2019-03-02T13:50:25","guid":{"rendered":"https:\/\/blog.freesilo.com\/?p=521"},"modified":"2019-03-02T21:50:25","modified_gmt":"2019-03-02T13:50:25","slug":"autofac-%e5%88%9b%e5%bb%ba%e5%ae%9e%e4%be%8b%e6%96%b9%e6%b3%95%e6%80%bb%e7%bb%93","status":"publish","type":"post","link":"https:\/\/freesilo.com\/?p=521","title":{"rendered":"autofac \u521b\u5efa\u5b9e\u4f8b\u65b9\u6cd5\u603b\u7ed3"},"content":{"rendered":"<p><strong>1\u3001InstancePerDependency<\/strong><\/p>\n<p>\u5bf9\u6bcf\u4e00\u4e2a\u4f9d\u8d56\u6216\u6bcf\u4e00\u6b21\u8c03\u7528\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u552f\u4e00\u7684\u5b9e\u4f8b\u3002\u8fd9\u4e5f\u662f\u9ed8\u8ba4\u7684\u521b\u5efa\u5b9e\u4f8b\u7684\u65b9\u5f0f\u3002<\/p>\n<p>\u5b98\u65b9\u6587\u6863\u89e3\u91ca\uff1aConfigure the component so that every dependent component or call to Resolve() gets a new, unique instance (default.)<\/p>\n<p><strong>2\u3001InstancePerLifetimeScope<\/strong><\/p>\n<p>\u5728\u4e00\u4e2a\u751f\u547d\u5468\u671f\u57df\u4e2d\uff0c\u6bcf\u4e00\u4e2a\u4f9d\u8d56\u6216\u8c03\u7528\u521b\u5efa\u4e00\u4e2a\u5355\u4e00\u7684\u5171\u4eab\u7684\u5b9e\u4f8b\uff0c\u4e14\u6bcf\u4e00\u4e2a\u4e0d\u540c\u7684\u751f\u547d\u5468\u671f\u57df\uff0c\u5b9e\u4f8b\u662f\u552f\u4e00\u7684\uff0c\u4e0d\u5171\u4eab\u7684\u3002<\/p>\n<p>\u5b98\u65b9\u6587\u6863\u89e3\u91ca\uff1aConfigure the component so that every dependent component or call to Resolve() within a single ILifetimeScope gets the same, shared instance. Dependent components in different lifetime scopes will get different instances.<\/p>\n<p><strong>3\u3001InstancePerMatchingLifetimeScope<\/strong><\/p>\n<p>\u5728\u4e00\u4e2a\u505a\u6807\u8bc6\u7684\u751f\u547d\u5468\u671f\u57df\u4e2d\uff0c\u6bcf\u4e00\u4e2a\u4f9d\u8d56\u6216\u8c03\u7528\u521b\u5efa\u4e00\u4e2a\u5355\u4e00\u7684\u5171\u4eab\u7684\u5b9e\u4f8b\u3002\u6253\u4e86\u6807\u8bc6\u4e86\u7684\u751f\u547d\u5468\u671f\u57df\u4e2d\u7684\u5b50\u6807\u8bc6\u57df\u4e2d\u53ef\u4ee5\u5171\u4eab\u7236\u7ea7\u57df\u4e2d\u7684\u5b9e\u4f8b\u3002\u82e5\u5728\u6574\u4e2a\u7ee7\u627f\u5c42\u6b21\u4e2d\u6ca1\u6709\u627e\u5230\u6253\u6807\u8bc6\u7684\u751f\u547d\u5468\u671f\u57df\uff0c\u5219\u4f1a\u629b\u51fa\u5f02\u5e38\uff1a<a href=\"http:\/\/autofac.org\/apidoc\/html\/770E317D.htm\" target=\"\" rel=\"noopener noreferrer\">DependencyResolutionException<\/a>\u3002<\/p>\n<p>\u5b98\u65b9\u6587\u6863\u89e3\u91ca\uff1aConfigure the component so that every dependent component or call to Resolve() within a ILifetimeScope tagged with any of the provided tags value gets the same, shared instance. Dependent components in lifetime scopes that are children of the tagged scope will share the parent&#8217;s instance. If no appropriately tagged scope can be found in the hierarchy an\u00a0<a href=\"http:\/\/autofac.org\/apidoc\/html\/770E317D.htm\" target=\"\" rel=\"noopener noreferrer\">DependencyResolutionException<\/a>\u00a0is thrown.<\/p>\n<p><strong>4\u3001InstancePerOwned<\/strong><\/p>\n<p>\u5728\u4e00\u4e2a\u751f\u547d\u5468\u671f\u57df\u4e2d\u6240\u62e5\u6709\u7684\u5b9e\u4f8b\u521b\u5efa\u7684\u751f\u547d\u5468\u671f\u4e2d\uff0c\u6bcf\u4e00\u4e2a\u4f9d\u8d56\u7ec4\u4ef6\u6216\u8c03\u7528Resolve()\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u5355\u4e00\u7684\u5171\u4eab\u7684\u5b9e\u4f8b\uff0c\u5e76\u4e14\u5b50\u751f\u547d\u5468\u671f\u57df\u5171\u4eab\u7236\u751f\u547d\u5468\u671f\u57df\u4e2d\u7684\u5b9e\u4f8b\u3002\u82e5\u5728\u7ee7\u627f\u5c42\u7ea7\u4e2d\u6ca1\u6709\u53d1\u73b0\u5408\u9002\u7684\u62e5\u6709\u5b50\u5b9e\u4f8b\u7684\u751f\u547d\u5468\u671f\u57df\uff0c\u5219\u629b\u51fa\u5f02\u5e38\uff1a<a href=\"http:\/\/autofac.org\/apidoc\/html\/770E317D.htm\" target=\"\" rel=\"noopener noreferrer\">DependencyResolutionException<\/a>\u3002<\/p>\n<p>\u5b98\u65b9\u6587\u6863\u89e3\u91ca\uff1aConfigure the component so that every dependent component or call to Resolve() within a ILifetimeScope created by an owned instance gets the same, shared instance. Dependent components in lifetime scopes that are children of the owned instance scope will share the parent&#8217;s instance. If no appropriate owned instance scope can be found in the hierarchy an\u00a0<a href=\"http:\/\/autofac.org\/apidoc\/html\/770E317D.htm\" target=\"\" rel=\"noopener noreferrer\">DependencyResolutionException<\/a>\u00a0is thrown.<\/p>\n<p><strong>5\u3001SingleInstance<\/strong><\/p>\n<p>\u6bcf\u4e00\u6b21\u4f9d\u8d56\u7ec4\u4ef6\u6216\u8c03\u7528Resolve()\u65b9\u6cd5\u90fd\u4f1a\u5f97\u5230\u4e00\u4e2a\u76f8\u540c\u7684\u5171\u4eab\u7684\u5b9e\u4f8b\u3002\u5176\u5b9e\u5c31\u662f\u5355\u4f8b\u6a21\u5f0f\u3002<\/p>\n<p>\u5b98\u65b9\u6587\u6863\u89e3\u91ca\uff1aConfigure the component so that every dependent component or call to Resolve() gets the same, shared instance.<\/p>\n<p><strong>6\u3001InstancePerHttpRequest<\/strong><\/p>\n<pre>\u5728\u4e00\u6b21Http\u8bf7\u6c42\u4e0a\u4e0b\u6587\u4e2d,\u5171\u4eab\u4e00\u4e2a\u7ec4\u4ef6\u5b9e\u4f8b\u3002\u4ec5\u9002\u7528\u4e8easp.net mvc\u5f00\u53d1\u3002<\/pre>\n<pre>\u5b98\u65b9\u6587\u6863\u89e3\u91ca\uff1aShare one instance of the component within the context of a single HTTP request.<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001InstancePerDependency \u5bf9\u6bcf\u4e00\u4e2a\u4f9d\u8d56\u6216\u6bcf\u4e00\u6b21\u8c03\u7528\u521b\u5efa\u4e00\u4e2a&#8230;<br \/><a class=\"read-more-button\" href=\"https:\/\/freesilo.com\/?p=521\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-521","post","type-post","status-publish","format-standard","hentry","category-csharp"],"_links":{"self":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts\/521","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=521"}],"version-history":[{"count":2,"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts\/521\/revisions"}],"predecessor-version":[{"id":1052,"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts\/521\/revisions\/1052"}],"wp:attachment":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=521"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}