我对 Autofac 很陌生并且遇到了问题。
以下代码是我如何注册我的依赖项,然后我检查它们是否都已注册(blah1/blah2)并且都返回“true”。
public AddInBootstrapper(Microsoft.Office.Interop.Outlook.NameSpace session)
{
var containerBuilder = new ContainerBuilder();
containerBuilder.Register(c => new OutlookService(session)).As<IOutlookService>();
containerBuilder.RegisterType<EmailPrintService>().As<IPrintService<Microsoft.Office.Interop.Outlook.MailItem>>();
_container = containerBuilder.Build();
var blah1 = _container.IsRegistered<IOutlookService>();
var blah2 = _container.IsRegistered<IPrintService<Microsoft.Office.Interop.Outlook.MailItem>>();
}
当我稍后单击已连接的 Outlook 功能区按钮时,第一个 (blah1) 显示为仍已注册,第二个 (blah2) 返回“假”
private void printNowButton_Click(object sender, RibbonControlEventArgs e)
{
var blah1 = Globals.ThisAddIn.Core.IsRegistered<IOutlookService>();
var blah2 = Globals.ThisAddIn.Core.IsRegistered<IPrintService<Microsoft.Office.Interop.Outlook.MailItem>>();
var outlookSvc = Globals.ThisAddIn.Core.Resolve<IOutlookService>();
var printSvc = Globals.ThisAddIn.Core.Resolve<IPrintService<Microsoft.Office.Interop.Outlook.MailItem>>();
var folder = outlookSvc.GetFolder(@"public folders\all public folders\testpf\docprinttest\anotherfolder");
foreach (Microsoft.Office.Interop.Outlook.MailItem item in folder.Items)
{
if (item.Attachments.Count > 0)
{
// print attachments
}
printSvc.PrintItem(item);
}
}
有任何想法吗?
编辑(这里是其余的代码):
该过程在 Outlook 加载项加载时开始:
public partial class ThisAddIn
{
private AddInBootstrapper _core;
public AddInBootstrapper Core
{
get { return _core; }
}
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
_core.Dispose();
}
public override void BeginInit()
{
_core = new AddInBootstrapper(this.Application.Session);
base.BeginInit();
}
// some VSTO generated code here (non-modified)
}
IPrintService.cs:
namespace MRP.Outlook.DocPrint.Core.Services.Interfaces
{
public interface IPrintService<T>
{
void PrintItem(T item);
}
}
电子邮件打印服务.cs:
namespace MRP.Outlook.DocPrint.Core.Services
{
public class EmailPrintService : IPrintService<Microsoft.Office.Interop.Outlook.MailItem>
{
public EmailPrintService()
{
}
public void PrintItem(Microsoft.Office.Interop.Outlook.MailItem item)
{
//item.PrintOut();
System.Diagnostics.Debug.WriteLine("Printing: " + item.Subject);
}
}
}
就是这样,加载项加载,它们都已注册,我单击按钮 - 一个已注册,一个未注册。