0

我有一个 WCF 服务,它应该从某个范围内返回特定机器的最大日志日期,或者如果该机器没有日志条目,则返回 null:

    public DateTime? GetLastBootEvent(string laptopName)
    {
       ITDashboardDataContext itdb = new ITDashboardDataContext();

       DateTime? latestEvent = (from be in itdb.tl_sta_bootTimes
                       where be.machineName.ToUpper() == laptopName.ToUpper()
                       select be.timestamp
                       ).Max();

      return latestEvent;

   }

但是,当我运行它时,我收到以下错误:

“由于内部错误,服务器无法处理请求。有关错误的更多信息,请在服务器上打开 IncludeExceptionDetailInFaults(来自 ServiceBehaviorAttribute 或来自配置行为),以便将异常信息发送回客户端,或根据 Microsoft .NET Framework SDK 文档打开跟踪并检查服务器跟踪日志。”

那里没有太多信息。我认为通过使用DateTime?而不是DateTime这个应该允许返回空值?

我可以通过返回一些过去的随机日期的方法来处理这个问题,比如 MinDate,但我想干净地做到这一点。

4

3 回答 3

1

您需要将查询中的时间戳转换为可为空的日期时间,即

DateTime? latestEvent = (from be in itdb.tl_sta_bootTimes
                       where be.machineName.ToUpper() == laptopName.ToUpper()
                       select (DateTime?)be.timestamp
                       ).Max();
于 2013-10-11T09:19:44.527 回答
1

听起来你想在这里使用DefaultIfEmpty例如

DateTime? latestEvent = (from be in itdb.tl_sta_bootTimes
                         where be.machineName.ToUpper() == laptopName.ToUpper()
                         select be.timestamp
                        ).DefaultIfEmpty(null).Max();

如果没有记录,Max将在没有记录的情况下抛出异常。

于 2013-10-11T09:17:13.407 回答
0

I agree with wiero, it looks like some exception was thrown during the execution. To get exception on client side you can turn on exceptions transferring using IncludeExceptionDetailInFaults parameter in web.config (look at Turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the <serviceDebug> configuration behavior) on the server for example; however it will be better to turn it off on production).

If you get this exception details it will be easier to understand where the problem is.

于 2013-10-11T09:25:18.300 回答