📄 filebasedpersonalizationprovider.cs
字号:
using System;
using System.IO;
using System.Threading;
using System.Data;
using System.Configuration;
using System.Collections.Specialized;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Hosting;
using System.Diagnostics;
namespace MsdnMag.Samples
{
/// <summary>
/// FileBasedPersonalziationProvider is a file-based provider for personalization that will
/// persist the binary blob for each user to a separate file in the App_Data directory
/// of the local application. It inherits from PersonalizationProvider which deals with
/// many of the provider implementation details, leaving just the actual serialization
/// and initialization to our class.
/// </summary>
public class FileBasedPersonalizationProvider : PersonalizationProvider
{
private string _applicationName;
public override string ApplicationName
{
get
{
if (string.IsNullOrEmpty(_applicationName))
_applicationName = HostingEnvironment.ApplicationVirtualPath;
return _applicationName;
}
set
{
_applicationName = value;
}
}
public override void Initialize(string name, NameValueCollection configSettings)
{
if (configSettings == null)
throw new ArgumentNullException("configSettings");
if (string.IsNullOrEmpty(name))
name = "FileBasedPersonalizationProvider";
if (string.IsNullOrEmpty(configSettings["description"]))
{
configSettings.Remove("description");
configSettings.Add("description", "File-based personalization provider");
}
base.Initialize(name, configSettings);
_applicationName = configSettings["applicationName"];
if (_applicationName != null)
configSettings.Remove("applicationName");
}
public override PersonalizationStateInfoCollection FindState(PersonalizationScope scope,
PersonalizationStateQuery query, int pageIndex, int pageSize, out int totalRecords)
{
// re-implement to retrieve data from files
// equivalent of aspnet_PersonalizationAdministration_FindState
//
totalRecords = 1;
throw new NotImplementedException();
}
public override int GetCountOfState(PersonalizationScope scope, PersonalizationStateQuery query)
{
// re-implement to retrieve count of either user or global personalization state
// equivalent of aspnet_PersonalizationAdministration_GetCountOfState sproc
//
throw new NotImplementedException();
}
public override int ResetUserState(string path, DateTime userInactiveSinceDate)
{
throw new NotImplementedException();
}
protected override void LoadPersonalizationBlobs(
WebPartManager webPartManager, string path, string userName,
ref byte[] sharedDataBlob, ref byte[] userDataBlob)
{
string allUsersFileName = HttpContext.Current.Server.MapPath(
ConstructAllUsersDataFileName(path));
string userFileName = HttpContext.Current.Server.MapPath(ConstructUserDataFileName(userName, path));
allUsersFileName = string.Intern(allUsersFileName);
userFileName = string.Intern(userFileName);
try
{
// Read in shared user data first.
//
// lock on the file name in case two clients try accessing
// the same file concurrently - note we lock on the Interned
// file name string, which will always return the same object
// for identical strings
if (Monitor.TryEnter(allUsersFileName, 5000) &&
File.Exists(allUsersFileName))
sharedDataBlob = File.ReadAllBytes(allUsersFileName);
else
throw new ApplicationException("Monitor timed out");
// Next read in user specific data (if there is any)
if (Monitor.TryEnter(userFileName, 5000) &&
File.Exists(userFileName))
userDataBlob = File.ReadAllBytes(userFileName);
else
throw new ApplicationException("Monitor timed out");
}
finally
{
Monitor.Exit(allUsersFileName);
Monitor.Exit(userFileName);
}
}
protected override void SavePersonalizationBlob(
WebPartManager webPartManager, string path,
string userName, byte[] dataBlob)
{
string fileName = HttpContext.Current.Server.MapPath(
string.IsNullOrEmpty(userName) ?
ConstructAllUsersDataFileName(path) :
ConstructUserDataFileName(userName, path));
fileName = string.Intern(fileName);
try
{
// lock on the file name in case two clients try accessing
// the same file concurrently
if (Monitor.TryEnter(fileName, 5000))
{
File.WriteAllBytes(HttpContext.Current.Server.MapPath(
fileName), dataBlob);
}
else throw new ApplicationException("Monitor timed out");
}
finally { Monitor.Exit(fileName); }
}
protected override void ResetPersonalizationBlob(WebPartManager webPartManager, string path,
string userName)
{
// re-implement to reset personalization data for either all users or per user
// equivalent of aspnet_PersonalizationPerUser_ResetPageSettings
// and aspnet_PersonalizationAllUsers_ResetPageSettings
//
}
public override int ResetState(PersonalizationScope scope, string[] paths, string[] usernames)
{
// re-implement to reset state for users
// equivalent of aspnet_PersonalizationAdministration_ResetUserState
// and (based on scope) aspnet_PersonalizationAdministration_ResetSharedState
//
throw new NotImplementedException();
}
private string ConstructAllUsersDataFileName(string path)
{
string pathConvertedToFileName = path.Replace('/', '_');
pathConvertedToFileName = pathConvertedToFileName.Replace('~', '_');
pathConvertedToFileName = pathConvertedToFileName.Replace('.', '_');
return "~/App_Data/allusers" + pathConvertedToFileName + ".bin";
}
private string ConstructUserDataFileName(string user, string path)
{
string pathConvertedToFileName = path.Replace('/', '_');
pathConvertedToFileName = pathConvertedToFileName.Replace('~', '_');
pathConvertedToFileName = pathConvertedToFileName.Replace('.', '_');
return "~/App_Data/" + user + pathConvertedToFileName + ".bin";
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -