【发布时间】:2013-03-01 15:37:54
【问题描述】:
我正在 Windows 运行时组件中实现 IBackgroundTask,我想向其中注入一个记录器依赖项,但应用程序在调用后台任务时退出。它甚至不会进入构造函数。我使用 Ninject 作为 DI 容器,在应用程序的其他任何地方使用它都没有问题。
我想做这样的事情:
private readonly ILog _logger;
public BackroundTask(ILog logger)
{
_logger = logger;
}
public async void Run(IBackgroundTaskInstance taskInstance)
{
var deferral = taskInstance.GetDeferral();
try
{
// do something here
}
catch (Exception ex)
{
// log the error with injected logger...
logger.ErrorFormat("{0}Error in QueueTimer {1}{0}",
Environment.NewLine, ex.ToString());
}
deferral.Complete();
}
我尝试过的任何事情都没有成功,现在只需登录调用 .cs 文件中的 OnComplete 方法。
【问题讨论】:
-
ILOG 必须经过编译才能支持 WinRT。究竟是什么错误?
-
在不相关的注释中,
async void通常应避免使用,但事件处理程序除外。您可能有充分的理由这样做,但如果您不这样做,则需要考虑一些事情。 -
我正在使用将数据发送回服务器的异步调用,然后我需要记录发送的成功或失败。
-
ILog 被编译为 Windows 运行时组件。事件日志中的错误是:激活应用程序 a.Winrt.Ui_tjekt9a7fmk0j!App for the Windows.BackgroundTasks contract failed with error: Not implemented
标签: c# dependency-injection background windows-runtime