C# “此故障的创建者未指定原因”异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/341512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"The creator of this fault did not specify a Reason" Exception
提问by Michael Kniskern
I have the following code in WCF service to throw a custom fault based on certain situations. I am getting a "The creator of this fault did not specify a Reason" exception. What am I doing wrong?
我在 WCF 服务中有以下代码可以根据某些情况抛出自定义错误。我收到“此故障的创建者未指定原因”异常。我究竟做错了什么?
//source code
if(!DidItPass)
{
InvalidRoutingCodeFault fault = new InvalidRoutingCodeFault("Invalid Routing Code - No Approval Started");
throw new FaultException<InvalidRoutingCodeFault>(fault);
}
//operation contract
[OperationContract]
[FaultContract(typeof(InvalidRoutingCodeFault))]
bool MyMethod();
//data contract
[DataContract(Namespace="http://myuri.org/Simple")]
public class InvalidRoutingCodeFault
{
private string m_ErrorMessage = string.Empty;
public InvalidRoutingCodeFault(string message)
{
this.m_ErrorMessage = message;
}
[DataMember]
public string ErrorMessage
{
get { return this.m_ErrorMessage; }
set { this.m_ErrorMessage = value; }
}
}
采纳答案by Michael Kniskern
After some addtional research, the following modified code worked:
经过一些额外的研究,以下修改后的代码有效:
if(!DidItPass)
{
InvalidRoutingCodeFault fault = new InvalidRoutingCodeFault("Invalid Routing Code - No Approval Started");
throw new FaultException<InvalidRoutingCodeFault>(fault, new FaultReason("Invalid Routing Code - No Approval Started"));
}
回答by Chris Porter
You might try this in the server config (behaviors -> serviceBehaviors -> behavior):
您可以在服务器配置中尝试此操作(行为 -> serviceBehaviors -> 行为):
<serviceDebug includeExceptionDetailInFaults="true" />
回答by SO User
One can also encounter this exception if one does not specify the FaultContract(typeof(className)) attribute for the method
如果没有为方法指定 FaultContract(typeof(className)) 属性,也可能会遇到此异常
回答by SO User
serviceDebug includeExceptionDetailInFaults="true"
is NOT the solution
不是解决方案
The following code works even with serviceDebug includeExceptionDetailInFaults="false"
以下代码即使使用 serviceDebug includeExceptionDetailInFaults="false"
// data contract
[DataContract]
public class FormatFault
{
private string additionalDetails;
[DataMember]
public string AdditionalDetails
{
get { return additionalDetails; }
set { additionalDetails = value; }
}
}
// interface method declaration
[OperationContract]
[FaultContract(typeof(FormatFault))]
void DoWork2();
// service method implementation
public void DoWork2()
{
try
{
int i = int.Parse("Abcd");
}
catch (FormatException ex)
{
FormatFault fault = new FormatFault();
fault.AdditionalDetails = ex.Message;
throw new FaultException<FormatFault>(fault);
}
}
// client calling code
private static void InvokeWCF2()
{
ServiceClient service = new ServiceClient();
try
{
service.DoWork2();
}
catch (FaultException<FormatFault> e)
{
// This is a strongly typed try catch instead of the weakly typed where we need to do -- if (e.Code.Name == "Format_Error")
Console.WriteLine("Handling format exception: " + e.Detail.AdditionalDetails);
}
}
There is no need to add fault reason if its not required. Just make sure the FaultContract attribute is correct
如果不需要,则无需添加故障原因。只要确保 FaultContract 属性是正确的
回答by SO User
By using strongly typed try catch, I was able get around with the error "The creator of this fault did not specify a Reason".
通过使用强类型 try catch,我能够解决错误“此故障的创建者未指定原因”。
回答by ScottG
I have code exactly like Rashmi has and I got the "The creator of this fault...." error. It was happening when I was debugging in VS2010. I found this post:
我的代码和 Rashmi 的代码完全一样,我收到了“这个错误的创造者......”错误。这是我在 VS2010 中调试时发生的。我找到了这个帖子:
which explained a couple of debugging options that I needed to turn off. Problem solved.
这解释了我需要关闭的几个调试选项。问题解决了。
回答by sandeep patil
If you do not want to be notified of such exceptions go to Debug -> Exceptions and uncheck "User-unhandled" for "Common Language Runtime Exceptions" or for specific exceptions.
如果您不想收到此类异常的通知,请转至调试 -> 异常并取消选中“用户未处理”的“公共语言运行时异常”或特定异常。
回答by user386451
I solved this problem using a two parameter constructer.
我使用两个参数构造函数解决了这个问题。
// service method implementation
throw new FaultException<FormatFault>(fault,new FaultReason(fault.CustomFaultMassage));
CustomFaultMassage is property from data contract.
CustomFaultMassage 是数据契约的属性。
回答by pencilslate
Updating the service reference in the client solved the problem. Same could work for you.
更新客户端中的服务引用解决了问题。同样可以为你工作。
回答by Daniel Davis
The short answer is you are doing nothing wrong, just reading the results incorrectly.
简短的回答是您没有做错任何事情,只是错误地读取结果。
On the client side when you catch the error, what is caught is of the type System.ServiceModel.FaultException<InvalidRoutingCodeFault>
.
Your InvalidRoutingCodeFault
object is actually in the .detail
property of the FaultException. SO....
在客户端,当您捕获错误时,捕获的类型为System.ServiceModel.FaultException<InvalidRoutingCodeFault>
。
您的InvalidRoutingCodeFault
对象实际上位于.detail
FaultException的属性中。所以....
// client code
// 客户端代码
private static void InvokeMyMethod()
{
ServiceClient service = new MyService.ServiceClient();
try
{
service.MyMethod();
}
catch (System.ServiceModel.FaultException<InvalidRoutingCodeFault> ex)
{
// This will output the "Message" property of the System.ServiceModel.FaultException
// 'The creator of this fault did not specify a Reason' if not specified when thrown
Console.WriteLine("faultException Message: " + ex.Message);
// This will output the ErrorMessage property of your InvalidRoutingCodeFault type
Console.WriteLine("InvalidRoutingCodeFault Message: " + ex.Detail.ErrorMessage);
}
}
The Message property of the FaultException is what is displayed on the error page so if it's not populated like in John Egerton's post, you will see the 'The creator of this fault did not specify a Reason' message. To easily populate it, use the two parameter constructor when throwing the fault in the service as follows, passing your error message from your fault type:
FaultException 的 Message 属性显示在错误页面上,因此如果它没有像 John Egerton 的帖子那样填充,您将看到“此故障的创建者未指定原因”消息。要轻松填充它,请在服务中抛出错误时使用两个参数构造函数,如下所示,从您的错误类型传递错误消息:
InvalidRoutingCodeFault fault = new InvalidRoutingCodeFault("Invalid Routing Code - No Approval Started");
throw new FaultException<InvalidRoutingCodeFault>(fault, new FaultReason(fault.ErrorMessage));