Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1869
  • Last Modified:

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 ?      

0
etowa
Asked:
etowa
  • 3
  • 3
2 Solutions
 
Rouneh10Commented:
It looks like one of two things:

1. You don't have any classes exporting the Fisharoo.Interfaces.IRedirector and Fisharoo.Interfaces.ILogger interfaces. I don't know anything about the Fisharoo library, but from the code I would assume there are classes somewhere in your project that implement those interfaces and have the attributes [Export(typeof(Fisharoo.Interfaces.IRedirector))] or [Export(typeof(Fisharoo.Interfaces.ILogger))] defined.
2. Number 1 is ok, but the classes exporting the interfaces do some imports of their own, and those requirements cannot be satisfied.

Your CompositionContainer seems to look for imports in the bin\MEFBin folder. Since the imports are related to the Fisharoo library, have you checked to see if .dll files for the Fisharoo library can be found in the bin\MEFBin folder?

0
 
etowaAuthor Commented:
@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
0
 
Rouneh10Commented:
It looks like you have a [ImportMany] attribute on a private member _webContext in the Fisharoo.Components.UserSession class.
This in itself isn't necessarily bad, but the _webContext member must be either an array or an IEnumerable of the type _webContext is.

Have a look at the UserSession class and check the type and if there's an ImportMany or Import statement.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
etowaAuthor Commented:
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;
            }
        }
    }
}
}
------------------------------------------
0
 
Rouneh10Commented:
Make your _webContext a property.

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

Open in new window

0
 
etowaAuthor Commented:
Got it working
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now