Mef和asp.net问题
我有一个 asp.net 应用程序,使用模型程序集(带有模型类)来实现业务逻辑。该模型程序集通过 IMailService 接口依赖于 MailService,我尝试使用 MEF 来满足 MailService 实现的模型需求。我正在 Model 类的构造函数中进行 MEF 组合。
这背后的想法是创建一个可以在我的网站之间重复使用的 MailService 程序集,而网站本身不需要知道邮件是如何发送的。也许 IoC 容器会是更好的选择,但我只是认为 MEF 方法更容易理解,而且我喜欢通过组合部件来组合应用程序的想法。如果与 IoC 容器相比,mef 方法是否有任何负面影响?
[Import]
private IMailService _mailService;
public Model()
{
Compose();
}
private void Compose()
{
DirectoryCatalog cat = new DirectoryCatalog(Settings.Default.PluginsFolder);
var container = new CompositionContainer(cat);
container.ComposeParts(this);
}
下面的代码位于另一个程序集中,接口位于另一个程序集中,
[Export(typeof(IMailService))]
public class MailService : IMailService
{
}
这在我的模型程序集单元测试中工作正常,但是当我通过我的 asp.net 站点使用模型程序集时,它会失败,并出现以下异常。我还尝试在 web.config 中将信任设置为完全,但仍然没有成功
成分保持不变。这 更改被拒绝,因为 下列错误: 成分 产生了一个构图错误。 下面提供了根本原因。 查看 CompositionException.Errors 属性更详细 信息。
1) 未找到匹配的导出 约束条件 '((exportDefinition.ContractName = “ExtensionInterfaces.IMailService”)&& (exportDefinition.Metadata.ContainsKey("ExportTypeIdentity") && “ExtensionInterfaces.IMailService”.Equals(exportDefinition.Metadata.get_Item(“ExportTypeIdentity”))))'。
导致:无法设置导入 模型.模型._mailService (ContractName="ExtensionInterfaces.IMailService")' 部分型号.型号'。元素: 模型.模型._mailService (ContractName="ExtensionInterfaces.IMailService") -->型号.型号
I have an asp.net application using a model assembly (with a Model class) for business logic. This model assembly has a dependency on a MailService through an IMailService interface and I am trying to use MEF to fill the Models need for a MailService implementation. I am doing the MEF composition in the contructor of the Model class.
The idea behind this is to create a MailService assembly I can reuse between my websites without the sites themselves need to know how the mail is sent. Maybe an IoC container would be a better choise but I just think the MEF approach is simpler to understand and I like the idea of composing my apps by combining parts. Also does the mef approach have any negative sides if you compare with a IoC container?
[Import]
private IMailService _mailService;
public Model()
{
Compose();
}
private void Compose()
{
DirectoryCatalog cat = new DirectoryCatalog(Settings.Default.PluginsFolder);
var container = new CompositionContainer(cat);
container.ComposeParts(this);
}
The code below is in another assembly and the interface in yet another assembly
[Export(typeof(IMailService))]
public class MailService : IMailService
{
}
this works fine in my unit tests for the model assembly but when I am using the model assembly through my asp.net site it fails with the exception below. I also tried to set the trust to full in web.config but still no luck
The composition remains unchanged. The
changes were rejected because of the
following error(s): The composition
produced a single composition error.
The root cause is provided below.
Review the CompositionException.Errors
property for more detailed
information.1) No exports were found that match
the constraint
'((exportDefinition.ContractName =
"ExtensionInterfaces.IMailService") &&
(exportDefinition.Metadata.ContainsKey("ExportTypeIdentity")
&&
"ExtensionInterfaces.IMailService".Equals(exportDefinition.Metadata.get_Item("ExportTypeIdentity"))))'.Resulting in: Cannot set import
Model.Model._mailService
(ContractName="ExtensionInterfaces.IMailService")'
on part Model.Model'. Element:
Model.Model._mailService
(ContractName="ExtensionInterfaces.IMailService")
--> Model.Model
我还没有在 Web 应用程序中使用 MEF,但如果我猜测的话,我会假设读取插件目录中的程序集存在一些问题。可能是权限问题什么的。无论如何,我首先会检查 DirectoryCatalog,看看它是否包含您期望的内容。
I've not used MEF in a web application yet, but if I were to guess I would assume there is some issue with reading the assemblies in the plugins directory. Maybe an permissions issue or something. At any rate I would start by examining the DirectoryCatalog to see if it contains what you would expect it to.