📄 serviceutility.cs
字号:
private static SqlDbType ConvertToSqlDbType(Type dataType) {
switch (dataType.Name) {
case "Int32" :
return SqlDbType.Int;
case "Guid" :
return SqlDbType.UniqueIdentifier;
default :
return SqlDbType.NVarChar;
}
}
//*********************************************************************
//
// GetServiceInfo Method
//
// Returns information about a service with a particular name.
//
//*********************************************************************
public static ServiceInfo GetServiceInfo(string name) {
ServiceInfo serviceInfo = null;
SqlConnection conPortal = new SqlConnection(CommunityGlobals.ConnectionString);
SqlCommand cmd = new SqlCommand("Community_ServicesGetServiceInfo", conPortal);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@communityID", CommunityGlobals.CommunityID);
cmd.Parameters.Add("@name", name);
conPortal.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
serviceInfo = PopulateServiceInfoFromSqlDataReader(dr);
conPortal.Close();
return serviceInfo;
}
//*********************************************************************
//
// GetServiceInfoForRefresh Method
//
// Returns information about a service with a particular name
// and records to the database that the service was called.
//
//*********************************************************************
public static ServiceInfo GetServiceInfoForRefresh(string name) {
ServiceInfo serviceInfo = null;
SqlConnection conPortal = new SqlConnection(CommunityGlobals.ConnectionString);
SqlCommand cmd = new SqlCommand("Community_ServicesGetServiceInfoForRefresh", conPortal);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@communityID", CommunityGlobals.CommunityID);
cmd.Parameters.Add("@name", name);
conPortal.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
serviceInfo = PopulateServiceInfoFromSqlDataReader(dr);
conPortal.Close();
return serviceInfo;
}
//*********************************************************************
//
// GetAllServices Method
//
// Returns a list of all the services created for this community.
//
//*********************************************************************
public static ArrayList GetAllServices() {
ArrayList colServices = new ArrayList();
SqlConnection conPortal = new SqlConnection(CommunityGlobals.ConnectionString);
SqlCommand cmd = new SqlCommand("Community_ServicesGetAllServices", conPortal);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@communityID", CommunityGlobals.CommunityID);
conPortal.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
colServices.Add(PopulateServiceInfoFromSqlDataReader(dr));
conPortal.Close();
return colServices;
}
//*********************************************************************
//
// GetAllCommunityServices Method
//
// Returns a list of all the services created for this community
// excluding RSS services.
//
//*********************************************************************
public static ArrayList GetAllCommunityServices() {
ArrayList colServices = new ArrayList();
SqlConnection conPortal = new SqlConnection(CommunityGlobals.ConnectionString);
SqlCommand cmd = new SqlCommand("Community_ServicesGetCommunityServices", conPortal);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@communityID", CommunityGlobals.CommunityID);
conPortal.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
colServices.Add(PopulateServiceInfoFromSqlDataReader(dr));
conPortal.Close();
return colServices;
}
//*********************************************************************
//
// PopulateServiceInfoFromSqlDataReader Method
//
// Creates a ServiceInfo object from a SqlDataReader.
//
//*********************************************************************
private static ServiceInfo PopulateServiceInfoFromSqlDataReader(SqlDataReader dr) {
ServiceInfo serviceInfo = new ServiceInfo();
serviceInfo.ID = (int)dr["service_id"];
serviceInfo.Name = (string)dr["service_name"];
serviceInfo.Url = (string)dr["service_url"];
serviceInfo.Password = (string)dr["service_password"];
serviceInfo.Type = (ServiceType)dr["service_type"];
serviceInfo.MaximumItems = (int)dr["service_maximumItems"];
serviceInfo.RefreshRate = (int)dr["service_refreshRate"];
serviceInfo.DateLastRefreshed = (DateTime)dr["service_dateLastRefreshed"];
return serviceInfo;
}
//*********************************************************************
//
// ClearWebServiceBoxFromCache Method
//
// Removes a single Web Service Box for cache.
//
//*********************************************************************
public static void ClearWebServiceBoxFromCache(string serviceName) {
string cacheKey = CommunityGlobals.CacheKey("WebServiceBox_" + serviceName);
HttpContext.Current.Cache.Remove(cacheKey);
}
//*********************************************************************
//
// GetServiceResponseInfo Method
//
// This is the main method for calling a service. The method
// attempts to get a service response from the cache. If it
// can't get the response from the cache, and the last refresh
// duration has passed, the service is called directly.
//
// Note: if the cache is dumped before the RefreshRate, then
// this method will return null. This method was designed to
// guarantee that the service will not be called more often
// than the refresh rate dictates.
//
//*********************************************************************
public static ServiceResponseInfo GetServiceResponseInfo(string name) {
// Get abbreviated reference to the current context
HttpContext context = HttpContext.Current;
// Get the cache key
string cacheKey = CommunityGlobals.CacheKey("WebServiceBox_" + name);
// Attempt to retrieve the Service Response from cache
ServiceResponseInfo responseInfo = (ServiceResponseInfo)context.Cache[cacheKey];
// If can't get from cache, better call the service
if (responseInfo == null)
{
// Retrieve service info from DB
ServiceInfo serviceInfo = GetServiceInfoForRefresh(name);
//SMR - Begin: fixing refresh behavior
// Removed to allow app restarts to attempt to pull data for service immediatly.
// Only happens when cache is empty... since failures are also cached not,
// this should only cause one call to a service on restart. Afterwards additional
// calls would follow rules for refresh interval, or re-try on failure interval.
// Don't refresh if under time limit
//if (serviceInfo.DateLastRefreshed.AddMinutes(serviceInfo.RefreshRate) > DateTime.Now)
// return null;
//SMR - End: fixing refresh behavior
// Call the service
responseInfo = CallService(serviceInfo);
if (responseInfo == null)
{
//SMR - Begin: fixing refresh behavior
// The intent here is to cache an empty item for 1/4 the normal refresh interval
// This will cause the CSK to retry a failed service sooner, but will not cause
// every request to the site to trigger another attempt.
responseInfo = new ServiceResponseInfo();
responseInfo.ServiceFailedLastCall = true;
context.Cache.Insert
(
cacheKey,
responseInfo,
null,
DateTime.Now.AddMinutes(((15)*(serviceInfo.RefreshRate/60))),
Cache.NoSlidingExpiration,
CacheItemPriority.High,
null
);
//SMR - End: fixing refresh behavior
return null;
}
// Update date last refreshed
responseInfo.DateLastRefreshed = DateTime.UtcNow;
UpdateServiceRefreshDate(name, responseInfo.DateLastRefreshed);//SMR - fixing refresh behavior
context.Cache.Insert
(
cacheKey,
responseInfo,
null,
responseInfo.DateLastRefreshed.AddMinutes(serviceInfo.RefreshRate).ToLocalTime(), //SMR - fixing refresh behavior
Cache.NoSlidingExpiration,
CacheItemPriority.High,
null
);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -