【问题标题】:Throw exception with inner exception in WCF在 WCF 中引发带有内部异常的异常
【发布时间】:2015-05-13 13:25:52
【问题描述】:

我试图通过 WCF 线路发送异常,但无法弄清楚我做错了什么。 我正在关注guidance of Oleg SychMSDN 但无济于事。

我返回的是The requested service, 'net.tcp://mymachine/myservicepath/MyService.svc' could not be activated. See the server's diagnostic trace logs for more information.

[ServiceContract]
public interface ISystemInfoService
{
    [OperationContract]
    [FaultContract(typeof(MyException))]
    void DoThrowException(string message);
}

//[ServiceContract]  // <- the culprit
public class SystemInfoService : ISystemInfoService
{
    public void DoThrowException(string message)
    {
        try
        {
            throw new MyException( "MyMessage" );
        }
        catch (MyExceptionexc)
        {
            throw new FaultException<MyException>(exc);
        }
    }
}

// The custom Exception resides in an common assembly reachable from both server and client.
[Serializable]
public class MyException: Exception
{
...
}

TIA

【问题讨论】:

  • 那么,你See the server's diagnostic trace logs for more information了吗?
  • 好发现@nvoigt - 我很惭愧地回答我没有。当我检查事件查看器-> 自定义视图-> 管理事件时,我发现 ... Contract inheritance can only be used among interface types. If a class is marked with ServiceContractAttribute, it must be the only type in the hierarchy with ServiceContractAttribute... 这显然是罪魁祸首。我已经更新了原始代码以显示这一点。
  • @nvoigt - 请回答有关如何访问“服务器的诊断跟踪日志”的说明,我很乐意为您提供应得的积分。

标签: c# .net wcf exception-handling


【解决方案1】:

您可以尝试使用数据合同类而不是可序列化的异常来处理异常吗?

[DataContract]
public class MyExceptionClass
{
    [DataMember]
    public Exception Exc { get; set; }
}

[ServiceContract]
public interface ISystemInfoService
{
    [OperationContract]
    [FaultContract(typeof(MyExceptionClass))]
    void DoThrowException(string message);
}

public class SystemInfoService : ISystemInfoService
{
    public void DoThrowException(string message)
    {
        try
        {
            throw new Exception("MyMessage");
        }
        catch (Exception exc)
        {
            var data = new MyExceptionClass { Exc = exc };
            throw new FaultException<MyExceptionClass>(data);
        }
    }
}

【讨论】:

  • 这是我的第一次尝试,但失败了。这可能是一个非常好的方法;我只是如何弄清楚如何。
  • 通常您不希望使用 WCF 服务向客户端公开实际异常(和堆栈跟踪)。您应该只发送有意义的错误代码和消息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-08
  • 1970-01-01
  • 2012-03-24
  • 1970-01-01
  • 2010-11-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多