Link to home
Start Free TrialLog in
Avatar of etowa
etowaFlag for Afghanistan

asked on

The composition remains unchanged error while using MEF in global.asax (Fisharoo ASP.net 4 Social Networking Book)

Hi I have following code in Global.asax
 ------------------------ Global.asax -----------------------------------
   [Import]
         public IRedirector redir;
         [Import]
         public ILogger log;
         protected void Application_Start(object sender, EventArgs e)
         {
             MEFManager.Compose(this);
             log.EnsureInitialized();
         }
 
         protected void Application_Error(object sender, EventArgs e)
         {
             MEFManager.Compose(this);
 
            log.Error(sender, "Error caught by the Global.asax: " + Server.GetLastError().Message);
             redir.GoToErrorPage();
         }
 ---------------------------------------------------------------------- End

This is Compose method
--------------------------------MEFManager.cs--------------------------------------

 public static void Compose(object o)
         {
             var container = new CompositionContainer(
                                     new DirectoryCatalog(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"bin\MEFBin")));
             var batch = new CompositionBatch();
             batch.AddPart(o);
             container.Compose(batch); //Error always occurs here
         }
---------------------------------------
Its throwing following error on aplication start running and when it runs to the
 
The composition remains unchanged. The changes were rejected because of the following error(s): The composition produced multiple composition errors, with 2 root causes. The root causes are provided below. Review the CompositionException.Errors property for more detailed information.
 
1) No valid exports were found that match the constraint '((exportDefinition.ContractName == "Fisharoo.Interfaces.IRedirector") AndAlso (exportDefinition.Metadata.ContainsKey("ExportTypeIdentity") AndAlso "Fisharoo.Interfaces.IRedirector".Equals(exportDefinition.Metadata.get_Item("ExportTypeIdentity"))))', invalid exports may have been rejected.
 
Resulting in: Cannot set import 'Web.Global.redir (ContractName="Fisharoo.Interfaces.IRedirector")' on part 'ASP.global_asax'.
 Element: Web.Global.redir (ContractName="Fisharoo.Interfaces.IRedirector") -->  ASP.global_asax
 
2) No valid exports were found that match the constraint '((exportDefinition.ContractName == "Fisharoo.Interfaces.ILogger") AndAlso (exportDefinition.Metadata.ContainsKey("ExportTypeIdentity") AndAlso "Fisharoo.Interfaces.ILogger".Equals(exportDefinition.Metadata.get_Item("ExportTypeIdentity"))))', invalid exports may have been rejected.
 
Resulting in: Cannot set import 'Web.Global.log (ContractName="Fisharoo.Interfaces.ILogger")' on part 'ASP.global_asax'.
 Element: Web.Global.log (ContractName="Fisharoo.Interfaces.ILogger") -->  ASP.global_asax
 
I am new to MEF. What is this error about? I googled it, but could not solve ?      

ASKER CERTIFIED SOLUTION
Avatar of Rouneh10
Rouneh10

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of etowa

ASKER

@Rouneh10
I did put the .dll files  into the nin\MEFBin folder and this error is gone a new error is generated as:

The composition produced multiple composition errors, with 2 root causes. The root causes are provided below. Review the CompositionException.Errors property for more detailed information.

1) Cannot populate the collection 'Fisharoo.Components.UserSession._webContext' because it does not implement ICollection<T> or is read-only. If the collection is not IEnumerable<T> or T[] it must implement ICollection<T> and be either pre-initialized or be writable with a default constructor.

Resulting in: Cannot activate part 'Fisharoo.Components.UserSession'.
Element: Fisharoo.Components.UserSession -->  Fisharoo.Components.UserSession

Resulting in: An exception occurred while trying to create an instance of type 'Fisharoo.Components.UserSession'.

Resulting in: Cannot activate part 'Fisharoo.Components.UserSession'.
Element: Fisharoo.Components.UserSession -->  Fisharoo.Components.UserSession -->  DirectoryCatalog (Path="C:\Akoraba\Fisharoo\Web\bin\MEFBin")

Resulting in: Cannot get export 'Fisharoo.Components.UserSession (ContractName="Fisharoo.DataAccess.Interfaces.IUserSession")' from part 'Fisharoo.Components.UserSession'.
Element: Fisharoo.Components.UserSession (ContractName="Fisharoo.DataAccess.Interfaces.IUserSession") -->  Fisharoo.Components.UserSession -->  DirectoryCatalog (Path="C:\Akoraba\Fisharoo\Web\bin\MEFBin")

