Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark FaultException as Serializable #4205

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace System.ServiceModel
{
[Serializable]
[KnownType(typeof(FaultCodeData))]
[KnownType(typeof(FaultCodeData[]))]
[KnownType(typeof(FaultReasonData))]
Expand Down Expand Up @@ -146,6 +147,16 @@ internal MessageFault Fault
get { return _fault; }
}

internal void AddFaultCodeObjectData(SerializationInfo info, string key, FaultCode code)
{
info.AddValue(key, FaultCodeData.GetObjectData(code));
}

internal void AddFaultReasonObjectData(SerializationInfo info, string key, FaultReason reason)
{
info.AddValue(key, FaultReasonData.GetObjectData(reason));
}

private static FaultCode CreateCode(string code)
{
return (code != null) ? new FaultCode(code) : DefaultCode;
Expand Down Expand Up @@ -188,6 +199,15 @@ private static FaultReason CreateReason(string reason)
return (reason != null) ? new FaultReason(reason) : DefaultReason;
}

public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
AddFaultCodeObjectData(info, "code", Code);
AddFaultReasonObjectData(info, "reason", Reason);
info.AddValue("messageFault", this._fault);
info.AddValue("action", this.Action);
}

private static FaultReason GetReason(MessageFault fault)
{
if (fault == null)
Expand Down Expand Up @@ -241,18 +261,19 @@ private static FaultReason EnsureReason(FaultReason reason)
return (reason != null) ? reason : DefaultReason;
}

[Serializable]
internal class FaultCodeData
{
private string _name;
private string _ns;
private string name;
private string ns;

internal static FaultCode Construct(FaultCodeData[] nodes)
{
FaultCode code = null;

for (int i = nodes.Length - 1; i >= 0; i--)
{
code = new FaultCode(nodes[i]._name, nodes[i]._ns, code);
code = new FaultCode(nodes[i].name, nodes[i].ns, code);
}

return code;
Expand All @@ -265,8 +286,8 @@ internal static FaultCodeData[] GetObjectData(FaultCode code)
for (int i = 0; i < array.Length; i++)
{
array[i] = new FaultCodeData();
array[i]._name = code.Name;
array[i]._ns = code.Namespace;
array[i].name = code.Name;
array[i].ns = code.Namespace;
code = code.SubCode;
}

Expand All @@ -291,18 +312,19 @@ private static int GetDepth(FaultCode code)
}
}

[Serializable]
internal class FaultReasonData
{
private string _xmlLang;
private string _text;
private string xmlLang;
private string text;

internal static FaultReason Construct(FaultReasonData[] nodes)
{
FaultReasonText[] reasons = new FaultReasonText[nodes.Length];

for (int i = 0; i < nodes.Length; i++)
{
reasons[i] = new FaultReasonText(nodes[i]._text, nodes[i]._xmlLang);
reasons[i] = new FaultReasonText(nodes[i].text, nodes[i].xmlLang);
}

return new FaultReason(reasons);
Expand All @@ -316,15 +338,16 @@ internal static FaultReasonData[] GetObjectData(FaultReason reason)
for (int i = 0; i < translations.Count; i++)
{
array[i] = new FaultReasonData();
array[i]._xmlLang = translations[i].XmlLang;
array[i]._text = translations[i].Text;
array[i].xmlLang = translations[i].XmlLang;
array[i].text = translations[i].Text;
}

return array;
}
}
}

[Serializable]
public class FaultException<TDetail> : FaultException
{
public FaultException(TDetail detail)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ public FaultContractAttribute(System.Type detailType) { }
public string Name { get { return default; } set { } }
public string Namespace { get { return default; } set { } }
}
[Serializable]
public partial class FaultException : System.ServiceModel.CommunicationException
{
public FaultException() { }
Expand All @@ -513,6 +514,7 @@ protected FaultException(System.Runtime.Serialization.SerializationInfo info, Sy
public static System.ServiceModel.FaultException CreateFault(System.ServiceModel.Channels.MessageFault messageFault, params System.Type[] faultDetailTypes) { return default; }
public virtual System.ServiceModel.Channels.MessageFault CreateMessageFault() { return default; }
}
[Serializable]
public partial class FaultException<TDetail> : System.ServiceModel.FaultException
{
public FaultException(TDetail detail) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// See the LICENSE file in the project root for more information.


using System.IO;
using System.Runtime.Serialization;
using System.ServiceModel;
using Infrastructure.Common;
using Xunit;
Expand Down Expand Up @@ -30,4 +32,19 @@ public static void Ctor_TDetail_FaultReason()
Assert.NotNull(exception2.Reason);
Assert.Null(exception2.Detail);
}

[WcfFact]
public static void Serializable_Default()
{
string netfxBsl = @"<FaultException xmlns=""http://schemas.datacontract.org/2004/07/System.ServiceModel"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:x=""http://www.w3.org/2001/XMLSchema""><ClassName i:type=""x:string"" xmlns="""">System.ServiceModel.FaultException</ClassName><Message i:type=""x:string"" xmlns="""">The creator of this fault did not specify a Reason.</Message><Data i:nil=""true"" xmlns=""""/><InnerException i:nil=""true"" xmlns=""""/><HelpURL i:nil=""true"" xmlns=""""/><StackTraceString i:nil=""true"" xmlns=""""/><RemoteStackTraceString i:nil=""true"" xmlns=""""/><RemoteStackIndex i:type=""x:int"" xmlns="""">0</RemoteStackIndex><ExceptionMethod i:nil=""true"" xmlns=""""/><HResult i:type=""x:int"" xmlns="""">-2146233088</HResult><Source i:nil=""true"" xmlns=""""/><WatsonBuckets i:nil=""true"" xmlns=""""/><code i:type=""a:ArrayOfFaultException.FaultCodeData"" xmlns="""" xmlns:a=""http://schemas.datacontract.org/2004/07/System.ServiceModel""><a:FaultException.FaultCodeData><a:name>Sender</a:name><a:ns/></a:FaultException.FaultCodeData></code><reason i:type=""a:ArrayOfFaultException.FaultReasonData"" xmlns="""" xmlns:a=""http://schemas.datacontract.org/2004/07/System.ServiceModel""><a:FaultException.FaultReasonData><a:text>The creator of this fault did not specify a Reason.</a:text><a:xmlLang>en-US</a:xmlLang></a:FaultException.FaultReasonData></reason><messageFault i:nil=""true"" xmlns=""""/><action i:nil=""true"" xmlns=""""/></FaultException>";
var dcs = new DataContractSerializer(typeof(FaultException));

using (var ms = new MemoryStream())
using (var sr = new StreamReader(ms))
{
dcs.WriteObject(ms, new FaultException());
ms.Position = 0;
Assert.Equal(netfxBsl, sr.ReadToEnd());
}
}
}