Created
March 21, 2011 20:56
-
-
Save srkirkland/880204 to your computer and use it in GitHub Desktop.
Attribute for adding the result of a server message remote call to cache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[System.Diagnostics.DebuggerStepThroughAttribute()] | |
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")] | |
public partial class MessageServiceClient : System.ServiceModel.ClientBase<IMessageService>, IMessageService | |
{ | |
public MessageServiceClient() | |
{ | |
} | |
public MessageServiceClient(string endpointConfigurationName) : | |
base(endpointConfigurationName) | |
{ | |
} | |
public MessageServiceClient(string endpointConfigurationName, string remoteAddress) : | |
base(endpointConfigurationName, remoteAddress) | |
{ | |
} | |
public MessageServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) | |
: | |
base(endpointConfigurationName, remoteAddress) | |
{ | |
} | |
public MessageServiceClient(System.ServiceModel.Channels.Binding binding, | |
System.ServiceModel.EndpointAddress remoteAddress) : | |
base(binding, remoteAddress) | |
{ | |
} | |
public Catbert4.Services.Wcf.ServiceMessage[] GetMessages(string appName) | |
{ | |
return base.Channel.GetMessages(appName); | |
} | |
public System.IAsyncResult BeginGetMessages(string appName, System.AsyncCallback callback, object asyncState) | |
{ | |
return base.Channel.BeginGetMessages(appName, callback, asyncState); | |
} | |
public Catbert4.Services.Wcf.ServiceMessage[] EndGetMessages(System.IAsyncResult result) | |
{ | |
return base.Channel.EndGetMessages(result); | |
} | |
} | |
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")] | |
[System.ServiceModel.ServiceContractAttribute(Namespace = "https://secure.caes.ucdavis.edu/Catbert4", | |
ConfigurationName = "IMessageService")] | |
public interface IMessageService | |
{ | |
[System.ServiceModel.OperationContractAttribute( | |
Action = "https://secure.caes.ucdavis.edu/Catbert4/IMessageService/GetMessages", | |
ReplyAction = "https://secure.caes.ucdavis.edu/Catbert4/IMessageService/GetMessagesResponse")] | |
Catbert4.Services.Wcf.ServiceMessage[] GetMessages(string appName); | |
[System.ServiceModel.OperationContractAttribute(AsyncPattern = true, | |
Action = "https://secure.caes.ucdavis.edu/Catbert4/IMessageService/GetMessages", | |
ReplyAction = "https://secure.caes.ucdavis.edu/Catbert4/IMessageService/GetMessagesResponse")] | |
System.IAsyncResult BeginGetMessages(string appName, System.AsyncCallback callback, object asyncState); | |
Catbert4.Services.Wcf.ServiceMessage[] EndGetMessages(System.IAsyncResult result); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.ServiceModel; | |
using System.Web; | |
using System.Web.Caching; | |
using System.Web.Mvc; | |
using Catbert4.Services.Wcf; | |
using System.Web.Configuration; | |
namespace Catbert4.Helpers | |
{ | |
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)] | |
public class ServiceMessageAttribute : ActionFilterAttribute | |
{ | |
private readonly string _appName; | |
public ServiceMessageAttribute(string appName) | |
{ | |
_appName = appName; | |
} | |
private string _cacheKey; | |
public string CacheKey | |
{ | |
get { return _cacheKey ?? "ServiceMessages"; } | |
set { _cacheKey = value; } | |
} | |
public int CacheExpirationInSeconds { get; set; } | |
private TimeSpan CacheExpiration | |
{ | |
get | |
{ | |
return CacheExpirationInSeconds == default(int) | |
? TimeSpan.FromDays(1) | |
: TimeSpan.FromSeconds(CacheExpirationInSeconds); | |
} | |
} | |
public string MessageServiceUrl { get; set; } | |
public string MessageServiceAppSettingsKey { get; set; } | |
private string ServiceUrl | |
{ | |
get | |
{ | |
if (MessageServiceUrl == null && MessageServiceAppSettingsKey == null) | |
throw new InvalidOperationException( | |
"Either MessageServiceAppSettingsKey or MessageServiceAppSettingsKey must be set."); | |
if (MessageServiceUrl != null && MessageServiceAppSettingsKey != null) | |
throw new InvalidOperationException( | |
"Either MessageServiceAppSettingsKey or MessageServiceAppSettingsKey must be set, but not both."); | |
return MessageServiceUrl ?? WebConfigurationManager.AppSettings[MessageServiceAppSettingsKey]; | |
} | |
} | |
public override void OnActionExecuting(ActionExecutingContext filterContext) | |
{ | |
var cache = HttpRuntime.Cache; | |
if (cache[CacheKey] != null) return; | |
if (string.IsNullOrWhiteSpace(ServiceUrl)) | |
throw new InvalidOperationException( | |
"Service Url is not valid: Please set either MessageServiceAppSettingsKey or MessageServiceAppSettingsKey"); | |
cache[CacheKey] = new ServiceMessage[0]; | |
var binding = new BasicHttpBinding(); | |
if (ServiceUrl.StartsWith("https://")) binding.Security.Mode = BasicHttpSecurityMode.Transport; | |
var client = new MessageServiceClient(binding, new EndpointAddress(ServiceUrl)); | |
client.BeginGetMessages(_appName, OnMessagesRecieved, client); | |
} | |
private void OnMessagesRecieved(IAsyncResult ar) | |
{ | |
var client = (MessageServiceClient) ar.AsyncState; | |
try | |
{ | |
// Get the results. | |
var messages = client.EndGetMessages(ar); | |
// Insert into the cache | |
HttpRuntime.Cache.Insert(CacheKey, messages, null, Cache.NoAbsoluteExpiration, CacheExpiration); | |
} | |
finally | |
{ | |
//Close the client | |
client.Close(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment