1

私は取得しています
Type 'WcfServiceLibrary1.GetDataErrorException' cannot be ISerializable and have DataContractAttribute attribute.

WCF サービスにカスタム例外を追加しようとしたとき。

 [ServiceContract]
public interface IService1 {

    [OperationContract]
    [FaultContract(typeof(GetDataErrorException))]
    string GetData(int value);
}

public class Service1 : IService1 {
    public string GetData(int value) {
        if (value.Equals(0))
            throw new FaultException<GetDataError>(new GetDataError(), new FaultReason("Zero"));

        return string.Format("You entered: {0}", value);
    }
}

[DataContract]
public class GetDataError {
    public GetDataError() { }
}

[DataContract]
public class GetDataErrorException : FaultException<GetDataError> {
    public GetDataErrorException(string message) :
        base(new GetDataError(), new FaultReason(message)) { }
}

これが機能しないのはなぜですか?シンプルなものが足りないと思います...

4

2 に答える 2

1

両方を持つことはできませんし、すべきではありません。それはあなたが得た例外から明らかです。フレームワークは、2 つのシリアライザーを確認すると、あいまいな状態を認識します。詳細については、次のブログを参照してください

http://blogs.msdn.com/b/sowmy/archive/2006/05/14/597476.aspx

あなたの障害コントラクトは次のようになります

public class DataErrorException : FaultException<GetDataError> 
{
    public DataErrorException(string message) :
            base(new GetDataError(), new FaultReason(message)) { }
}

ここでの唯一の変更点は、DataContract属性を削除したことです。

于 2014-04-16T15:19:24.613 に答える