We can see from the definition of faultcontractattribute that the attribute can be applied multiple times on the same target object (AllowMultiple = True). This is also very good to understand: for the same service operation, there may be different exception scenarios, in different cases, you need to throw different exceptions.
1: [AttributeUsage (AttributeTargets.Method, AllowMultiple = true, inherited = False)]
2:public Sealed class Faultcontractattribute:attribute
3: {
4: //ellipsis member
5:}
However, if you have applied more Faultcontractattribute features to the same method of operation, you need to follow a series of rules, and we will now introduce them one by one.
Declare the same error detail type one or more times
For example, in the following code, for Operation divide, the same error detail type calculationerror is set two times with the Faultcontractattribute attribute.
1:using System.ServiceModel;
2:namespace Artech.WcfServices.Contracts
3: {
4: [ServiceContract (Namespace = "http://www.artech.com/")]
5: Public interface ICalculator
6: {
7: [OperationContract]
8: [Faultcontract (typeof (Calculationerror))]
9: [Faultcontract (typeof (Calculationerror))]
int Divide (int x, int y);
One: }
12:}
WCF service-side framework when initializing ServiceHost and creating service representations (about service descriptions and the creation of service descriptions during service boarding, the 7th chapter of WCF Profiling (Volume 1) is described in detail), Throws the InvalidOperationException exception as shown in Figure 1.
Figure 1 The exception that is caused by declaring the same error detail type multiple times
However, if you specify a different name or namespace while applying the Faultcontractattribute attribute to specify the same error detail type, this is allowed. For example, in the following code, in two Faultcontractattribute attributes, the same error detail type is specified Calculationerror, as we specify a different name, there will be no such exception at the time of the boarding service.
1:using System.ServiceModel;
2:namespace Artech.WcfServices.Contracts
3: {
4: [ServiceContract (Namespace = "http://www.artech.com/")]
5: Public interface ICalculator
6: {
7: [OperationContract]
8: [Faultcontract (typeof (Calculationerror), Name = "Calculationerror")]
9: [Faultcontract (typeof (Calculationerror), Name = "calculationexception")]
int Divide (int x, int y);
One: }
12:}
Two, several times declare different has the same valid name error detail type
The types of error types that are declared multiple times are different, but if we give them the same name and namespace we can call the combination of name and namespace the valid name Qn:qualified name), this is still not allowed. For example, in the following code, two different error detail types (Calculationerror and calculationexception) are specified for the divide operation through the Faultcontractattribute attribute. But the name of the setting is the same (Calculationerror).
1:using System.ServiceModel;
2:namespace Artech.WcfServices.Contracts
3: {
4: [ServiceContract (Namespace = "http://www.artech.com/")]
5: Public interface ICalculator
6: {
7: [OperationContract]
8:
9:name = "Calculationerror", Namespace = "http://www.artech.com/")]
10:
11:name = "Calculationerror", Namespace = "http://www.artech.com/")]
: int Divide (int x, int y);
: }
14:}
In this case, when the service is boarding, it will still throw a Invalidoperationexcepiton exception as above, as shown in Figure 2:
Figure 2 Multiple declarations with the same valid name cause the exception