我得到一个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)) { }
}
为什么这不起作用?我想我想念一些简单的东西......