{"id":180307,"date":"2013-05-20T21:44:04","date_gmt":"2013-05-20T17:44:04","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=180307"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=180307","title":{"rendered":"<span class=\"post_title\">\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 \u043e\u0431\u043b\u0430\u0441\u0442\u0435\u0439 \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0432 MEF<\/span>"},"content":{"rendered":"<div class=\"content html_format\">   \t\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435, \u0436\u0438\u0442\u0435\u043b\u0438 \u0445\u0430\u0431\u0440\u0430.<br \/>  Managed Extensibility Framework aka MEF, \u0447\u0442\u043e \u0431\u044b \u043d\u0435 \u0433\u043e\u0432\u043e\u0440\u0438\u043b\u0438 \u043b\u044e\u0431\u0438\u0442\u0435\u043b\u0438 \u043d\u0430\u0432\u043e\u0440\u043e\u0447\u0435\u043d\u043d\u044b\u0445 Autofac-\u043e\u0432 \u0438 \u043f\u0440\u043e\u0447\u0438\u0445 StructureMap-\u043e\u0432, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0441\u0442\u044b\u043c \u0438 \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u044b\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u043a\u043e\u043c\u043f\u043e\u0437\u0438\u0446\u0438\u0438 \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438. \u0418 \u043f\u043e\u0441\u043b\u0435 \u043e\u0431\u044a\u0435\u043c\u043d\u043e\u0439 \u0434\u0438\u0441\u043a\u0443\u0441\u0438\u0438 \u0441 \u0443\u0432\u0430\u0436\u0430\u0435\u043c\u044b\u043c <a href=\"http:\/\/habrahabr.ru\/users\/razaz\/\" class=\"user_link\">Razaz<\/a> \u043f\u043e \u043f\u043e\u0432\u043e\u0434\u0443 \u0441\u0438\u043b\u044c\u043d\u044b\u0445 \u0438 \u0441\u043b\u0430\u0431\u044b\u0445 \u0441\u0442\u043e\u0440\u043e\u043d MEF \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b \u043f\u0440\u043e\u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 \u043e\u0431\u043b\u0430\u0441\u0442\u0435\u0439 \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0432 \u044d\u0442\u043e\u043c \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0435.<\/p>\n<p>  <a name=\"habracut\"><\/a><br \/>  \u041e\u0431\u043b\u0430\u0441\u0442\u0435\u0439 \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0432 MEF, \u043a\u0430\u043a \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e, \u0432\u0441\u0435\u0433\u043e \u0434\u0432\u0435 \u2014 Shared (\u043e\u0434\u0438\u043d \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043d\u0430 \u0432\u0435\u0441\u044c \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440) \u0438 NonShared (\u043d\u043e\u0432\u044b\u0439 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043d\u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0430). \u041d\u0430\u0432\u0435\u0440\u043d\u043e\u0435, \u043e\u0434\u0438\u043d \u0438\u0437 \u043f\u0435\u0440\u0432\u044b\u0445 \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u0432 \u0442\u0435\u0445, \u043a\u0442\u043e \u0438\u0437\u0443\u0447\u0430\u0435\u0442 \u044d\u0442\u043e\u0442 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 \u043f\u043e\u0441\u043b\u0435 Unity: \u00ab\u0410 \u0433\u0434\u0435 \u0436\u0435 \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u044c per-thread?\u00bb. \u0418\u043b\u0438, \u0434\u043b\u044f \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 WCF-\u0441\u043b\u0443\u0436\u0431, \u00abper-call\u00bb. <br \/>  \u041d\u0435 \u0432\u0434\u0430\u0432\u0430\u044f\u0441\u044c \u0432 \u0432\u043e\u043f\u0440\u043e\u0441, \u0437\u0430\u0447\u0435\u043c \u044d\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u0437\u0430\u0434\u0430\u0447 \u043a\u043e\u043c\u043f\u043e\u0437\u0438\u0446\u0438\u0438, \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u044e \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u043d\u0435\u0441\u043b\u043e\u0436\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u044d\u0442\u0438\u0445 \u043f\u043e\u043b\u0438\u0442\u0438\u043a \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0432 \u043e\u0431\u0449\u0435\u043c \u0432\u0438\u0434\u0435. <\/p>\n<p>  \u0414\u043b\u044f \u0442\u0435\u0445, \u043a\u043e\u043c\u0443 \u043d\u0435 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0447\u0438\u0442\u0430\u0442\u044c \u043e\u0431 \u044d\u0442\u0430\u043f\u0430\u0445 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0438 \u0442\u0435\u0445\u043d\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0434\u0435\u0442\u0430\u043b\u044f\u0445, <a href=\"https:\/\/github.com\/kayanme\/MEFLifetimePoliciesExtensions\">\u0442\u0443\u0442<\/a> \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0449\u0443\u043f\u0430\u0442\u044c \u0440\u0443\u043a\u0430\u043c\u0438 \u043a\u043e\u0434, \u0430 <a href=\"https:\/\/nuget.org\/packages\/MEFLifetimeExtensions\/\">\u0442\u0443\u0442<\/a> \u2014 \u0437\u0430\u0431\u0440\u0430\u0442\u044c \u0432 \u0432\u0438\u0434\u0435 \u043f\u0430\u043a\u0435\u0442\u0430.<\/p>\n<p>  \u0420\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0432 MEF 2.0, \u0430 \u043f\u043e\u0447\u0435\u043c\u0443 \u2014 \u043d\u0438\u0436\u0435.<\/p>\n<p>  \u0418\u0442\u0430\u043a. <br \/>  \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0434\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0432 \u043e\u0431\u0449\u0435\u043c \u0432\u0438\u0434\u0435: <br \/>  <i>\u00ab\u0412 \u0440\u0430\u043c\u043a\u0430\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0430 \u0435\u0441\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442. \u0412 \u043c\u043e\u043c\u0435\u043d\u0442 \u044d\u0442\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043e\u0442\u0434\u0430\u0432\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0430 \u0434\u043b\u044f \u043e\u0434\u043d\u043e\u0433\u043e \u0438 \u0442\u043e\u0433\u043e \u0436\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430, \u0438 \u0440\u0430\u0437\u043d\u044b\u0435 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u044b \u2014 \u0434\u043b\u044f \u0440\u0430\u0437\u043d\u044b\u0445 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043e\u0432.\u00bb<\/i><\/p>\n<p>  \u041d\u0435 \u0437\u043d\u0430\u044e \u043a\u0430\u043a \u0432\u044b, \u0430 \u044f \u0441\u0440\u0430\u0437\u0443 \u0443\u0432\u0438\u0434\u0435\u043b \u0432 \u044d\u0442\u043e\u043c \u043e\u0431\u044b\u0447\u043d\u044b\u0439 \u0441\u043b\u043e\u0432\u0430\u0440\u044c \u00ab\u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u00bb, \u0433\u0434\u0435 \u043a\u043b\u044e\u0447 \u2014 \u0438 \u0435\u0441\u0442\u044c \u043d\u0430\u0448 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442. \u0420\u0430\u0437\u0443\u043c\u0435\u0435\u0442\u0441\u044f, \u0441\u043b\u043e\u0432\u0430\u0440\u044c \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u043e\u0434\u0438\u043d \u043d\u0430 \u0432\u0435\u0441\u044c \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 Shared, \u043d\u043e \u044d\u0442\u043e \u043c\u044b \u0437\u0430\u0434\u0430\u0434\u0438\u043c \u043f\u043e\u0437\u0436\u0435.<\/p>\n<pre><code class=\"cs\">    public abstract class AffinityStorage&lt;TExport, TAffinity&gt; where TExport : class     {                 private ConcurrentDictionary&lt;TAffinity,TExport&gt; _partStorage             = new ConcurrentDictionary&lt;TAffinity,TExport&gt;();                internal TExport GetOrAdd(TAffinity affinity, Func&lt;TExport&gt; creator)         {             var t = _partStorage.GetOrAdd(affinity, (a) =&gt;creator());                       return t;         }          internal void RemoveAffinity(TAffinity affinity)         {             TExport val;            _partStorage.TryRemove(affinity, out val);         }     } <\/code><\/pre>\n<p>  \u041f\u043e\u044f\u0441\u043d\u044f\u0442\u044c \u0442\u0443\u0442, \u043d\u0430\u0432\u0435\u0440\u043d\u043e\u0435, \u043d\u0435\u0447\u0435\u0433\u043e, \u043e\u0431\u0440\u0430\u0449\u0443 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u044b\u0439 \u0444\u0430\u043a\u0442, \u0447\u0442\u043e \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043f\u0440\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0430 \u0434\u043b\u044f \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430, \u0447\u0442\u043e\u0431\u044b \u0431\u0435\u0437 \u043d\u0443\u0436\u0434\u044b \u043d\u0435 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442, \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0444\u0430\u0431\u0440\u0438\u0447\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434.<\/p>\n<p>  \u041d\u043e \u0433\u0434\u0435 \u0432\u0437\u044f\u0442\u044c \u044d\u0442\u043e\u0442 \u0444\u0430\u0431\u0440\u0438\u0447\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434? \u041f\u043e\u043c\u043d\u0438\u043c, \u0447\u0442\u043e \u043e\u043d \u0434\u043e\u043b\u0436\u0435\u043d \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043f\u043e\u043b\u043d\u043e\u0446\u0435\u043d\u043d\u0443\u044e \u0447\u0430\u0441\u0442\u044c, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0441\u043e \u0441\u0432\u043e\u0438\u043c\u0438 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c\u0438 \u0438\u043c\u043f\u043e\u0440\u0442\u0430\u043c\u0438.<br \/>  \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e MEF \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u0447\u0430\u0441\u0442\u0438 \u0432 \u00ab\u043b\u0435\u043d\u0438\u0432\u043e\u043c\u00bb \u0432\u0438\u0434\u0435. \u0410 \u0434\u043b\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u0434\u0430 \u0438 \u0432\u043e\u043e\u0431\u0449\u0435 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u0431\u0435\u0440\u0442\u043a\u0438 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u043a\u043b\u0430\u0441\u0441 \u2014 \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0443 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f. \u041e\u043d\u0430 \u0432 \u043f\u0435\u0440\u0441\u043f\u0435\u043a\u0442\u0438\u0432\u0435 NonShared, \u0442.\u043a. \u043d\u0430\u0448 \u043b\u0435\u043d\u0438\u0432\u044b\u0439 \u044d\u043a\u0441\u043f\u043e\u0440\u0442 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u043d\u043e\u0432\u044b\u0439 (\u0430 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0438\u043b\u0438 \u043d\u0435\u0442 \u2014 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u0442\u0441\u044f \u043d\u0430\u0448\u0435 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435).<\/p>\n<pre><code class=\"cs\">    public abstract class Policy&lt;TExport, TAffinity&gt;  where TExport : class     {          private readonly AffinityStorage&lt;TExport, TAffinity&gt; _storage;          [Import(AllowRecomposition = true, RequiredCreationPolicy = CreationPolicy.NonShared)]         private Lazy&lt;TExport&gt; _lazyPart;                  private bool _wasCreated;                private int _wasDisposed;               protected abstract TAffinity GetAffinity();          protected Policy(AffinityStorage&lt;TExport, TAffinity&gt; storage)         {             _storage = storage;         }          private TExport GetExportedValue()         {             _wasCreated = true;             return _storage.GetOrAdd(GetAffinity(), () =&gt; _lazyPart.Value);         }                  protected void DestroyAffinity(TAffinity affinity)         {             var wasDisposed = Interlocked.CompareExchange(ref _wasDisposed, 1, 0);             if (_wasCreated && wasDisposed == 0)             {                 _storage.RemoveAffinity(affinity);                           }          }          public static implicit operator TExport(Policy&lt;TExport, TAffinity&gt; threadPolicy)         {             return threadPolicy.GetExportedValue();         }                      } <\/code><\/pre>\n<p>  \u0417\u0434\u0435\u0441\u044c, \u043a\u0430\u043a \u0432\u0438\u0434\u0438\u043c, \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u044e\u0442:  <\/p>\n<ul>\n<li>\u043d\u0430\u0448 \u0444\u0430\u0431\u0440\u0438\u0447\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u0432 \u0432\u0438\u0434\u0435 Lazy&#038;ltTExport&#038;gt, \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e NonShared, \u0438\u043d\u0430\u0447\u0435 \u0437\u0430\u0447\u0435\u043c \u043e\u043d \u043d\u0443\u0436\u0435\u043d<\/li>\n<li>\u043d\u0435\u044f\u0432\u043d\u043e\u0435 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0438 \u0432 \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0442\u0438\u043f \u0447\u0435\u0440\u0435\u0437 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0430 \u0432 \u043d\u0430\u0448\u0435\u043c \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 <\/li>\n<li>\u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0436\u0435 \u0435\u0441\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442, \u0432 \u0432\u0438\u0434\u0435 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0432 \u043f\u043e\u0442\u043e\u043c\u043a\u0430\u0445<\/li>\n<li>\u043c\u0435\u0442\u043e\u0434 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043f\u0440\u0438\u0432\u044f\u0437\u043a\u0438 \u043a \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0443<\/li>\n<\/ul>\n<p>  \u041e\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u044e\u0441\u044c \u043d\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c \u043f\u0443\u043d\u043a\u0442\u0435.<br \/>  \u0415\u0449\u0435 \u0441 \u043c\u043e\u043c\u0435\u043d\u0442\u0430 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 AffinityStorage \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u043c\u044b \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435\u043c \u0438 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435\u043c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u043e\u0432 \u043d\u0430\u0448\u0438\u0445 \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u043e\u0432, \u0442\u043e \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0438 \u0438\u0445 \u043e\u0447\u0438\u0441\u0442\u043a\u043e\u0439. \u0412\u043e\u043f\u0440\u043e\u0441 \u043e\u0447\u0438\u0441\u0442\u043a\u0438 \u0441\u0430\u043c\u0438\u0445 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u043e\u0432 \u2014 \u0432\u0435\u0441\u044c\u043c\u0430 \u0431\u043e\u043b\u0435\u0437\u043d\u0435\u043d\u043d\u044b\u0439 \u0434\u043b\u044f IoC-\u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u043e\u0432 \u0432 \u0446\u0435\u043b\u043e\u043c, \u0432\u043a\u0440\u0430\u0442\u0446\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u0437\u044f\u0442\u044c \u0438 \u043e\u0447\u0438\u0441\u0442\u0438\u0442\u044c (Dispose) \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 \u0438\u043c \u0436\u0435 \u044d\u043a\u0441\u043f\u043e\u0440\u0442, \u0442.\u043a. \u043e\u043d \u043d\u0435 \u0437\u043d\u0430\u0435\u0442 \u0433\u0434\u0435 \u044d\u0442\u043e\u0442 \u044d\u043a\u0441\u043f\u043e\u0440\u0442 \u0438 \u043a\u0430\u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0437\u0430\u0434\u0430\u0447\u0430 \u043e\u0447\u0438\u0441\u0442\u043a\u0438 \u0447\u0430\u0441\u0442\u0435\u0439 \u043b\u043e\u0436\u0438\u0442\u0441\u044f \u043d\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. \u0412 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u043d\u0435 \u0437\u0430\u0434\u0443\u043c\u044b\u0432\u0430\u044f\u0441\u044c \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0441\u0430\u043c\u0438 \u0447\u0430\u0441\u0442\u0438, \u0431\u0443\u0434\u0435\u043c \u043e\u0447\u0438\u0449\u0430\u0442\u044c \u043d\u0430\u0448\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043d\u043e-\u043f\u0440\u0438\u0432\u044f\u0437\u0430\u043d\u043d\u043e\u0435 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u0432 \u043c\u043e\u043c\u0435\u043d\u0442 \u043b\u0438\u043a\u0432\u0438\u0434\u0430\u0446\u0438\u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430.<br \/>  \u0418 \u043c\u043e\u043c\u0435\u043d\u0442 \u043b\u0438\u043a\u0432\u0438\u0434\u0430\u0446\u0438\u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430, \u043e\u043f\u044f\u0442\u044c \u0436\u0435, \u043f\u0443\u0441\u043a\u0430\u0439 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0438.<\/p>\n<p>  \u0421\u0434\u0435\u043b\u0430\u0435\u043c \u043d\u0430\u043a\u043e\u043d\u0435\u0446 \u0443\u0436\u0435 \u044d\u0442\u0443 \u043a\u043e\u043d\u0435\u0447\u043d\u0443\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u2014 \u0434\u043b\u044f \u043f\u043e\u0442\u043e\u043a\u0430.<\/p>\n<pre><code class=\"cs\">    [Export(typeof(ThreadStorage&lt;&gt;))]     [PartCreationPolicy(CreationPolicy.Shared)]     internal sealed class ThreadStorage&lt;TExport&gt; : AffinityStorage&lt;TExport, Thread&gt; where T : class     {     }      [Export(typeof (ThreadPolicy&lt;&gt;))]       [PartCreationPolicy(CreationPolicy.NonShared)]     public sealed class ThreadPolicy&lt;TExport&gt; : Policy&lt;TExport, Thread&gt; where T : class     {                  protected override Thread GetAffinity()         {             return Thread.CurrentThread;         }                              [ImportingConstructor]         internal ThreadPolicy(             [Import(RequiredCreationPolicy = CreationPolicy.Shared)]              ThreadStorage&lt;TExport&gt; storage)             : base(storage)         {                    }     } <\/code><\/pre>\n<p>  \u042d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0432 MEF 2.0, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0435 \u043e\u0431\u043e\u0431\u0449\u0435\u043d\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0430. \u0412 \u0441\u0432\u044f\u0437\u0438 \u0441\u043e \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u043e\u0439 \u0437\u0430\u0434\u0430\u043d\u0438\u044f \u043a\u043e\u043d\u0442\u0440\u0430\u043a\u0442\u043e\u0432, \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u043f\u043e\u043b\u0438\u0442\u0438\u043a\u0438 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0447\u0430\u0441\u0442\u0438\u0447\u043d\u043e \u0437\u0430\u043a\u0440\u044b\u0442\u044b\u0439 \u043f\u043e \u0442\u0438\u043f\u0443 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 \u043a\u043b\u0430\u0441\u0441-\u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u0438 \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0435\u0433\u043e.<\/p>\n<p>  \u0420\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u044d\u0442\u043e \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0442\u0430\u043a (\u043d\u0430\u0434\u043e, \u043a\u043e\u043d\u0435\u0447\u043d\u043e, \u0432\u0441\u0435 \u043d\u0430\u043c\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u043e\u0435 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440):  <\/p>\n<pre><code class=\"cs\">   TestPart part = _container.GetExportedValue&lt;ThreadPolicy&lt;TestPart&gt;&gt;(); <\/code><\/pre>\n<p>  \u0438\u043b\u0438 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"cs\">[Export] class Outer {             Inner _perThreadObject;             [ImportingConstructor]             Outer([Import]ThreadPolicy &lt;Inner&gt; perThreadObject)             {                     _perThreadObject = perThreadObject;            }  } <\/code><\/pre>\n<p>  \u0427\u0442\u043e \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0437\u0430 \u043a\u0430\u0434\u0440\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u0443\u0441\u043b\u043e\u0436\u043d\u044f\u0442\u044c, \u043d\u043e \u043b\u0435\u0436\u0438\u0442 \u0432 \u0433\u0438\u0442\u0435:  <\/p>\n<ol>\n<li>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u043b\u044f \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430 WCF- \u0432\u0441\u0435 \u0442\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435, TAffinity \u0435\u0441\u0442\u044c Transaction.Current \u0438 OperationContext.Current<\/li>\n<li>\u0415\u0441\u043b\u0438 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u043f\u0440\u0438\u0448\u0435\u043b \u0432 \u0432\u0438\u0434\u0435 default(TAffinity), \u0442\u043e \u0431\u0443\u0434\u0435\u043c \u0441\u0447\u0438\u0442\u0430\u0442\u044c, \u0447\u0442\u043e \u043d\u0430\u0434\u043e \u043e\u0442\u0434\u0430\u0442\u044c \u043e\u0431\u044b\u0447\u043d\u044b\u0439 NonShared \u044d\u043a\u0441\u043f\u043e\u0440\u0442 <\/li>\n<li>\u041f\u0435\u0440\u0435\u0445\u0432\u0430\u0442 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u2014 \u0435\u0441\u043b\u0438 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0447\u0430\u0441\u0442\u044c, \u0442\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0441 \u043d\u0435\u0439 \u0447\u0442\u043e-\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u2014 \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0434\u043b\u044f \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439 \u044f \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u044e, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0447\u0430\u0441\u0442\u044c \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u043c \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 (ISinglePhaseNotification \u0438\u043b\u0438 IEnlistmentNotification) \u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u044e \u0435\u0435 \u043a \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u043a\u0430\u043a \u0432\u043e\u043b\u0430\u0442\u0438\u043b\u044c\u043d\u044b\u0439 \u0440\u0435\u0441\u0443\u0440\u0441, \u0435\u0441\u043b\u0438 \u044d\u0442\u043e \u0442\u0430\u043a. <\/li>\n<li> \u0423\u043d\u0438\u0447\u0442\u043e\u0436\u0435\u043d\u0438\u0435 \u043f\u0440\u0438\u0432\u044f\u0437\u043a\u0438 \u2014 \u0432 \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u043e\u0439 \u0432\u044b\u0448\u0435 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0434\u043b\u044f \u043f\u043e\u0442\u043e\u043a\u0430 \u044f \u0441\u043e\u0437\u0434\u0430\u044e \u043f\u043e\u0442\u043e\u043a, \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0449\u0438\u0439 DestroyAffinity \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u043d\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430. \u0414\u043b\u044f \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438\/\u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u2014 \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0440\u0438\u0432\u044f\u0437\u044b\u0432\u0430\u044e\u0441\u044c \u043a \u0441\u043e\u0431\u044b\u0442\u0438\u044e \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438\/\u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438.<\/li>\n<\/ol>\n<p>  \u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0432\u0441\u0435\u043c, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u043a\u043e\u043c\u0443-\u0442\u043e \u043f\u043e\u043c\u043e\u0436\u0435\u0442.<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/180307\/\"> http:\/\/habrahabr.ru\/post\/180307\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">   \t\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435, \u0436\u0438\u0442\u0435\u043b\u0438 \u0445\u0430\u0431\u0440\u0430.<br \/>  Managed Extensibility Framework aka MEF, \u0447\u0442\u043e \u0431\u044b \u043d\u0435 \u0433\u043e\u0432\u043e\u0440\u0438\u043b\u0438 \u043b\u044e\u0431\u0438\u0442\u0435\u043b\u0438 \u043d\u0430\u0432\u043e\u0440\u043e\u0447\u0435\u043d\u043d\u044b\u0445 Autofac-\u043e\u0432 \u0438 \u043f\u0440\u043e\u0447\u0438\u0445 StructureMap-\u043e\u0432, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0441\u0442\u044b\u043c \u0438 \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u044b\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u043a\u043e\u043c\u043f\u043e\u0437\u0438\u0446\u0438\u0438 \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438. \u0418 \u043f\u043e\u0441\u043b\u0435 \u043e\u0431\u044a\u0435\u043c\u043d\u043e\u0439 \u0434\u0438\u0441\u043a\u0443\u0441\u0438\u0438 \u0441 \u0443\u0432\u0430\u0436\u0430\u0435\u043c\u044b\u043c <a href=\"http:\/\/habrahabr.ru\/users\/razaz\/\" class=\"user_link\">Razaz<\/a> \u043f\u043e \u043f\u043e\u0432\u043e\u0434\u0443 \u0441\u0438\u043b\u044c\u043d\u044b\u0445 \u0438 \u0441\u043b\u0430\u0431\u044b\u0445 \u0441\u0442\u043e\u0440\u043e\u043d MEF \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b \u043f\u0440\u043e\u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 \u043e\u0431\u043b\u0430\u0441\u0442\u0435\u0439 \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0432 \u044d\u0442\u043e\u043c \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0435.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-180307","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/180307","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=180307"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/180307\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=180307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=180307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=180307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}