我有一项基于以下合同(节录)的服务:
[ServiceContract]
public interface ISchedulerService
{
[OperationContract]
void Process(bool isForced);
}
实施的密切相关(恕我直言)部分是:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, UseSynchronizationContext = false)]
public class SchedulerService : ISchedulerService
{
public async void Process(bool isForced)
{
try
{
var prevStatus = Status;
Status = SchedulerStatus.Processing;
await ProcessListings();
Status = prevStatus;
}
catch (Exception ex)
{
throw new FaultException(ex.Message);
}
}
private static Task ProcessListings()
{
throw new Exception("ProcessListings failed.");
return Task.Delay(5000);
}
}
该服务目前托管在一个小型控制台应用程序中:
class Program
{
private static readonly SchedulerService Scheduler = SchedulerService.Instance;
private static ServiceHost schedulerHost;
protected static void OnStart()
{
try
{
if (schedulerHost != null)
{
schedulerHost.Close();
schedulerHost = null;
}
schedulerHost = new ServiceHost(Scheduler);
schedulerHost.Open();
Scheduler.Start();
}
catch (Exception ex)
{
//EventLog.WriteEntry("Exception: " + ex.Message);
throw;
}
}
}
最后,客户:
private readonly SchedulerServiceClient _proxy= new SchedulerServiceClient();
...
void ExecuteProcessNowCommand()
{
try
{
_proxy.Process(true);
}
catch (Exception ex)
{
if (exception is SchedulerException)
{
MessageBoxFacility.ProcessingError((SchedulerException)exception);
}
}
}
SchedulerServiceClient
添加服务引用时生成的代理在哪里。到目前为止,我已经成功地将 WCF 托管在实时 Windows 服务中并测试了非异常功能。在我添加异常处理之前一切都很好。我知道这是一个复杂的场景,但我见过的大多数示例都表明FaultException
至少会被Exception
客户端中的通用处理程序捕获。我的调试器让我猜想这个异常甚至没有进入代理,并且它在 MVC 代码中仍未处理。当我点击“继续”足够多时,我最终会出现一个屏幕,告诉我堆栈只包含外部代码。这是该外部代码的堆栈跟踪:
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>b__1(Object state)
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()</ExceptionString></Exception></TraceRecord>
这让我觉得我async
在 MCV 服务中的使用可能是我扮演了一个角色,但也许它只是一个无辜的中间人。请帮助我尝试确定为什么异常甚至没有传播到客户端。