Mef和asp.net问题

发布于 08-03 11:21 字数 1484 浏览 8 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木有鱼丸2024-08-10 11:21:06

我还没有在 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.

情何以堪。2024-08-10 11:21:06

我发现了错误,我在 bin 文件夹中有一个程序集,其名称与我导出的 MailService 程序集相同。由于某种原因,目录选择了 bin 文件夹中的程序集,而不是我指定的目录中的程序集。不知道为什么,但至少当我从 bin 文件夹中删除旧程序集时它才起作用。

I found the error, I had a assembly in the bin folder with the same name as the MailService assembly with my export. For some reason the catalog picked up the assembly in the bin folder instead of the assembly in the catalog I specified. Don't know why but it worked when I removed the old assembly from the bin folder at least.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文