PageRenderTime 36ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Branches/Upgrade2008/TfsDeployer/Readify.Useful.TeamFoundation.Common/ServiceHelper.cs

http://tfsdeployer.codeplex.com
C# | 174 lines | 105 code | 30 blank | 39 comment | 1 complexity | d63a6e6f850f2f758e0870c28a093417 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Microsoft.TeamFoundation.Server;
  5. using Microsoft.TeamFoundation.Client;
  6. using Readify.Useful.TeamFoundation.Common;
  7. using System.Net;
  8. using System.IO;
  9. using System.Xml.Serialization;
  10. using System.Diagnostics;
  11. using System.Collections.ObjectModel;
  12. using Readify.Useful.TeamFoundation.Common.Properties;
  13. namespace Readify.Useful.TeamFoundation.Common
  14. {
  15. /// <summary>
  16. /// This class contains utilities for in the communication
  17. /// with a Team Foundation Server
  18. /// </summary>
  19. public static class ServiceHelper
  20. {
  21. #region Get Service
  22. /// <summary>
  23. /// Get a Service from Team Foundation Server
  24. /// </summary>
  25. /// <typeparam name="T">The type of service to retrieve</typeparam>
  26. /// <param name="userName">The user name to connect the server with</param>
  27. /// <param name="password">the password to for the user</param>
  28. /// <returns>The service requested or an expection is thrown</returns>
  29. public static T GetService<T>(string userName, string password, string domain)
  30. {
  31. TraceHelper.TraceVerbose(Constants.CommonSwitch, "UserName: {0}", userName);
  32. TraceHelper.TraceVerbose(Constants.CommonSwitch, "Password: {0}", password);
  33. TraceHelper.TraceVerbose(Constants.CommonSwitch, "Domain: {0}", domain);
  34. NetworkCredential credential = new NetworkCredential(userName, password, domain);
  35. return GetService<T>(credential);
  36. }
  37. /// <summary>
  38. /// Get a Service from Team Foundation Server using the Default credientials,
  39. /// </summary>
  40. /// <typeparam name="T"></typeparam>
  41. /// <returns></returns>
  42. public static T GetService<T>()
  43. {
  44. if (Settings.Default.UseDefaultCredentials)
  45. {
  46. return GetService<T>(CredentialCache.DefaultCredentials);
  47. }
  48. else
  49. {
  50. return GetService<T>(Settings.Default.UserName, Settings.Default.Password, Settings.Default.Domain);
  51. }
  52. }
  53. /// <summary>
  54. /// Get the server that we are connecting to
  55. /// </summary>
  56. public static Uri TeamFoundationServerUrl
  57. {
  58. get
  59. {
  60. return new Uri(Settings.Default.TeamFoundationServerUrl);
  61. }
  62. }
  63. /// <summary>
  64. /// Internal helper method to get the service
  65. /// </summary>
  66. /// <typeparam name="T">Type of service to retrieve</typeparam>
  67. /// <param name="credentials">Credentials to use when connecting the service</param>
  68. /// <returns></returns>
  69. private static T GetService<T>(ICredentials credentials)
  70. {
  71. TempCredentials creds = new TempCredentials(credentials);
  72. string teamFoundationServerUrl = Settings.Default.TeamFoundationServerUrl;
  73. TraceHelper.TraceVerbose(Constants.CommonSwitch, "Connecting to server {0} To get service {1} ", teamFoundationServerUrl, typeof(T).ToString());
  74. TeamFoundationServer server = TeamFoundationServerFactory.GetServer(teamFoundationServerUrl, creds);
  75. T service = (T)server.GetService(typeof(T));
  76. return service;
  77. }
  78. private class TempCredentials : ICredentialsProvider
  79. {
  80. private ICredentials _credentials;
  81. public TempCredentials(ICredentials credentials)
  82. {
  83. _credentials = credentials;
  84. }
  85. #region ICredentialsProvider Members
  86. public ICredentials GetCredentials(Uri uri, ICredentials failedCredentials)
  87. {
  88. return _credentials;
  89. }
  90. public void NotifyCredentialsAuthenticated(Uri uri)
  91. {
  92. }
  93. #endregion
  94. }
  95. #endregion
  96. public static int Subscribe(string userName, TeamFoundationServerEvents eventToSubscribeTo,string filterExpression, DeliveryPreference preference)
  97. {
  98. return Subscribe(userName, eventToSubscribeTo.ToString(), filterExpression, preference);
  99. }
  100. public static int Subscribe(string userName, string eventToSubscribeTo, string filterExpression, DeliveryPreference preference)
  101. {
  102. TraceHelper.TraceVerbose(Constants.CommonSwitch, "Subscribing to {0} using UserName:{1} FilterExpression:{2}", eventToSubscribeTo, userName, filterExpression);
  103. IEventService service = GetService<IEventService>();
  104. return service.SubscribeEvent(userName, eventToSubscribeTo, filterExpression, preference);
  105. }
  106. /// <summary>
  107. /// Unsubscribe from this event.
  108. /// </summary>
  109. /// <param name="subscriptionID"></param>
  110. public static void Unsubscribe(int subscriptionId)
  111. {
  112. IEventService service = GetService<IEventService>();
  113. TraceHelper.TraceVerbose(Constants.CommonSwitch,"Unsubsribing event with id {0}",subscriptionId);
  114. service.UnsubscribeEvent(subscriptionId);
  115. }
  116. /// <summary>
  117. /// Unsubscribe a number of subscriptions
  118. /// </summary>
  119. /// <param name="subscriptionIds"></param>
  120. public static void Unsubscribe(Collection<int> subscriptionIds)
  121. {
  122. foreach (int subscriptionId in subscriptionIds)
  123. {
  124. Unsubscribe(subscriptionId);
  125. }
  126. }
  127. /// <summary>
  128. /// This method deserializes and event that has been received into a given type
  129. /// </summary>
  130. /// <typeparam name="T"></typeparam>
  131. /// <param name="eventXml"></param>
  132. /// <returns></returns>
  133. public static T DeserializeEvent<T>(string eventXml)
  134. {
  135. TraceHelper.TraceVerbose(Constants.CommonSwitch,"Deserializing Event of type {0} from XML:", typeof(T).ToString());
  136. TraceHelper.TraceVerbose(Constants.CommonSwitch, eventXml);
  137. XmlSerializer serializer = new XmlSerializer(typeof(T));
  138. using (TextReader reader = new StringReader(eventXml))
  139. {
  140. return (T)serializer.Deserialize(reader);
  141. }
  142. }
  143. }
  144. }