根据this blog post by Adam Najmanowicz,使用Sitecore.Jobs.AsyncUI中的JobMonitor和JobContext类从后台作业打开警报或确认对话框应该相对容易。我无法让它发挥作用。谁能告诉我我失踪了什么?我控制的CodeBeside类如下。我尝试将它作为模态对话框和应用程序打开,但它不显示任何警报。
public class JobMonitorTestForm : BaseForm
{
protected JobMonitor Monitor;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (this.Monitor == null)
{
if (!Context.ClientPage.IsEvent)
{
this.Monitor = new JobMonitor { ID = "Monitor" };
Context.ClientPage.Controls.Add(this.Monitor);
}
else
{
this.Monitor = Context.ClientPage.FindControl("Monitor") as JobMonitor;
}
}
this.Monitor.JobFinished += this.JobFinished;
this.Monitor.JobDisappeared += this.JobFinished;
this.Monitor.Start("JobMonitorTest", "Test", this.Run);
}
private void JobFinished(object sender, EventArgs eventArgs)
{
SheerResponse.Alert("Job finished!");
}
private void Run()
{
Thread.Sleep(2000);
JobContext.Alert("Hello!");
Thread.Sleep(2000);
}
}
更新
我找到了另一条线索,但我仍然不知所措。我的日志文件显示以下异常。它是Sitecore.Web.UI.Sheer.ClientPage的构造函数中的空引用异常,因为HttpContext.Current为null。这是有道理的,因为调用是从后台线程启动的,但我认为AsyncUI命名空间中的类的重点是解决这个问题。
ManagedPoolThread #3 23:18:28 ERROR Exception
Exception: System.Reflection.TargetInvocationException
Message: Exception has been thrown by the target of an invocation.
Source: mscorlib
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at (Object , Object[] )
at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
at Sitecore.Jobs.Job.ThreadEntry(Object state)
Nested Exception
Exception: System.NullReferenceException
Message: Object reference not set to an instance of an object.
Source: Sitecore.Kernel
at Sitecore.Web.UI.Sheer.ClientPage..ctor()
at Sitecore.Web.UI.Sheer.SheerResponse.Alert(String text, String[] arguments)
at TestApp.sitecore.shell.Applications.Dialogs.JobMonitorTest.JobMonitorTestForm.Run() in c:\Projects\sc66Test\TestApp\TestApp\sitecore\shell\Applications\Dialogs\JobMonitorTest\JobMonitorTestForm.cs:line 45
at Sitecore.Jobs.AsyncUI.JobMonitor.TaskRunner.Run()
答案 0 :(得分:4)
您发布的代码存在一个小问题,就像异步用户界面中的任何内容引起了大量调查一样:)
问题在于时间问题。您无法在OnLoad中执行作业创建,因为在请求有时间往返用户并由浏览器中的监视器控件处理之前,它将启用监视器。基本上,约伯还没有什么可以坚持的。
它有两种方法。您可以显示对话框,也可以作为用户按下按钮/功能区/菜单,这将触发事件以启动作业。另一种方法就是我在PowerShell Extensions中所做的,当我希望跑步者在没有用户干预的情况下开始工作时。在Sheer控件中包含一个JavaScript,它会在页面加载后将事件发布到对话框中:
window.onload=function(){
scForm.postRequest("", "", "", "psr:execute");
};
然后在你的控件的代码旁边:
[HandleMessage("psr:execute", true)]
protected virtual void Execute(ClientPipelineArgs args)
{
// Do the Job starting through the monitor.
}
希望有所帮助。如果您有任何问题,我将非常乐意分享更多细节或提供样品