我對Autofac非常陌生,並且遇到問題。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);
}
}
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);
}
}
}
就是這樣,加載項加載,他們都註冊,我點擊按鈕 - 一個是註冊,一個不是。
我不認爲問題出現在您向我們展示的代碼中,但在代碼中您不是。 – 2012-07-19 17:20:24
可能是這樣,但我不知道如何。其餘部分現在添加。 – raydenl 2012-07-19 21:22:52
顯然與通用接口IPrintService有關,就好像我從該接口刪除泛型一樣,它仍然在容器中註冊。 – raydenl 2012-07-19 21:41:39