How to Create MEF Container in Server Side?

How to Create MEF Container in Server Side?

Old forum URL:

simon posted on Tuesday, March 13, 2012

In my solution, use WCF communicate between client and server.  and host in IIS.

my problem is how to create MEF Container in server side and import  repository instance to ObjectFactory?

JonnyBee replied on Tuesday, March 13, 2012

Follow this link  and download the latest source.

In the folder $\samples\MEFSamples\ObjectFactory you will find a sample application with ObjectFactory that use MEF.
And in the $\trunk\CslaContrib.MEF you will find baseclasses and an ObjectFactoryLoader that use MEF.


  1. Create interfaces for your ObjectFactories in the business assembly or separate assembly.rom
  2. Use the MEFFactoryLoader from CslaContrib.MEF (configured in servers web.config and maybe applications app.config)
  3. Make sure the object factory assemblies make into the servers bin folder.
  4. Make your "root" object factories export the actual interface and import child factory interfaces

simon replied on Tuesday, March 13, 2012


I will have to combine diffgrah、customField  MEF MefBusinessBase into one Bussinessbase

JonnyBee replied on Wednesday, March 14, 2012

When you use ObjectFactory you have no need for MefBusinessBase and the other Mef intermediate base classes.

MefFactoryLoader is the key component and the usage of interfaces for the "root" and "child" object factories.

They MefXYX base classes are only required for repository pattern and just my laziness to use the same business library with a minimum of changes for the 2 samples.

simon replied on Wednesday, March 14, 2012

According to what I understand, if need to sepreate BO and DataAccess,such as Fetch,Create, into two assembly, will use ObjectFactory. If I no sepeate them. How to Create Container?

for example in ProjectTracker, if i need to create MEF, should create static class  and  examine it in DALFactory

 public static IDALManager GetManager()

           _container = Container.Current;
            if (dalManager == null)

                var dalTypeName = ConfigurationManager.AppSettings["DalManagerType"];
                if (!string.IsNullOrEmpty(dalTypeName))
                    // _dalType = Type.GetType(dalTypeName);
                    dalManager = ServiceLocator.Current.GetInstance<IDALManager>(dalTypeName);
                    throw new NullReferenceException("DalManagerType");
                if (dalManager == null)
                    throw new ArgumentException(string.Format("Type {0} could not be found", dalTypeName));
            return dalManager;

JonnyBee replied on Wednesday, March 14, 2012

Look at MefFactoryLoader.

You want to use MEF to have a separate DAL layer for unit testing and another for production - right?

That would typically require that you have the implementation in separate asseblies to be loaded into the container.

CSLA - when using ObjectFactory requires an ObjectFactoryLoader to return

The ObjectFactoryLoader gets the text string from ObjectFactoryAttribute - and the ObjectFactoryAttribute constructor that accepts typeof(interface) will actually use the fully qualified name of the interface. So this name is sent to the ObjectFactoryLoader to return an instance of the actual class that the DataPortal will call.

This is implemented in the MefFactoryLoader - that may be altered to use another container - my implementation just loads the available types from the bin folder - assuming that only the relevant assemblies is present at the given time.

So - anything that needs MEF container must be handled inside the implementation of ObjectFactoryLoader and must also "build" the imports of the actual class before it is returned to the data portal.

Copyright (c) Marimer LLC