FaultBodyWriter.cs
6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using System;
using System.Globalization;
using System.IO;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Xml;
namespace SoapCore
{
public class FaultBodyWriter : BodyWriter
{
private const string Soap12Namespace = "http://www.w3.org/2003/05/soap-envelope";
private const string Soap11Namespace = "http://schemas.xmlsoap.org/soap/envelope/";
private readonly MessageVersion _version;
private readonly Exception _exception;
private readonly string _faultStringOverride;
public FaultBodyWriter(Exception exception, MessageVersion version, bool isBuffered = true, string faultStringOverride = null) : base(isBuffered)
{
_version = version;
_exception = exception;
_faultStringOverride = faultStringOverride;
}
protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
{
if (_version.Envelope == EnvelopeVersion.Soap12)
{
WriteSoap12Fault(writer);
}
else if (_version.Envelope == EnvelopeVersion.Soap11)
{
WriteSoap11Fault(writer);
}
else
{
// We will default to the oldest SOAP format instead of
// breaking everything
WriteSoap11Fault(writer);
}
}
private void WriteSoap12Fault(XmlDictionaryWriter writer)
{
// NOTE: This default culture is a hack until a better localisation solution is
// built. At this stage it assumes the current thread culture
var defaultCulture = CultureInfo.CurrentCulture;
var faultString = _faultStringOverride ?? (_exception.InnerException != null ? _exception.InnerException.Message : _exception.Message);
var faultDetail = ExtractFaultDetailsAsXmlElement(_exception);
var prefix = writer.LookupPrefix(Soap12Namespace) ?? "s";
writer.WriteStartElement(prefix, "Fault", Soap12Namespace);
writer.WriteStartElement(prefix, "Code", Soap12Namespace);
writer.WriteStartElement(prefix, "Value", Soap12Namespace);
writer.WriteString(prefix + ":Sender");
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteStartElement(prefix, "Reason", Soap12Namespace);
writer.WriteStartElement(prefix, "Text", Soap12Namespace);
writer.WriteAttributeString("xml:lang", defaultCulture.IetfLanguageTag);
writer.WriteString(faultString);
writer.WriteEndElement();
writer.WriteEndElement();
if (faultDetail != null)
{
writer.WriteStartElement(prefix, "Detail", Soap12Namespace);
faultDetail.WriteTo(writer);
writer.WriteEndElement();
}
writer.WriteEndElement();
}
private void WriteSoap11Fault(XmlDictionaryWriter writer)
{
var faultString = _faultStringOverride ?? (_exception.InnerException != null ? _exception.InnerException.Message : _exception.Message);
var faultDetail = ExtractFaultDetailsAsXmlElement(_exception);
writer.WriteStartElement("Fault", Soap11Namespace);
/* SUPPORT FOR SPECIFYING CUSTOM FAULTCODE AND NAMESPACE
For Example, this would result in the response below:
throw new System.ServiceModel.FaultException(new FaultReason("faultString1"), new FaultCode("faultCode1", "faultNamespace1"), "action1");
<s:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<s:Fault>
<a:faultcode xmlns:a="faultNamespace1">a:faultCode1</a:faultcode>
<faultstring>faultString1</faultstring>
</s:Fault>
</s:Body>
</s:Envelope>
For Example, this would result in the response below:
throw new System.ServiceModel.FaultException(new FaultReason("faultString1"), new FaultCode("faultCode1"), "action1");
<s:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<s:Fault>
<faultcode>s:faultCode1</faultcode>
<faultstring>faultString1</faultstring>
</s:Fault>
</s:Body>
</s:Envelope>
*/
if (_exception is FaultException)
{
var faultException = (FaultException)_exception;
if (faultException != null && faultException.Code != null && !string.IsNullOrEmpty(faultException.Code.Name))
{
if (!string.IsNullOrEmpty(faultException.Code.Namespace))
{
writer.WriteElementString("a", "faultcode", faultException.Code.Namespace, "a:" + faultException.Code.Name);
}
else
{
writer.WriteElementString("faultcode", "s:" + faultException.Code.Name);
}
}
else
{
writer.WriteElementString("faultcode", "s:Client");
}
}
else
{
writer.WriteElementString("faultcode", "s:Client");
}
writer.WriteElementString("faultstring", faultString);
if (faultDetail != null)
{
writer.WriteStartElement("detail");
faultDetail.WriteTo(writer);
writer.WriteEndElement();
}
writer.WriteEndElement();
}
private XmlElement ExtractFaultDetailsAsXmlElement(Exception ex)
{
var detailObject = ExtractFaultDetail(ex);
if (detailObject == null)
{
return null;
}
using (var ms = new MemoryStream())
{
var serializer = new DataContractSerializer(detailObject.GetType());
serializer.WriteObject(ms, detailObject);
ms.Position = 0;
var doc = new XmlDocument();
doc.Load(ms);
return doc.DocumentElement;
}
}
/// <summary>
/// Helper to extract object of a detailed fault.
/// </summary>
/// <param name="exception">
/// The exception that caused the failure.
/// </param>
/// <returns>
/// Returns instance of T if the exception (or its InnerExceptions) is of type FaultException<T>.
/// otherwise returns null
/// </returns>
private object ExtractFaultDetail(Exception exception)
{
try
{
var currentException = exception;
while (currentException != null)
{
var type = currentException.GetType();
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(FaultException<>))
{
var detailInfo = type.GetProperty("Detail");
var value = detailInfo?.GetValue(currentException);
if (value != null)
{
return value;
}
}
currentException = currentException.InnerException;
}
}
catch
{
return null;
}
return null;
}
}
}