gpt4 book ai didi

c# - 客户端不会捕获通用 FaultException< T >,只会捕获 FaultException

转载 作者:可可西里 更新时间:2023-11-01 07:46:22 24 4
gpt4 key购买 nike

我已经阅读了关于此的所有内容,但也许我遗漏了一些东西(好吧,我肯定遗漏了一些东西,否则它已经可以工作了)

我在我的服务器业务层中抛出了一些异常错误:

public class RfcException :  Exception
{
public RfcException(string _m, Exception _inner) : base(_m, _inner)
{ }

public Dictionary<string, string> ExtendedProperties
{
get { return extendedProperties; }
protected set { extendedProperties = value; }
}

private Dictionary<string, string> extendedProperties = new Dictionary<string, string>();
}

我在服务中没有处理,但我有一个 IErrorHandler捕捉并创造一个FaultMessage :

public class FaultErrorHandler : BehaviorExtensionElement, IErrorHandler, IServiceBehavior
{
public bool HandleError(Exception error)
{
if (!Logger.IsLoggingEnabled()) return true;
var logEntry = new LogEntry
{
EventId = 100,
Severity = TraceEventType.Error,
Priority = 1,
Title = "WCF Failure",
Message = string.Format("Error occurred: {0}", error)
};
logEntry.Categories.Add("MiddleTier");

Logger.Write(logEntry);
return true;
}

public void ProvideFault(Exception error, System.ServiceModel.Channels.MessageVersion version, ref System.ServiceModel.Channels.Message fault)
{
if (error is RfcException)
{
RfcException rfcException = error as RfcException;
var serviceFault = new RfcServiceFault(rfcException);
var faultException = new FaultException<RfcServiceFault>(serviceFault, new FaultReason(string.Format("System error occurred, exception: {0}", error)));
var faultMessage = faultException.CreateMessageFault();
fault = Message.CreateMessage(version, faultMessage, Schema.WebServiceStandard);
}
else
{
var faultException = new FaultException<Exception>(error, new FaultReason(string.Format("System error occurred, exception: {0}", error)));
var faultMessage = faultException.CreateMessageFault();
fault = Message.CreateMessage(version, faultMessage, Schema.WebServiceStandard);
}
}

public void AddBindingParameters(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{ }

public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
{
foreach (ChannelDispatcher chanDisp in serviceHostBase.ChannelDispatchers)
{
chanDisp.ErrorHandlers.Add(this);
};
}

public void Validate(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
{ }

public override Type BehaviorType
{
get { return typeof(FaultErrorHandler); }
}

protected override object CreateBehavior()
{
return new FaultErrorHandler();
}
}

不用问;我已经通过调试器确认它进入了 if (error is RfcException) 部分部分,我已经逐步完成了该代码,并且毫无问题地到达了最后。errorHandler 包装了一个 FaultException<RfcServiceFault>消息,RfcServiceFault消息是这样的

[DataContract(Name = "RfcServiceFault", Namespace = "Service.DataTransfer.Rfc")]
public class RfcServiceFault
{
public RfcServiceFault(RfcException rfcException) : this( (Exception)rfcException )
{
ExtendedProperties = new Dictionary<string, string>(rfcException.ExtendedProperties);
}

public RfcServiceFault()
{ }

public RfcServiceFault(Exception regularException)
{
FaultMessage = regularException.Message;
StackTrace = regularException.StackTrace;
}

public Dictionary<string, string> ExtendedProperties
{
get { return extendedProperties; }
protected set { extendedProperties = value; }
}

[DataMember]
private Dictionary<string, string> extendedProperties = new Dictionary<string, string>();

[DataMember]
public string FaultMessage { get; set; }

[DataMember]
public string StackTrace { get; set; }
}

该服务具有所有应该具有带有故障契约(Contract)的 wcf 服务的注释:

[ServiceContract(Name = "MyService", Namespace = Schema.WebServiceStandard, SessionMode = SessionMode.Allowed)]
public interface IMyService
{
[OperationContract(Name = "GetStuff")]
[FaultContract(typeof(RfcServiceFault) , Name="RfcServiceFault", Namespace="Service.DataTransfer.Rfc")]
LookupResult GetStuff();
}

现在:在客户端测试,像这样的简单测试:

try
{
var result = myService.GetStuff();
Assert.IsTrue(!string.IsNullOrEmpty(result));
}
catch (FaultException<RfcServiceFault> rfcEx)
{
// OMG FOR THE LIFE OF THE PUPPIES ENTER HERE
}
catch (FaultException rr)
{
// it always falls in here
}
catch (Exception ex)
{ }

我读过很多很多关于这个类似问题的帖子:

但到目前为止似乎没有任何帮助,我已经尝试在 web.config 中设置 WCF 跟踪:

<system.diagnostics>
<sources>
<source name="System.ServiceModel"
switchValue="Information, ActivityTracing">
<listeners>
<add name="log"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData="c:\Traces.svclog" />
</listeners>
</source>
</sources>
</system.diagnostics>

我在那里得到一个 svclog 文件,我用 WCF Trace 查看器打开,但我只看到一堆消息,黄色的显示异常,但它只确认客户端已经看到的内容,System.ServiceModel.FaultException正在接收,而不是通用的

有什么办法解决这个问题吗?

编辑 忘了说,我在配置中启用了我的错误处理程序,如下所示:

<behaviorExtensions>
.....
<add name="faultErrorHandlerBehavior"
type="Service.FaultErrorHandler,Service, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
.....
</behaviorExtensions>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceThrottling maxConcurrentCalls="200" maxConcurrentSessions="200"
maxConcurrentInstances="200" />
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<faultErrorHandlerBehavior />
</behavior>
</serviceBehaviors>

最佳答案

(这有点摸不着头脑)我相信您可能遇到了问题,因为对故障的操作与客户端的预期不符。您能否将您的 CreateMessage 替换为以下内容:

fault = Message.CreateMessage(version, faultMessage, faultException.Action);

关于c# - 客户端不会捕获通用 FaultException< T >,只会捕获 FaultException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6588320/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com