Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6806621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:44:00+00:00 2026-05-26T19:44:00+00:00

Just wanted to know if what I’m trying to do is possible since I

  • 0

Just wanted to know if what I’m trying to do is possible since I couldn’t find any information about it in the net.

Lets say I got a class that I export with PartCreationPolicy(CreationPolicy.Any).
If I’d like to import it with ServiceLocator.Instance.GetInstance(), where can I specify with which CreationPolicy I’d like to import it? (If it would be Shared or NonShared)

Just couldn’t find any info about it anywhere.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:44:01+00:00Added an answer on May 26, 2026 at 7:44 pm

    As I understand it this can’t be specified through a generic service locator.

    By default, CreationPolicy.Any will be treated as ‘shared’. To get a ‘non-shared’ instance you need to either:

    • Change the creation policy on the part to CreationPolicy.NonShared; or,
    • Use the MEF CompositionContainer.GetExports(ImportDefinition) method; or,
    • Create a part that uses an ExportFactory import and request that from the ServiceLocator (see below. Requires MEF 2, or Silverlight 4 – not available in the .NET 4.0 RTM MEF.)

    The kind of thing you need for the third option would look like:

    [Export]
    public class ExportFactoryProvider<T>
    {
        [Import]
        public ExportFactory<T> Factory { get; set; }
    }
    

    Then, request one of these from the service locator and use the factory property to create new instances (where IService is the thing you want to instantiate new instances of):

    var serviceFactory =
        ServiceLocator.Instance.GetService<ExportFactoryProvider<IService>>();
    
    using (var service1 = serviceFactory.Factory.CreateExport())
    {
        DoSomething(service1.Value);
    }
    

    Admittedly this ends up looking a bit hacky. I believe the reason for the awkwardness is that few (no?) other IoC containers support specifying anything like CreationPolicy when a service is requested.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just wanted to know. Is it possible to highlight text in ElasticSearch on an
Just wanted to know something about signed vs unsigned interpretation. Am I right if
Just wanted to know if is possible to use mod_rewrite on a single(or more
Just wanted to know what could be the security cautions I should know about
Just wanted to know if there are any ready made Ecommerce Libraries out there.
I just wanted to know whether it is possible to pass geocoords to the
just wanted to know how to create a dropdownlist in asp.net with images beside
I just wanted to know. Is it possible to change the http request headers
I just wanted to know more about analytics. I am new to this thing.
I just wanted to know if there's anything built into the .net framework where

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.