2
0

WsatTransactionHeader.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Transactions
  5. {
  6. using System;
  7. using System.Diagnostics.CodeAnalysis;
  8. using System.Runtime;
  9. using System.ServiceModel.Channels;
  10. using System.Diagnostics;
  11. using System.ServiceModel;
  12. using System.Transactions;
  13. using System.Xml;
  14. using Microsoft.Transactions.Wsat.Messaging;
  15. using Microsoft.Transactions.Wsat.Protocol;
  16. using System.Security.Permissions;
  17. class WsatTransactionHeader : MessageHeader
  18. {
  19. string wsatHeaderElement;
  20. string wsatNamespace;
  21. CoordinationContext context;
  22. // The demand is not added now (in 4.5), to avoid a breaking change. To be considered in the next version.
  23. /*
  24. // We demand full trust because we call into CoordinationStrings.Version(..), which is defined in a non-APTCA assembly and does an Environment.FailFast
  25. // if the argument is invalid. It's recommended to not let partially trusted callers to bring down the process.
  26. // WSATs are not supported in partial trust, so customers should not be broken by this demand.
  27. [PermissionSet(SecurityAction.Demand, Unrestricted = true)]
  28. */
  29. public WsatTransactionHeader(CoordinationContext context, ProtocolVersion protocolVersion)
  30. {
  31. this.context = context;
  32. CoordinationStrings coordinationStrings = CoordinationStrings.Version(protocolVersion);
  33. this.wsatHeaderElement = coordinationStrings.CoordinationContext;
  34. this.wsatNamespace = coordinationStrings.Namespace;
  35. }
  36. public override bool MustUnderstand
  37. {
  38. get { return true; }
  39. }
  40. public override string Name
  41. {
  42. get { return wsatHeaderElement; }
  43. }
  44. public override string Namespace
  45. {
  46. get { return wsatNamespace; }
  47. }
  48. // The demand is not added now (in 4.5), to avoid a breaking change. To be considered in the next version.
  49. /*
  50. // We demand full trust because we call into CoordinationContext and CoordinationStrings, which are defined in a non-APTCA assembly. Also, CoordinationStrings.Version(..)
  51. // does an Environment.FailFast if the argument is invalid. It's recommended to not let partially trusted callers to bring down the process.
  52. // WSATs are not supported in partial trust, so customers should not be broken by this demand.
  53. [PermissionSet(SecurityAction.Demand, Unrestricted = true)]
  54. */
  55. public static CoordinationContext GetCoordinationContext(Message message, ProtocolVersion protocolVersion)
  56. {
  57. CoordinationStrings coordinationStrings = CoordinationStrings.Version(protocolVersion);
  58. string locWsatHeaderElement = coordinationStrings.CoordinationContext;
  59. string locWsatNamespace = coordinationStrings.Namespace;
  60. int index;
  61. try
  62. {
  63. index = message.Headers.FindHeader(locWsatHeaderElement, locWsatNamespace);
  64. }
  65. catch (MessageHeaderException e)
  66. {
  67. DiagnosticUtility.TraceHandledException(e, TraceEventType.Warning);
  68. return null;
  69. }
  70. if (index < 0)
  71. return null;
  72. CoordinationContext context;
  73. XmlDictionaryReader reader = message.Headers.GetReaderAtHeader(index);
  74. using (reader)
  75. {
  76. context = GetCoordinationContext(reader, protocolVersion);
  77. }
  78. MessageHeaderInfo header = message.Headers[index];
  79. if (!message.Headers.UnderstoodHeaders.Contains(header))
  80. {
  81. message.Headers.UnderstoodHeaders.Add(header);
  82. }
  83. return context;
  84. }
  85. // The demand is not added now (in 4.5), to avoid a breaking change. To be considered in the next version.
  86. /*
  87. [PermissionSet(SecurityAction.Demand, Unrestricted = true)] // because we call into CoordinationContext, which is defined in a non-APTCA assembly; WSATs are not supported in partial trust, so customers should not be broken by this demand
  88. */
  89. protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion)
  90. {
  91. this.context.WriteContent(writer);
  92. }
  93. // The demand is not added now (in 4.5), to avoid a breaking change. To be considered in the next version.
  94. /*
  95. // We demand full trust because we call into CoordinationXmlDictionaryStrings.Version(..), which is defined in a non-APTCA assembly and does an Environment.FailFast
  96. // if the argument is invalid. It's recommended to not let partially trusted callers to bring down the process.
  97. // WSATs are not supported in partial trust, so customers should not be broken by this demand.
  98. [PermissionSet(SecurityAction.Demand, Unrestricted = true)]
  99. */
  100. public static CoordinationContext GetCoordinationContext(XmlDictionaryReader reader, ProtocolVersion protocolVersion)
  101. {
  102. CoordinationXmlDictionaryStrings coordinationXmlDictionaryStrings =
  103. CoordinationXmlDictionaryStrings.Version(protocolVersion);
  104. try
  105. {
  106. return CoordinationContext.ReadFrom(reader,
  107. coordinationXmlDictionaryStrings.CoordinationContext,
  108. coordinationXmlDictionaryStrings.Namespace,
  109. protocolVersion);
  110. }
  111. catch (InvalidCoordinationContextException e)
  112. {
  113. DiagnosticUtility.TraceHandledException(e, TraceEventType.Error);
  114. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TransactionException(SR.GetString(SR.WsatHeaderCorrupt), e));
  115. }
  116. }
  117. }
  118. }