客户端未正确捕获 WCF 自定义故障异常 [英] WCF custom fault exception not caught correctly in client
问题描述
我正在尝试创建一些自定义FaultException
.我创建了一个名为 CreateFault
的 DataContract
类.
I'm trying to create some custom FaultException
. I've made a DataContract
class called CreateFault
.
[DataContract]
public class CreateFault
{
private string report;
public CreateFault(string message)
{
this.report = message;
}
[DataMember]
public string Message
{
get { return this.report; }
set { this.report = value; }
}
}
然后我在服务方法中抛出错误.
I'm then throwing the fault in a service method.
在IService1.cs
[OperationContract]
[FaultContract(typeof(CreateFault))]
void TestFaultException();
和Service1.cs
public void TestFaultException()
{
throw new FaultException<CreateFault>(new CreateFault("CreateFault message"), "Message abt exception");
}
我在客户端中捕获了 FaultException
.
I catch the FaultException
in my client.
private void btnTest_Click(object sender, RoutedEventArgs e)
{
try
{
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
client.TestFaultException();
}
catch (FaultException<CreateFault> ex)
{
MessageBox.Show(ex.Detail.Message, "Success", MessageBoxButton.OK, MessageBoxImage.Error);
}
catch (FaultException ex)
{
MessageBox.Show(ex.Message, "Failure", MessageBoxButton.OK, MessageBoxImage.Error);
}
catch (Exception ex)
{
}
}
现在问题来了.当我在 Visual Studio 2010 中创建 WCF 服务应用程序项目时,它按预期工作.错误在:
Now here comes the problem. When I create a WCF Service Application project in Visual Studio 2010 it works like expected. The error is caught in:
catch (FaultException<CreateFault> ex)
但是当我使用自定义FaultExceptions
创建 WCF 服务库项目时,客户端无法识别我的自定义异常.相反,它会捕获以下错误:
But when I create a WCF Service Library project with my custom FaultExceptions
the client does not recognize my custom exception. It instead catches the error in:
catch (FaultException ex)
为什么它不适用于 WCF 服务应用程序项目?
Why does it not work with WCF Service Application Project?
这是我在调试过程中捕获到异常时得到的
This is what i get during debugging when it catches the exception in
catch (FaultException ex)
(在立即窗口中输入 ?ex)
(typed ?ex in Immediate window)
{"消息 abt 异常"}
{"Message abt exception"}
[System.ServiceModel.FaultException<WpfApplication1.ServiceReference2.CreateFault>]: {"Message abt exception"}
base {System.ServiceModel.CommunicationException}: {"Message abt exception"}
Action: "http://tempuri.org/IService1/TestFaultExceptionCreateFaultFault"
Code: {System.ServiceModel.FaultCode}
Message: "Message abt exception"
Reason: {Message abt exception}
编辑 2:
发现问题.我有两个 Service 引用,它们都有 CreateFault DataContract.当我运行程序时,它使用了错误的程序.
Found the problem. I had two Service references who both had the CreateFault DataContract. And it was using the wrong one when i ran the program.
当我变成
catch (FaultException<ServiceReference2.CreateFault> ex)
成功了
推荐答案
发现问题.我有两个 Service 引用,它们都有 CreateFault DataContract.当我运行程序时,它使用了错误的程序.
Found the problem. I had two Service references who both had the CreateFault DataContract. And it was using the wrong one when i ran the program.
当我变成
catch (FaultException<ServiceReference2.CreateFault> ex)
成功了
这篇关于客户端未正确捕获 WCF 自定义故障异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!