public void FaultContractInfos ()
{
var host = new ServiceHost (typeof (TestFaultContract));
- host.Description.Behaviors.Find<ServiceDebugBehavior> ().IncludeExceptionDetailInFaults = true;
+ host.Description.Behaviors.Find<ServiceDebugBehavior> ().IncludeExceptionDetailInFaults = false;
host.AddServiceEndpoint (typeof (ITestFaultContract), new BasicHttpBinding (), new Uri ("http://localhost:37564"));
host.Open ();
try {
var cf = new ChannelFactory<ITestFaultContract> (new BasicHttpBinding (), new EndpointAddress ("http://localhost:37564"));
var cli = cf.CreateChannel ();
- cli.Run ("test");
- } catch (FaultException<PrivateAffairError> ex) {
- var p = ex.Detail;
- Assert.AreEqual (5, p.ErrorCode, "#1");
- Assert.AreEqual ("foobarerror", p.Text, "#2");
+ try {
+ cli.Run ("default");
+ Assert.Fail ("#1");
+ } catch (FaultException<PrivateAffairError> ex) {
+ var p = ex.Detail;
+ Assert.AreEqual (5, p.ErrorCode, "#2");
+ Assert.AreEqual ("foobarerror", p.Text, "#3");
+ }
+
+ try {
+ cli.Run ("deriveddata");
+ Assert.Fail ("#4");
+ } catch (Exception ex) {
+ // The type must be explicitly listed in the [FaultContract],
+ // it is not allowed to use a subclass of the exception data type.
+ Assert.AreEqual (typeof (FaultException), ex.GetType (), "#5");
+ }
+
+ try {
+ cli.Run ("derivedexception");
+ Assert.Fail ("#6");
+ } catch (Exception ex) {
+ // However, it is allowed to derive from FaultException<T>, provided
+ // that T is explicitly listed in [FaultContract]. Bug #7177.
+ Assert.AreEqual (typeof (FaultException<PrivateAffairError>), ex.GetType (), "#7");
+ }
} finally {
host.Close ();
}
Assert.AreEqual (1, dop.FaultContractInfos.Count, "s#2");
var fci = dop.FaultContractInfos [0];
Assert.AreEqual (typeof (PrivateAffairError), fci.Detail, "s#3");
- throw new FaultException<PrivateAffairError> (new PrivateAffairError () { ErrorCode = 5, Text = "foobarerror" });
+ if (input.Equals ("default"))
+ throw new FaultException<PrivateAffairError> (new PrivateAffairError () { ErrorCode = 5, Text = "foobarerror" });
+ else if (input.Equals ("deriveddata"))
+ throw new FaultException<DerivedPrivateAffairError> (new DerivedPrivateAffairError () { ErrorCode = 5, Text = "foobarerror" });
+ else if (input.Equals ("derivedexception"))
+ throw new DerivedFaultException (new PrivateAffairError () { ErrorCode = 5, Text = "foobarerror" });
+ else
+ throw new FaultException ("Invalid operation");
}
}
[DataMember]
public string Text { get; set; }
}
+
+ [DataContract]
+ class DerivedPrivateAffairError : PrivateAffairError
+ {
+ }
+
+ class DerivedFaultException : FaultException<PrivateAffairError>
+ {
+ public DerivedFaultException (PrivateAffairError error)
+ : base (error)
+ { }
+ }
}
}
\ No newline at end of file