etowa
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().Mess age);
redir.GoToErrorPage();
}
-------------------------- ---------- ---------- ---------- ---------- ---- End
This is Compose method
-------------------------- ------MEFM anager.cs- ---------- ---------- ---------- -------
public static void Compose(object o)
{
var container = new CompositionContainer(
new DirectoryCatalog(Path.Comb ine(AppDom ain.Curren tDomain.Ba seDirector y, @"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.Error s property for more detailed information.
1) No valid exports were found that match the constraint '((exportDefinition.Contra ctName == "Fisharoo.Interfaces.IRedi rector") AndAlso (exportDefinition.Metadata .ContainsK ey("Export TypeIdenti ty") AndAlso "Fisharoo.Interfaces.IRedi rector".Eq uals(expor tDefinitio n.Metadata .get_Item( "ExportTyp eIdentity" ))))', invalid exports may have been rejected.
Resulting in: Cannot set import 'Web.Global.redir (ContractName="Fisharoo.In terfaces.I Redirector ")' on part 'ASP.global_asax'.
Element: Web.Global.redir (ContractName="Fisharoo.In terfaces.I Redirector ") --> ASP.global_asax
2) No valid exports were found that match the constraint '((exportDefinition.Contra ctName == "Fisharoo.Interfaces.ILogg er") AndAlso (exportDefinition.Metadata .ContainsK ey("Export TypeIdenti ty") AndAlso "Fisharoo.Interfaces.ILogg er".Equals (exportDef inition.Me tadata.get _Item("Exp ortTypeIde ntity")))) ', invalid exports may have been rejected.
Resulting in: Cannot set import 'Web.Global.log (ContractName="Fisharoo.In terfaces.I Logger")' on part 'ASP.global_asax'.
Element: Web.Global.log (ContractName="Fisharoo.In terfaces.I Logger") --> ASP.global_asax
I am new to MEF. What is this error about? I googled it, but could not solve ?
------------------------ 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().Mess
redir.GoToErrorPage();
}
--------------------------
This is Compose method
--------------------------
public static void Compose(object o)
{
var container = new CompositionContainer(
new DirectoryCatalog(Path.Comb
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.Error
1) No valid exports were found that match the constraint '((exportDefinition.Contra
Resulting in: Cannot set import 'Web.Global.redir (ContractName="Fisharoo.In
Element: Web.Global.redir (ContractName="Fisharoo.In
2) No valid exports were found that match the constraint '((exportDefinition.Contra
Resulting in: Cannot set import 'Web.Global.log (ContractName="Fisharoo.In
Element: Web.Global.log (ContractName="Fisharoo.In
I am new to MEF. What is this error about? I googled it, but could not solve ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.Interf aces
{
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.Interf aces;
using Fisharoo.Interfaces;
using Fisharoo.DataAccess;
using Fisharoo.Common;
namespace Fisharoo.Components
{
[Export(typeof(IUserSessio n))]
public class UserSession : IUserSession
{
[Import]
private IWebContext _webContext;
public UserSession()
{
MEFManager.Compose(this);
//_webContext = ObjectFactory.GetInstance< IWebContex t>();
}
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;
}
}
}
}
}
-------------------------- ---------- ------
[Import]
private IWebContext _webContext;
in my UserSession.cs
is that what is causing the error.
IWebcontext.cs------------
namespace Fisharoo.DataAccess.Interf
{
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.Interf
using Fisharoo.Interfaces;
using Fisharoo.DataAccess;
using Fisharoo.Common;
namespace Fisharoo.Components
{
[Export(typeof(IUserSessio
public class UserSession : IUserSession
{
[Import]
private IWebContext _webContext;
public UserSession()
{
MEFManager.Compose(this);
//_webContext = ObjectFactory.GetInstance<
}
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;
}
}
}
}
}
--------------------------
Make your _webContext a property.
[Import]
private IWebContext _webContext { get; set; }
ASKER
Got it working
ASKER
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.Error
1) Cannot populate the collection 'Fisharoo.Components.UserS
Resulting in: Cannot activate part 'Fisharoo.Components.UserS
Element: Fisharoo.Components.UserSe
Resulting in: An exception occurred while trying to create an instance of type 'Fisharoo.Components.UserS
Resulting in: Cannot activate part 'Fisharoo.Components.UserS
Element: Fisharoo.Components.UserSe
Resulting in: Cannot get export 'Fisharoo.Components.UserS
Element: Fisharoo.Components.UserSe
Resulting in: Cannot set import 'Fisharoo.Web.Site._userSe
Element: Fisharoo.Web.Site._userSes
2) Cannot populate the collection 'Fisharoo.Components.UserS
Resulting in: Cannot activate part 'Fisharoo.Components.UserS
Element: Fisharoo.Components.UserSe
Resulting in: An exception occurred while trying to create an instance of type 'Fisharoo.Components.UserS
Resulting in: Cannot activate part 'Fisharoo.Components.UserS
Element: Fisharoo.Components.UserSe
Resulting in: Cannot get export 'Fisharoo.Components.UserS
Element: Fisharoo.Components.UserSe
Resulting in: Cannot set import 'Fisharoo.Components.Navig
Element: Fisharoo.Components.Naviga
Resulting in: Cannot get export 'Fisharoo.Components.Navig
Element: Fisharoo.Components.Naviga
Resulting in: Cannot set import 'Fisharoo.Web.Site._naviga
Element: Fisharoo.Web.Site._navigat
I dont see why this error keeps ocurring now...
Still trying to solve but your help is much appreciated