无论我多么努力,我似乎都无法在 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 中使用行为扩展,如此处所述,但我收到警告说扩展无效。
如何正确处理 Silverlight 中的 WCF 错误?提前致谢。