PageRenderTime 36ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/Visual Studio 2008/CSServicedComponent/SimpleObject.cs

#
C# | 194 lines | 87 code | 29 blank | 78 comment | 3 complexity | 52b033940734cf440c7afde96d4af992 MD5 | raw file
  1. /****************************** Module Header ******************************\
  2. * Module Name: SimpleObject.cs
  3. * Project: CSServicedComponent
  4. * Copyright (c) Microsoft Corporation.
  5. *
  6. * CSServicedComponent demonstrates a serviced component written in Visua C#.
  7. * A serviced component is a class that is authored in a CLS-compliant
  8. * language and that derives directly or indirectly from the
  9. * System.EnterpriseServices.ServicedComponent class. Classes configured in
  10. * this way can be hosted in a COM+ application and can use COM+ services by
  11. * way of the EnterpriseServices namespace.
  12. *
  13. * CSServicedComponent exposes the following component:
  14. *
  15. * Program ID: CSServicedComponent.SimpleObject
  16. * CLSID_SimpleObject: 14EAD156-F55E-4d9b-A3C5-658D4BB56D30
  17. * IID_ISimpleObject: 2A59630E-4232-4582-AE47-706E2B648579
  18. * DIID_ISimpleObjectEvents: A06C000A-7A85-42dc-BA6D-BE736B6EB97A
  19. * LIBID_CSServicedComponent: 3308202E-A355-4C3D-805D-B527D1EF5FA3
  20. *
  21. * Properties:
  22. * // With both get and set accessor methods
  23. * float FloatProperty
  24. *
  25. * Methods:
  26. * // HelloWorld returns a string "HelloWorld"
  27. * string HelloWorld();
  28. * // GetProcessThreadID outputs the running process ID and thread ID
  29. * void GetProcessThreadID(out uint processId, out uint threadId);
  30. * // Transactional operation
  31. * void DoTransaction();
  32. *
  33. * Events:
  34. * // FloatPropertyChanging is fired before new value is set to the
  35. * // FloatProperty property. The Cancel parameter allows the client to cancel
  36. * // the change of FloatProperty.
  37. * void FloatPropertyChanging(float NewValue, ref bool Cancel);
  38. *
  39. * This source is subject to the Microsoft Public License.
  40. * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  41. * All other rights reserved.
  42. *
  43. * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  44. * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  45. * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  46. \***************************************************************************/
  47. #region Using directives
  48. using System;
  49. using System.Collections.Generic;
  50. using System.Text;
  51. using System.EnterpriseServices;
  52. using System.Runtime.InteropServices;
  53. #endregion
  54. namespace CSServicedComponent
  55. {
  56. #region Interfaces
  57. [Description("ISimpleObject description")]
  58. [Guid("2A59630E-4232-4582-AE47-706E2B648579")]
  59. // The public interface describing the COM interface of the coclass
  60. public interface ISimpleObject
  61. {
  62. #region Properties
  63. float FloatProperty { get; set; }
  64. #endregion
  65. #region Methods
  66. [Description("HelloWorld description")]
  67. string HelloWorld();
  68. void GetProcessThreadID(out uint processId, out uint threadId);
  69. [Description("DoTransaction description")]
  70. void DoTransaction();
  71. #endregion
  72. }
  73. [Guid("A06C000A-7A85-42dc-BA6D-BE736B6EB97A")]
  74. [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
  75. // The public interface describing the events the coclass can sink
  76. public interface ISimpleObjectEvents
  77. {
  78. #region Events
  79. [DispId(1)]
  80. void FloatPropertyChanging(float NewValue, ref bool Cancel);
  81. #endregion
  82. }
  83. #endregion
  84. // The ObjectPooling attribute is used to configure the component's object
  85. // pooling. It can enable or disables object pooling and set the min. or
  86. // max. pool size and object creation timeout.
  87. [ObjectPooling(MinPoolSize=2, MaxPoolSize=10, CreationTimeout=20)]
  88. // Creates the component with a new transaction, regardless of the state
  89. // of the current context.
  90. [Transaction(TransactionOption.Required)]
  91. [ClassInterface(ClassInterfaceType.None)]
  92. [ComSourceInterfaces(typeof(ISimpleObjectEvents))]
  93. [Guid("14EAD156-F55E-4d9b-A3C5-658D4BB56D30")]
  94. public class SimpleObject : ServicedComponent, ISimpleObject
  95. {
  96. #region Properties
  97. /// <summary>
  98. /// The private members don't make it into the type-library and are
  99. /// hidden from the COM clients.
  100. /// </summary>
  101. private float fField = 0;
  102. /// <summary>
  103. /// A public property with both get and set accessor methods.
  104. /// </summary>
  105. public float FloatProperty
  106. {
  107. get { return this.fField; }
  108. set
  109. {
  110. bool cancel = false;
  111. // Raise the event FloatPropertyChanging
  112. if (null != FloatPropertyChanging)
  113. FloatPropertyChanging(value, ref cancel);
  114. if (!cancel)
  115. this.fField = value;
  116. }
  117. }
  118. #endregion
  119. #region Methods
  120. /// <summary>
  121. /// A public method that returns a string "HelloWorld".
  122. /// </summary>
  123. /// <returns>"HelloWorld"</returns>
  124. public string HelloWorld()
  125. {
  126. return "HelloWorld";
  127. }
  128. /// <summary>
  129. /// A public method with two outputs: the current process Id and the
  130. /// current thread Id.
  131. /// </summary>
  132. /// <param name="processId">[out] The current process Id</param>
  133. /// <param name="threadId">[out] The current thread Id</param>
  134. public void GetProcessThreadID(out uint processId, out uint threadId)
  135. {
  136. processId = NativeMethod.GetCurrentProcessId();
  137. threadId = NativeMethod.GetCurrentThreadId();
  138. }
  139. public void DoTransaction()
  140. {
  141. try
  142. {
  143. // Operate on the resource managers like DBMS
  144. // ...
  145. ContextUtil.SetComplete(); // Commit
  146. }
  147. catch (Exception ex)
  148. {
  149. ContextUtil.SetAbort(); // Rollback
  150. throw ex;
  151. }
  152. }
  153. #endregion
  154. #region Events
  155. [ComVisible(false)]
  156. public delegate void FloatPropertyChangingEventHandler(float NewValue, ref bool Cancel);
  157. /// <summary>
  158. /// A public event that is fired before new value is set to the
  159. /// FloatProperty property. The Cancel parameter allows the client
  160. /// to cancel the change of FloatProperty.
  161. /// </summary>
  162. public event FloatPropertyChangingEventHandler FloatPropertyChanging;
  163. #endregion
  164. }
  165. }