| Steve's profileSteve's spaceBlogListsNetwork | Help |
Steve's space |
||||||
|
September 24 Custom Provider-Based ServicesMyBaseProvider.cs
using System.Configuration.Provider;
public abstract class MyBaseProvider : ProviderBase
{ public abstract string MyProperty { get; set; } public abstract void MyMethod();
} MyProvider.cs
using System;
using System.Collections.Generic; using System.Collections.Specialized; public class MyProvider : MyBaseProvider
{ protected override string MyProperty { get; set; } public override void Initialize(string name, NameValueCollection nameValueCollection)
{ if (nameValueCollection == null) { throw new ArgumentNullException("nameValueCollection"); } if (string.IsNullOrEmpty(name))
{ name = "MyProvider"; } if (string.IsNullOrEmpty(nameValueCollection["description"]))
{ nameValueCollection.Remove("description"); nameValueCollection.Add("description", "My Provider"); } base.Initialize(name, nameValueCollection);
} protected override void MyMethod()
{ throw new NotImplementedException(); } } Web.config / app.config <configuration>
<configSections> <section allowDefinition="MachineToApplication" name="myBaseProvider" restartOnExternalChanges="true" type="MyBaseProviderConfigurationSection, MyProvider, Version=1.0.0.0, Culture=Neutral"/> </configSections> <comparisonProvider defaultProvider="MyProvider"> <providers> <clear/> <add name="MyProvider" type="MyProvider, MaxRepSSRSAutomatedTesting, Version=1.0.0.0, Culture=Neutral"/> </providers> </comparisonProvider> </configuration> MyProviderCollection.cs
using System;
using System.Configuration.Provider; public class MyProviderCollection : ProviderCollection
{ public new MyBaseProvider this[string name] { get { return (MyBaseProvider)base[name]; } } public override void Add(ProviderBase provider)
{ if (provider == null) { throw new ArgumentNullException("provider"); } if (!(provider is MyBaseProvider))
{ throw new ArgumentException("Invalid provider type", "provider"); } base.Add(provider);
} } MyBaseProviderConfigurationSection.cs
using System.Configuration;
public class MyBaseProviderConfigurationSection : ConfigurationSection
{ [ConfigurationProperty("providers")] public ProviderSettingsCollection Providers { get { return (ProviderSettingsCollection)base["providers"]; } } [StringValidator(MinLength = 1)]
[ConfigurationProperty("defaultProvider", DefaultValue="MyProvider")] public string DefaultProvider { get { return (string)base["defaultProvider"]; } set { base["defaultProvider"] = value; } } } MyManager.cs
using System;
using System.Configuration; using System.Configuration.Provider; using System.Web.Configuration; public class MyManager
{ private static MyBaseProvider _MyBaseProvider = null; private static MyBaseProviderCollection _MyBaseProviderCollection = null; private static object _lock = new object(); public MyBaseProvider Provider
{ get { return _MyBaseProvider; } } public MyBaseProviderCollection ProviderCollection
{ get { return _MyBaseProviderCollection; } } public static void MyMethod()
{ LoadProviders(); _MyBaseProvider.MyMethod();
} private static void LoadProviders()
{ if (_MyBaseProvider == null) { lock (_lock) { if (_MyBaseProvider == null) { MyBaseProviderConfigurationSection myBaseProviderConfigurationSection = (MyBaseProviderConfigurationSection)ConfigurationManager.GetSection("myBaseProvider"); _MyBaseProviderCollection = new ComparisonProviderCollection();
ProvidersHelper.InstantiateProviders(myBaseProviderConfigurationSection.Providers, _MyBaseProviderCollection, typeof(MyBaseProvider));
_MyBaseProvider = _MyBaseProviderCollection[myBaseProviderConfigurationSection.DefaultProvider];
if (_MyBaseProvider == null)
{ throw new ProviderException ("Unable to load default provider MyBaseProvider"); } } } } } } March 28 How to use System.Data.SqlClient.SqlDependency with a Windows Serviceusing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Security;
using System.Security.Permissions;
using System.ServiceProcess;
using System.Data.SqlClient;
using System.Text;
namespace SqlDependencyExample
{
}
|
|
|||||
|
|