Resulting in: Cannot set import 'Fisharoo.Web.Site._userSession (ContractName="Fisharoo.DataAccess.Interfaces.IUserSession")' on part 'ASP.site_master'.
Element: Fisharoo.Web.Site._userSession (ContractName="Fisharoo.DataAccess.Interfaces.IUserSession") -->  ASP.site_master

2) Cannot populate the collection 'Fisharoo.Components.UserSession._webContext' because it does not implement ICollection<T> or is read-only. If the collection is not IEnumerable<T> or T[] it must implement ICollection<T> and be either pre-initialized or be writable with a default constructor.

Resulting in: Cannot activate part 'Fisharoo.Components.UserSession'.
Element: Fisharoo.Components.UserSession -->  Fisharoo.Components.UserSession

Resulting in: An exception occurred while trying to create an instance of type 'Fisharoo.Components.UserSession'.

Resulting in: Cannot activate part 'Fisharoo.Components.UserSession'.
Element: Fisharoo.Components.UserSession -->  Fisharoo.Components.UserSession -->  DirectoryCatalog (Path="C:\Akoraba\Fisharoo\Web\bin\MEFBin")

Resulting in: Cannot get export 'Fisharoo.Components.UserSession (ContractName="Fisharoo.DataAccess.Interfaces.IUserSession")' from part 'Fisharoo.Components.UserSession'.
Element: Fisharoo.Components.UserSession (ContractName="Fisharoo.DataAccess.Interfaces.IUserSession") -->  Fisharoo.Components.UserSession -->  DirectoryCatalog (Path="C:\Akoraba\Fisharoo\Web\bin\MEFBin")

Resulting in: Cannot set import 'Fisharoo.Components.Navigation._userSession (ContractName="Fisharoo.DataAccess.Interfaces.IUserSession")' on part 'Fisharoo.Components.Navigation'.
Element: Fisharoo.Components.Navigation._userSession (ContractName="Fisharoo.DataAccess.Interfaces.IUserSession") -->  Fisharoo.Components.Navigation -->  DirectoryCatalog (Path="C:\Akoraba\Fisharoo\Web\bin\MEFBin")

Resulting in: Cannot get export 'Fisharoo.Components.Navigation (ContractName="Fisharoo.Interfaces.INavigation")' from part 'Fisharoo.Components.Navigation'.
Element: Fisharoo.Components.Navigation (ContractName="Fisharoo.Interfaces.INavigation") -->  Fisharoo.Components.Navigation -->  DirectoryCatalog (Path="C:\Akoraba\Fisharoo\Web\bin\MEFBin")

Resulting in: Cannot set import 'Fisharoo.Web.Site._navigation (ContractName="Fisharoo.Interfaces.INavigation")' on part 'ASP.site_master'.
Element: Fisharoo.Web.Site._navigation (ContractName="Fisharoo.Interfaces.INavigation") -->  ASP.site_master

I dont see why this error keeps ocurring now...
Still trying to solve but your help is much appreciated
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of etowa

ASKER

Yes i have Import
 [Import]
        private IWebContext _webContext;
in my UserSession.cs
is that what is causing the error.
IWebcontext.cs----------------------
namespace Fisharoo.DataAccess.Interfaces
{
    public interface IWebContext
    {
        void ClearSession();
        bool ContainsInSession(string key);
        void RemoveFromSession(string key);
        bool LoggedIn { get; set; }
        string Username { get; set; }
        Account CurrentUser { get; set; }
        string CaptchaImageText { get; set; }
        string UsernameToVerify { get; }
    }
UserSession is this

using Fisharoo.DataAccess.Interfaces;
using Fisharoo.Interfaces;
using Fisharoo.DataAccess;
using Fisharoo.Common;

namespace Fisharoo.Components
{
    [Export(typeof(IUserSession))]
    public class UserSession : IUserSession
    {
        [Import]
        private IWebContext _webContext;

        public UserSession()
        {
            MEFManager.Compose(this);
            //_webContext = ObjectFactory.GetInstance<IWebContext>();
        }

        public bool LoggedIn
        {
            get
            {
                return _webContext.LoggedIn;
            }
            set
            {
                _webContext.LoggedIn = value;
            }
        }

        public Account CurrentUser
        {
            get
            {
                return _webContext.CurrentUser;
            }
            set
            {
                _webContext.CurrentUser = value;
            }
        }

        public string Username
        {
            get
            {
                return _webContext.Username;
            }

            set
            {
                _webContext.Username = value;
            }
        }
    }
}
}
------------------------------------------
Avatar of Rouneh10
Rouneh10

Make your _webContext a property.

[Import]
private IWebContext _webContext { get; set; }

Open in new window

Avatar of etowa

ASKER

Got it working