WordPress.com



namespace ServiceApplication{ using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Configuration; using System.Security; using System.ServiceModel; using System.ServiceModel.Channels; using System.ServiceModel.Configuration; using Microsoft.SharePoint; using Microsoft.SharePoint.Administration; using Microsoft.SharePoint.Utilities; /// <summary> /// The <see cref="T:SPIisWebServiceApplicationProxy"/> is reponsible for all communication between the SharePoint WFE and /// Service Applications running on the Application Servers. /// The Service Application Proxy is also responsible for creating the channels to talk to the Service Applications /// </summary> [IisWebServiceApplicationProxyBackupBehavior] [System.Runtime.InteropServices.Guid("D7D685EE-B67C-4963-AC6A-2B149DDE9B58")] public class WebServiceApplicationProxy : SPIisWebServiceApplicationProxy { #region Fields private const int MaxChannelCacheSize = 5; private static TimeSpan defaultOperationTimeout; private Queue<IService> channelCache; private ChannelFactory<IService> channelFactory; private object channelFactoryLock = new object(); private string configurationName; /// <summary> /// A persisted instance of <see cref="T:SPServiceLoadBalancer"/> /// </summary> [Persisted] private SPServiceLoadBalancer loadBalancer; /// <summary> /// /// </summary> private long ticksSinceLastCheck; #endregion Fields #region Constructors /// <summary> /// Initializes the <see cref="WebServiceApplicationProxy"/> class. /// </summary> static WebServiceApplicationProxy() { defaultOperationTimeout = TimeSpan.MinValue; } /// <summary> /// Initializes a new instance of the <see cref="WebServiceApplicationProxy"/> class. /// </summary> public WebServiceApplicationProxy() { this.ticksSinceLastCheck = DateTime.Now.Ticks; this.channelCache = new Queue<IService>(); this.channelFactoryLock = new object(); } /// <summary> /// Initializes a new instance of the <see cref="WebServiceApplicationProxy"/> class. /// </summary> /// <param name="name">The name.</param> /// <param name="serviceProxy">The service proxy.</param> /// <param name="serviceApplicationAddress">The service application address.</param> internal WebServiceApplicationProxy( string name, WebServiceProxy serviceProxy, Uri serviceApplicationAddress) : base(name, serviceProxy, serviceApplicationAddress) { this.loadBalancer = new SPRoundRobinServiceLoadBalancer(serviceApplicationAddress); this.ticksSinceLastCheck = DateTime.Now.Ticks; this.channelCache = new Queue<IService>(); this.channelFactoryLock = new object(); } #endregion Constructors #region Delegates /// <summary> /// A wrapper around the code to execute /// </summary> /// <typeparam name="T"></typeparam> /// <param name="application">The application.</param> internal delegate void CodeToRun<T>(T application); #endregion Delegates #region Properties /// <summary> /// Gets the Application Proxy Manage link /// </summary> /// <returns>Returns <see cref="T:Microsoft.SharePoint.Administration.SPAdministrationLink"/>.</returns> public override SPAdministrationLink ManageLink { get { if (!string.IsNullOrEmpty(Constants.ProxyApplicationManageLink)) { return new SPAdministrationLink(Constants.ProxyApplicationManageLink); } return base.ManageLink; } } /// <summary> /// Gets the Application Proxy Administration link /// </summary> /// <returns>Returns <see cref="T:Microsoft.SharePoint.Administration.SPAdministrationLink"/>.</returns> public override SPAdministrationLink PropertiesLink { get { if (!string.IsNullOrEmpty(Constants.ProxyApplicationPropertiesLink)) { return new SPAdministrationLink(Constants.ProxyApplicationPropertiesLink); } return base.PropertiesLink; } } /// <summary> /// Gets the display name that describes the object type in the administrative user interface. /// </summary> /// <returns>A string that contains the name of the object type.</returns> public override string TypeName { get { return Constants.ProxyApplicationName; } } #endregion Properties #region Methods public void AddListEntries() { this.RunOnChannel(delegate(IService serviceApplication) { serviceApplication.AddListEntries(); }); } public bool AddAuditEntries(IEnumerable<Guid> sitesId, DateTime startDate) { bool updated = false; this.RunOnChannel(delegate(IService serviceApplication) { updated = serviceApplication.AddAuditEntries(sitesId, startDate); }); return updated; } public void AddDummyAuditEntries(System.Collections.Generic.IEnumerable<Guid> sitesId, DateTime startDate) { this.RunOnChannel(delegate(IService serviceApplication) { serviceApplication.AddDummyAuditEntries(sitesId, startDate); }); } public void ClearAllSitesAuditLogs(List<Guid> sitesId) { this.RunOnChannel(delegate(IService serviceApplication) { serviceApplication.ClearAllSitesAuditLogs(sitesId); }); } public void DeleteDatabaseEntries(List<Guid> sitesId) { this.RunOnChannel(delegate(IService serviceApplication) { serviceApplication.DeleteDatabaseEntries(sitesId); }); } public void DeleteListDatabaseEntries() { this.RunOnChannel(delegate(IService serviceApplication) { serviceApplication.DeleteListDatabaseEntries(); }); } public Collection<Entities.AuditEntry> GetAllEntries() { Collection<Entities.AuditEntry> entries = null; this.RunOnChannel(delegate(IService serviceApplication) { entries = serviceApplication.GetAllEntries(); }); return entries; } public Collection<Entities.ListEntry> GetAllListEntries() { Collection<Entities.ListEntry> entries = null; this.RunOnChannel(delegate(IService serviceApplication) { entries = serviceApplication.GetAllListEntries(); }); return entries; } public Collection<Entities.AuditEntry> GetEntriesById(Guid siteId) { Collection<Entities.AuditEntry> entries = null; this.RunOnChannel(delegate(IService serviceApplication) { entries = serviceApplication.GetEntriesById(siteId); }); return entries; } public Collection<Entities.ListEntry> GetEntriesById() { Collection<Entities.ListEntry> entries = null; this.RunOnChannel(delegate(IService serviceApplication) { entries = serviceApplication.GetAllListEntries(); }); return entries; } public override void Provision() { this.loadBalancer.Provision(); base.Provision(); this.Update(); } public override void Unprovision(bool deleteData) { this.loadBalancer.Unprovision(); base.Unprovision(deleteData); this.Update(); } internal static WebServiceApplicationProxy CreateProxy(Uri serviceApplicationUri, string proxyName, bool isInDefaultAssociationGroup) { WebServiceApplicationProxy serviceApplicationProxy = SPFarm.Local.Services.GetValue<WebService>().CreateProxy(proxyName, serviceApplicationUri) as WebServiceApplicationProxy; serviceApplicationProxy.Update(true); if (isInDefaultAssociationGroup) { SPServiceApplicationProxyGroup group = SPServiceApplicationProxyGroup.Default; group.Add(serviceApplicationProxy); group.Update(); } serviceApplicationProxy.Provision(); return serviceApplicationProxy; } internal void RunOnChannel<T>(CodeToRun<T> codeToRun, double operationTimeoutFactor) { using (new SPMonitoredScope("Service Application Example")) { SPServiceLoadBalancerContext context = null; bool flag = true; int num = 0; while (flag && num < 10) { num++; try { context = this.loadBalancer.BeginOperation(); bool cachedChannel = false; IChannel channel = (IChannel)this.GetChannel(context.EndpointAddress, out cachedChannel); TimeSpan localDefaultOperationTimeout = defaultOperationTimeout; if (channel != null) { if (operationTimeoutFactor != 1) { localDefaultOperationTimeout = new TimeSpan(Convert.ToInt64(Math.Ceiling((double)defaultOperationTimeout.Ticks * operationTimeoutFactor))); } ((IContextChannel)channel).OperationTimeout = localDefaultOperationTimeout; } if (!cachedChannel) { flag = false; } try { codeToRun((T)channel); flag = false; this.PutChannel(channel); channel = null; } catch (Exception) { //// TODO:Catch more specific exceptions here throw; } finally { if (channel != null && channel.State != CommunicationState.Closed) { channel.Abort(); } } continue; } finally { if (context != null) { context.EndOperation(); } } } if (num == 10) { //// Log message } } } internal void RunOnChannel<T>(CodeToRun<T> codeToRun) { this.RunOnChannel(codeToRun, 1); } private static string GetEndpointConfigurationName(Uri endPointAddress) { if (endPointAddress == null) { throw new ArgumentNullException("endPointAddress"); } string configurationName; if (endPointAddress.Scheme == Uri.UriSchemeHttps) { configurationName = "https"; } else if (endPointAddress.Scheme == Uri.UriSchemeHttp) { configurationName = "http"; } else { throw new NotSupportedException("Unsupported endpoint address"); } return configurationName; } /// <summary> /// Creates the channel factory. /// </summary> /// <typeparam name="T">An instance defining the service contract</typeparam> /// <param name="endpointConfigurationName">Name of the endpoint configuration.</param> /// <returns>Returns a <see cref="T:ChannelFactory"/> instance</returns> private ChannelFactory<T> CreateChannelFactory<T>(string endpointConfigurationName) { string clientConfigurationPath = SPUtility.GetGenericSetupPath(Constants.WebClientServicePath); Configuration clientConfiguration = this.OpenClientConfiguration(clientConfigurationPath); ConfigurationChannelFactory<T> factory = new ConfigurationChannelFactory<T>(endpointConfigurationName, clientConfiguration, null); factory.ConfigureCredentials(SPServiceAuthenticationMode.Claims); return factory; } /// <summary> /// Gets the channel. /// </summary> /// <param name="address">The address of the servic application</param> /// <param name="cachedChannel">if set to <c>true</c> [cached channel].</param> /// <returns></returns> private IService GetChannel(Uri address, out bool cachedChannel) { IService application = null; cachedChannel = false; EndpointAddress endPointAddress = new EndpointAddress(address, new AddressHeader[0]); string endpointConfigurationName = GetEndpointConfigurationName(address); if (this.channelFactory == null || endpointConfigurationName != this.configurationName) { lock (this.channelFactoryLock) { if (this.channelFactory == null || endpointConfigurationName != this.configurationName) { if (this.channelFactory != null) { using (this.channelFactory) { } } this.channelFactory = this.CreateChannelFactory<IService>(endpointConfigurationName); } } } lock (this.channelCache) { while (this.channelCache.Count > 0) { application = this.channelCache.Dequeue(); IChannel channel = application as IChannel; if (channel != null) { if (channel.State == CommunicationState.Opened) { cachedChannel = true; } try { channel.Close(); } catch (Exception) { throw; } application = null; } } } if (application == null) { application = this.channelFactory.CreateChannelAsProcess<IService>(endPointAddress); if (TimeSpan.MinValue == defaultOperationTimeout) { defaultOperationTimeout = ((IContextChannel)application).OperationTimeout; } } return application; } /// <summary> /// Gets the channel. /// </summary> /// <param name="address">The address.</param> /// <returns></returns> private IService GetChannel(Uri address) { string endpointConfigurationName = GetEndpointConfigurationName(address); if (this.channelFactory == null || endpointConfigurationName != this.configurationName) { lock (this.channelFactoryLock) { if (this.channelFactory == null || endpointConfigurationName != this.configurationName) { this.channelFactory = CreateChannelFactory<IService>(endpointConfigurationName); this.configurationName = endpointConfigurationName; } } } return this.channelFactory.CreateChannelActingAsLoggedOnUser<IService>(new EndpointAddress(address)); } /// <summary> /// Puts the channel. /// </summary> /// <param name="channel">The <see cref="T:IChannel"/> channel.</param> private void PutChannel(IChannel channel) { IService item = channel as IService; if (item != null && channel.State == CommunicationState.Opened && this.channelCache.Count < MaxChannelCacheSize) { lock (this.channelCache) { this.channelCache.Enqueue(item); return; } } if (channel != null) { try { channel.Close(); } catch (Exception) { throw; } } } #endregion Methods }} ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download