我有一个项目,我应该在其中编写调用另一个WCF服务的WCF服务。它看起来如下:
[ServiceContract]
public interface IAsurService
{
[OperationContract(ReplyAction = AsurService.ReplyAction_GetCatalogList)]
Message GetCatalogList();
public Message GetCatalogList()
{
// The external client service
GetNsiClient client = new GetNsiClient();
authContext auth = new authContext
{
company = "asur_nsi",
password = "lapshovva",
user = "dogm_LapshovVA"
};
catalogs catalogs = client.getCatalogList(auth);
在这种情况下如何处理错误?我可以使用这样的标准故障契约(Contract)方法:
[DataContract]
public class AsurDataFaultException
{
private string reason;
[DataMember]
public string Reason
{
get { return reason; }
set { reason = value; }
}
}
public Message GetCatalogList()
{
// The external client service
GetNsiClient client = new GetNsiClient();
authContext auth = new authContext
{
company = "asur_nsi",
password = "lapshovva",
user = "dogm_LapshovVA"
};
catalogs catalogs = null;
try
{
catalogs = client.getCatalogList(auth);
}
catch (Exception exception)
{
AsurDataFaultException fault = new AsurDataFaultException();
fault.Reason = "The error: " + exception.Message.ToString();
throw new FaultException<AsurDataFaultException>(fault);
}
或者是其他东西?
先感谢您。
戈兰
最佳答案
如果远程WCF服务的失败导致代码失败,那么显然您应该抛出一个错误,但是该错误不应公开远程WCF服务的内部详细信息或其异常,例如:
catch (Exception exception)
{
// Indicate all that you may expose -- that the data is unavailable.
DataUnavailableFault fault = new DataUnavailableFault();
throw new FaultException<DataUnavailableFault>(fault);
}
当然,您也可以使用重试机制,但是很显然,如果最终导致的是远程服务失败,则应通过抛出适当的错误来向调用方表明这一点。
关于wcf - WCF : How to handle errors when calling another WCF service?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12892648/