1 //------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //------------------------------------------------------------
4 
5 namespace System.Runtime.DurableInstancing
6 {
7     using System.Diagnostics.CodeAnalysis;
8     using System.Runtime.Serialization;
9     using System.Security;
10     using System.Xml.Linq;
11 
12     [Serializable]
13     public class InstanceKeyCompleteException : InstancePersistenceCommandException
14     {
15         const string InstanceKeyName = "instancePersistenceInstanceKey";
16 
InstanceKeyCompleteException()17         public InstanceKeyCompleteException()
18             : this(SRCore.KeyNotReadyDefault, null)
19         {
20         }
21 
InstanceKeyCompleteException(string message)22         public InstanceKeyCompleteException(string message)
23             : this(message, null)
24         {
25         }
26 
InstanceKeyCompleteException(string message, Exception innerException)27         public InstanceKeyCompleteException(string message, Exception innerException)
28             : base(message, innerException)
29         {
30         }
31 
InstanceKeyCompleteException(XName commandName, InstanceKey instanceKey)32         public InstanceKeyCompleteException(XName commandName, InstanceKey instanceKey)
33             : this(commandName, instanceKey, null)
34         {
35         }
36 
InstanceKeyCompleteException(XName commandName, InstanceKey instanceKey, Exception innerException)37         public InstanceKeyCompleteException(XName commandName, InstanceKey instanceKey, Exception innerException)
38             : this(commandName, Guid.Empty, instanceKey, ToMessage(instanceKey), innerException)
39         {
40         }
41 
InstanceKeyCompleteException(XName commandName, Guid instanceId, InstanceKey instanceKey, string message, Exception innerException)42         public InstanceKeyCompleteException(XName commandName, Guid instanceId, InstanceKey instanceKey, string message, Exception innerException)
43             : base(commandName, instanceId, message, innerException)
44         {
45             InstanceKey = instanceKey;
46         }
47 
48         [SecurityCritical]
InstanceKeyCompleteException(SerializationInfo info, StreamingContext context)49         protected InstanceKeyCompleteException(SerializationInfo info, StreamingContext context)
50             : base(info, context)
51         {
52             Guid guid = (Guid)info.GetValue(InstanceKeyName, typeof(Guid));
53             InstanceKey = guid == Guid.Empty ? null : new InstanceKey(guid);
54         }
55 
56         public InstanceKey InstanceKey { get; private set; }
57 
58         [Fx.Tag.SecurityNote(Critical = "Overrides critical inherited method")]
59         [SecurityCritical]
60         [SuppressMessage(FxCop.Category.Security, FxCop.Rule.SecureGetObjectDataOverrides,
61             Justification = "Method is SecurityCritical")]
GetObjectData(SerializationInfo info, StreamingContext context)62         public override void GetObjectData(SerializationInfo info, StreamingContext context)
63         {
64             base.GetObjectData(info, context);
65             info.AddValue(InstanceKeyName, (InstanceKey != null && InstanceKey.IsValid) ? InstanceKey.Value : Guid.Empty, typeof(Guid));
66         }
67 
ToMessage(InstanceKey instanceKey)68         static string ToMessage(InstanceKey instanceKey)
69         {
70             if (instanceKey != null && instanceKey.IsValid)
71             {
72                 return SRCore.KeyCompleteSpecific(instanceKey.Value);
73             }
74             return SRCore.KeyCompleteDefault;
75         }
76     }
77 }
78