PageRenderTime 6206ms CodeModel.GetById 22ms RepoModel.GetById 2ms app.codeStats 0ms

/Trident/Product/DataLayer/Services/DataLayerBroker/DataLayerBroker.svc.cs

#
C# | 289 lines | 219 code | 45 blank | 25 comment | 7 complexity | b87a1fe4c83187714ca721f05cbe0883 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. //*********************************************************
  2. //
  3. // Copyright (c) Microsoft. All rights reserved.
  4. // This code is licensed under the Apache License, Version 2.0.
  5. // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
  6. // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
  7. // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
  8. // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
  9. //
  10. //*********************************************************
  11. using System;
  12. using System.Data.Common;
  13. using System.Collections.Generic;
  14. using System.Linq;
  15. using System.Runtime.Serialization;
  16. using System.ServiceModel;
  17. using System.ServiceModel.Activation;
  18. using System.Web;
  19. using SR = Microsoft.Research.DataLayer;
  20. using System.Runtime.Remoting.Contexts;
  21. namespace DataLayerBroker
  22. {
  23. [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
  24. //[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
  25. [ServiceBehavior]
  26. public class DLBrokerService : IDataLayerBroker
  27. {
  28. private SR.Connection conn;
  29. private Storage objType;
  30. private const string PROTOCOL_SESSION_NAME = "Protocol";
  31. static DLBrokerService()
  32. {
  33. SR.SR_Connection.Init();
  34. }
  35. public DLBrokerService()
  36. {
  37. //SR.ConnectionManager.AlternateProviderLocation = @"E:\src\msr\sd\trident\4\Trident\Research\DataLayer\Services\DataLayerBroker\bin";
  38. SR.ConnectionManager.AlternateProviderLocation = System.Web.HttpRuntime.BinDirectory;
  39. SR.ConnectionManager mgr = SR.ConnectionManager.CreateForAgent(
  40. SR.ConnectionManager.CancelBehavior.ThrowException,
  41. SR.ConnectionManager.Agent.WebService);
  42. conn = mgr.PickConnection(SR.ConnectionManagerBase.ConnectionUI.NeverShowUI);
  43. }
  44. /// <summary>
  45. /// Gets or sets the serialization protocol.
  46. /// </summary>
  47. /// <value>The serialization protocol.</value>
  48. private SerializationProtocol SerializationProtocol
  49. {
  50. get
  51. {
  52. if (HttpContext.Current.Session[PROTOCOL_SESSION_NAME] != null)
  53. {
  54. return (SerializationProtocol)HttpContext.Current.Session[PROTOCOL_SESSION_NAME];
  55. }
  56. else
  57. {
  58. return SerializationProtocol.SOAP;
  59. }
  60. }
  61. set
  62. {
  63. HttpContext.Current.Session[PROTOCOL_SESSION_NAME] = value;
  64. }
  65. }
  66. public void Connect()
  67. {
  68. }
  69. /// <summary>
  70. /// Sets the serialization protocol.
  71. /// </summary>
  72. /// <param name="protocol">The protocol.</param>
  73. public void SetSerializationProtocol(SerializationProtocol protocol)
  74. {
  75. SerializationProtocol = protocol;
  76. }
  77. public void Object(Storage obj, SR.Connection.Action action, ref List<Parameter> paramList)
  78. {
  79. List<SR.Parameter> paramList2 = new List<SR.Parameter>();
  80. objType = obj;
  81. // Load parameters into list
  82. foreach (Parameter p in paramList)
  83. {
  84. p.protocol = this.SerializationProtocol;
  85. SR.Parameter inParam = p.FromNetwork();
  86. paramList2.Add(inParam);
  87. }
  88. obj.protocol = this.SerializationProtocol;
  89. SR.Connection.Storage objSR = obj.FromNetwork();
  90. // Call registry service
  91. conn.Object(objSR, action, ref paramList2);
  92. // Marshal results back to client
  93. List<Parameter> results = new List<Parameter>(paramList);
  94. paramList.Clear();
  95. foreach (SR.Parameter p in paramList2)
  96. {
  97. paramList.Add(Parameter.ToNetwork(p, this.SerializationProtocol));
  98. }
  99. }
  100. public void Object(Storage obj, SR.Connection.Action action, ref List<Parameter> paramList,
  101. out List<XferObjectInstance> results)
  102. {
  103. List<SR.Parameter> paramList2 = new List<SR.Parameter>();
  104. objType = obj;
  105. // Load parameters into list
  106. if (action == Microsoft.Research.DataLayer.Connection.Action.Search)
  107. {
  108. paramList[0].protocol = this.SerializationProtocol;
  109. paramList2.Add(paramList[0].FromNetworkSearch(conn));
  110. }
  111. else
  112. {
  113. foreach (Parameter p in paramList)
  114. {
  115. p.protocol = this.SerializationProtocol;
  116. paramList2.Add(p.FromNetwork());
  117. }
  118. }
  119. List<SR.IObject> results2 = new List<SR.IObject>();
  120. obj.protocol = this.SerializationProtocol;
  121. conn.Object(obj.FromNetwork(), action, ref paramList2, CreateTransferObject, out results2);
  122. results = new List<XferObjectInstance>();
  123. results.AddRange(Array.ConvertAll<SR.IObject, XferObjectInstance>(results2.ToArray(),
  124. new Converter<Microsoft.Research.DataLayer.IObject, XferObjectInstance>(delegate(SR.IObject src)
  125. {
  126. return (XferObjectInstance)src;
  127. }
  128. )));
  129. }
  130. public void Relation(SR.Connection.Action action, XferObjectReference obj1ref, XferObjectReference obj2ref)
  131. {
  132. obj1ref.protocol = this.SerializationProtocol;
  133. obj2ref.protocol = this.SerializationProtocol;
  134. SR.IObject obj1 = obj1ref.Create(conn);
  135. SR.IObject obj2 = obj2ref.Create(conn);
  136. conn.Relation(action, obj1, obj2);
  137. }
  138. public void Relation(Microsoft.Research.DataLayer.Connection.Action action,
  139. XferObjectReference obj1Ref, XferExtraRelDataReference objRelRef)
  140. {
  141. obj1Ref.protocol = this.SerializationProtocol;
  142. SR.IObject obj1 = obj1Ref.Create(conn);
  143. objRelRef.protocol = this.SerializationProtocol;
  144. SR.IObject obj2 = objRelRef.Create(conn);
  145. conn.Relation(action, obj1, obj2);
  146. }
  147. public void Relation(SR.Connection.Action action, XferObjectReference obj1, string field, Storage type2, out List<XferObjectInstance> results)
  148. {
  149. obj1.protocol = this.SerializationProtocol;
  150. type2.protocol = this.SerializationProtocol;
  151. SR.IObject refObj = obj1.Create(conn);
  152. List<SR.IObject> results2 = new List<SR.IObject>();
  153. conn.Relation(action, refObj, field, type2.FromNetwork(), CreateTransferRelation, out results2);
  154. results = new List<XferObjectInstance>();
  155. results.AddRange(Array.ConvertAll<SR.IObject, XferObjectInstance>(results2.ToArray(),
  156. new Converter<Microsoft.Research.DataLayer.IObject, XferObjectInstance>(delegate(SR.IObject src)
  157. {
  158. // CreateTransferObject creates XferObjectInstance objects. Safe to cast.
  159. return (XferObjectInstance)src;
  160. }
  161. )));
  162. }
  163. public void Relation(SR.Connection.Action action, XferObjectReference obj1, Storage obj2, string revKey, ref List<Parameter> paramList)
  164. {
  165. obj1.protocol = this.SerializationProtocol;
  166. obj2.protocol = this.SerializationProtocol;
  167. SR.IObject refObj = obj1.Create(conn);
  168. List<SR.Parameter> paramList2 = new List<SR.Parameter>();
  169. foreach (Parameter p in paramList)
  170. {
  171. p.protocol = this.SerializationProtocol;
  172. paramList2.Add(p.FromNetwork());
  173. }
  174. conn.Relation(action, refObj, obj2.FromNetwork(), revKey, ref paramList2);
  175. }
  176. private SR.IObject CreateTransferRelation(DbDataReader input, SR.Connection c)
  177. {
  178. XferObjectInstance ret = new XferObjectInstance();
  179. ret.ID = Guid.Empty;
  180. ret.Type = objType;
  181. ret.Fields = new List<Parameter>();
  182. for (int i = 0; i < input.FieldCount; i++)
  183. {
  184. string name = input.GetName(i);
  185. Type fieldType = input.GetFieldType(i);
  186. SR.Parameter.ParamType paramType;
  187. paramType = SR.Connection.ClrTridentTypeMap[fieldType];
  188. object paramValue = input.GetValue(i);
  189. SR.Parameter param = new SR.Parameter(name, paramType, SR.Parameter.ParamDirection.Unknown, paramValue);
  190. Parameter xferParam = Parameter.ToNetwork(param, this.SerializationProtocol);
  191. ret.Fields.Add(xferParam);
  192. }
  193. return ret;
  194. }
  195. private SR.IObject CreateTransferObject(DbDataReader input, SR.Connection c)
  196. {
  197. XferObjectInstance ret = new XferObjectInstance();
  198. ret.ID = input.GetGuid(input.GetOrdinal("ID"));
  199. ret.Type = objType;
  200. ret.Fields = new List<Parameter>();
  201. for (int i = 0; i < input.FieldCount; i++)
  202. {
  203. string name = input.GetName(i);
  204. Type fieldType = input.GetFieldType(i);
  205. SR.Parameter.ParamType paramType;
  206. paramType = SR.Connection.ClrTridentTypeMap[fieldType];
  207. object paramValue = input.GetValue(i);
  208. SR.Parameter param = new SR.Parameter(name, paramType, SR.Parameter.ParamDirection.Unknown, paramValue);
  209. Parameter xferParam = Parameter.ToNetwork(param, this.SerializationProtocol);
  210. ret.Fields.Add(xferParam);
  211. }
  212. return ret;
  213. }
  214. private delegate void ConnAtomCallback(SR.IConnectionAtomicity atom);
  215. private void ExecConnectionAtomicity(ConnAtomCallback callback)
  216. {
  217. if (conn is SR.IConnectionAtomicity)
  218. {
  219. callback((SR.IConnectionAtomicity)conn);
  220. }
  221. }
  222. public void ConnAtom_BeginEdit()
  223. {
  224. ExecConnectionAtomicity(new ConnAtomCallback(delegate(SR.IConnectionAtomicity atom)
  225. {
  226. atom.BeginEdit();
  227. }));
  228. }
  229. public void ConnAtom_EndEdit()
  230. {
  231. ExecConnectionAtomicity(new ConnAtomCallback(delegate(SR.IConnectionAtomicity atom)
  232. {
  233. atom.EndEdit();
  234. }));
  235. }
  236. public void ConnAtom_CancelEdit()
  237. {
  238. ExecConnectionAtomicity(new ConnAtomCallback(delegate(SR.IConnectionAtomicity atom)
  239. {
  240. atom.CancelEdit();
  241. }));
  242. }
  243. }
  244. }