【问题标题】:How to properly handle WCF faults with Silverlight?如何使用 Silverlight 正确处理 WCF 错误?
【发布时间】:2012-04-20 15:00:16
【问题描述】:

无论我多么努力,我似乎​​都无法在 Silverlight 中处理 WCF 错误。 事实上,错误似乎永远不会离开服务器!

例如当我调试它时,它停在我抛出 FaultException 说它没有被处理的那一行:

[SilverlightFaultBehavior]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class StoreService : IStoreContract
{
    public System.Collections.Generic.List<string> GetStoreDesignNames()
    {
        try
        {
            StoreDataContext swdc = new StoreDataContext();
            var query = from storeDesign in swdc.StoreDesignDBs select storeDesign.Name;
            return query.ToList();
        }
        catch (System.Data.SqlClient.SqlException sqlExcept)
        {
            throw new FaultException<SqlFault>(new SqlFault() { Message = sqlExcept.Message });
        }
    }
}

实现此方法的类派生自合约接口:

[ServiceContract(Namespace = "Store")]
public interface IStoreContract
{
    /// <summary>
    /// Obtain the list of store design names.
    /// </summary>
    [OperationContract,
     FaultContract(typeof(SqlFault))]
    List<String> GetStoreDesignNames();
}

而SqlFault类是这样定义的:

public class SqlFault
{
    public String Message { get; set; }
}

在客户端,我按如下方式处理错误:

        // swc is the client
        swc.GetStoreDesignNamesCompleted += new EventHandler<ServiceReference.GetStoreDesignNamesCompletedEventArgs>((obj, evt) =>
        {
            if (evt.Error == null)
            {
                // In case of success
                MessageBox.Show(evt.Result.First());
            }
            else if (evt.Error is FaultException<ServiceReference.SqlFault>)
            {
                FaultException<ServiceReference.SqlFault> fault = evt.Error as FaultException<ServiceReference.SqlFault>;
                Dispatcher.BeginInvoke(() =>
                {
                    ErrorWindow ew = new ErrorWindow(fault.Detail.Message, "No details");
                    ew.Show();
                });
            }
        });
        swc.GetStoreDesignNamesAsync();

我尝试将[SilverlightFaultBehavior]属性放在界面上,但无济于事。即使我没有接口我仍然有这个错误。

我还尝试在 web.config 中使用行为扩展作为 described here,但我收到警告说扩展无效。

如何正确处理 Silverlight 中的 WCF 错误? 提前致谢。

【问题讨论】:

    标签: wcf silverlight wcf-ria-services silverlight-5.0


    【解决方案1】:

    我没有使用过 WCF(一直在使用 WCF RIA 服务),但不久前我确实看到了这篇文章。

    Getting something better than “Server not found.” from WCF in Silverlight

    【讨论】:

      【解决方案2】:

      在与这个问题斗争了几个小时后,我终于一起破解了一些可行的东西。 这真的是一个可怕的 hack,我更愿意使用 BehaviorExtension 来完成这项任务。诀窍是在 WCF 方法的主体中手动设置 HTTP 状态代码,如下所示:

          public System.Collections.Generic.List<string> GetStoreDesignNames()
          {
              try
              {
                  StoreDataContext swdc = new StoreDataContext();
                  var query = from storeDesign in swdc.StoreDesignDBs select storeDesign.Name;
                  return query.ToList();
              }
              catch (System.Data.SqlClient.SqlException sqlExcept)
              {
                  System.ServiceModel.Web.WebOperationContext ctx = System.ServiceModel.Web.WebOperationContext.Current;
                  ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;
                  throw new FaultException<SqlFault>(new SqlFault() { Message = sqlExcept.Message });
              }
          }
      

      然后错误消息会正确显示在客户端。 如果有人有比这更好的解决方案,我想听听。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-16
        • 1970-01-01
        • 1970-01-01
        • 2019-05-25
        • 1970-01-01
        • 1970-01-01
        • 2018-12-06
        相关资源
        最近更新 更多