我对Autofac非常陌生并且遇到了一个问题。
以下代码是我如何注册依赖项的方式,之后我会检查它们是否都已经被注册并且都返回“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)返回“false”。
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);
}
}
EmailPrintService.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);
}
}
}
那就是这样,插件已经加载,它们都已注册,我点击按钮——其中一个已注册,另一个没有。