C# 使用包含方法的自定义FaultException类型
在我的WCF服务中,我有两种自定义故障类型。一个名为BaseFault的抽象类型和它的两个实现名为TypeOneFault和TypeTwoFault 我像这样在服务端抛出异常C# 使用包含方法的自定义FaultException类型,c#,.net,wcf,exception-handling,C#,.net,Wcf,Exception Handling,在我的WCF服务中,我有两种自定义故障类型。一个名为BaseFault的抽象类型和它的两个实现名为TypeOneFault和TypeTwoFault 我像这样在服务端抛出异常 public string ThisMethodHasFault(string eType) { if (eType.Contains("One")) { TypeOneFault one = new TypeOneFault("TypeOneFault thrown");
public string ThisMethodHasFault(string eType)
{
if (eType.Contains("One"))
{
TypeOneFault one = new TypeOneFault("TypeOneFault thrown");
throw new FaultException<TypeOneFault>(one, new FaultReason(new FaultReasonText("Fault reason here")));
}
else
{
TypeTwoFault two = new TypeTwoFault("TypeTwoFault thrown");
throw new FaultException<TypeTwoFault>(two, new FaultReason(new FaultReasonText("Fault reason here")));
}
return "";
}
[OperationContract]
[FaultContract(typeof(TypeOneFault ))]
[FaultContract(typeof(TypeTwoFault ))]
string ThisMethodHasFault(string eType);
MyServiceClient client = new MyServiceClient();
try
{
client.ThisMethodHasFault(""); //get value from user
}
catch (FaultException<TypeOneFault> ox)
{
TypeOneFault oneEx = ox.Detail;
oneEx.{property} ...
}
catch (FaultException<TypeTwoFault> tx)
{
TypeTwoFault twoEx = tx.Detail;
twoEx.{property} ...
}
在客户端,我有一个测试winform应用程序,在这里我像这样捕获它
public string ThisMethodHasFault(string eType)
{
if (eType.Contains("One"))
{
TypeOneFault one = new TypeOneFault("TypeOneFault thrown");
throw new FaultException<TypeOneFault>(one, new FaultReason(new FaultReasonText("Fault reason here")));
}
else
{
TypeTwoFault two = new TypeTwoFault("TypeTwoFault thrown");
throw new FaultException<TypeTwoFault>(two, new FaultReason(new FaultReasonText("Fault reason here")));
}
return "";
}
[OperationContract]
[FaultContract(typeof(TypeOneFault ))]
[FaultContract(typeof(TypeTwoFault ))]
string ThisMethodHasFault(string eType);
MyServiceClient client = new MyServiceClient();
try
{
client.ThisMethodHasFault(""); //get value from user
}
catch (FaultException<TypeOneFault> ox)
{
TypeOneFault oneEx = ox.Detail;
oneEx.{property} ...
}
catch (FaultException<TypeTwoFault> tx)
{
TypeTwoFault twoEx = tx.Detail;
twoEx.{property} ...
}
问题:
在上面的方法中,我只获取自定义fault类的属性,而不是我在其中定义的方法。该方法在我在子类TypeOne和TypeTwo中实现的基础抽象类中定义为抽象。是否也有方法获取该方法?方法不会序列化。只有数据。方法不会序列化。仅数据。支持的序列化仅用于数据。本质上,任何web服务都必须隐藏实现并只向您提供服务。序列化的是使任何客户机都能有效交互的数据类型。不需要序列化方法。支持的序列化仅用于数据。本质上,任何web服务都必须隐藏实现并只向您提供服务。序列化的是使任何客户机都能有效交互的数据类型。不需要序列化方法。请每个问题一个问题。完成。在此处添加第二个>>每个问题一个问题,请。完成。在这里添加第二个>>谢谢John。关于我提出的第二个问题,我们能做些什么吗。另问一个问题。每个问题一个问题在这里发布第二个>>谢谢John。关于我提出的第二个问题,我们能做些什么吗。另问一个问题。每个问题一个问题张贴第二个问题在此处>>