Windows Azure Cache Dependency

by Rok Bermež 10. April 2011 23:07
We are supposed to get Windows AppFabric Cache real soon, but till than form time to time we need to synchronize content cached inside our Web roles. SqlDependency is one way, but it cannot solve all problems (especialy those that are not based on SQL data). So to help with the matter I wrote AzureStorageCacheDependency that uses Azure storage to know when data is outdated and cache shloul be cleared. If anyone is in need of something similar, here it goes: public class AzureStorageCacheDependency : System.Web.Caching.CacheDependency { private System.Threading.Timer _timer; private int _poolTime; private CloudBlob _blob; private string _key; public AzureStorageCacheDependency(string connectionString, string conteinerAddress, string blobAddress, int poolTime = 5000) { _poolTime = poolTime; using (AzureContext azureContext = new AzureContext(true)) { var storageAccount = CloudStorageAccount.Parse(connectionString); CloudBlobClient blobStorage = storageAccount.CreateCloudBlobClient(); CloudBlobContainer container = blobStorage.GetContainerReference(conteinerAddress.ToLower()); container.CreateIfNotExist(); _blob = container.GetBlockBlobReference(blobAddress.ToLower()); if (!Exists(_blob)) { Reset(); } else { _key = _blob.DownloadText(); } } _timer = new Timer(new System.Threading.TimerCallback(CheckDependencyCallback), this, 0, _poolTime); } public void Reset() { _key = Guid.NewGuid().ToString(); _blob.UploadText(_key); } private void CheckDependencyCallback(object sender) { if (!Exists(_blob) || _key != _blob.DownloadText()) { NotifyDependencyChanged(this, EventArgs.Empty); _timer.Dispose(); } } public static bool Exists(CloudBlob blob) { try { blob.FetchAttributes(); return true; } catch (StorageClientException e) { if (e.ErrorCode == StorageErrorCode.ResourceNotFound) { return false; } else { throw; } } } } public class AzureContext : IDisposable { HttpContext _oldHttpContext; bool _restoreOldHttpContext = false; public AzureContext(bool forceSettingContextToNull = false) { if (forceSettingContextToNull) { _oldHttpContext = HttpContext.Current; HttpContext.Current = null; _restoreOldHttpContext = true; } else { try { HttpResponse response = HttpContext.Current.Response; } catch (HttpException) { _oldHttpContext = HttpContext.Current; HttpContext.Current = null; _restoreOldHttpContext = true; } } } public void Dispose(bool disposing) { if (disposing) { if (_restoreOldHttpContext) { HttpContext.Current = _oldHttpContext; } } } public void Dispose() { Dispose(true); } ~AzureContext() { Dispose(false); } }


.Net | c# | ASP.NET | Azure | Web


by Rok Bermež 5. April 2011 14:31
From time to time, we need to selectively redirect the browser to another location as a result of an AJAX action. Just returning RedirectResult won’t do the trick (even if we are used to similar functionality in ASP.NET AJAX in combination with WebForms ). Here is a very simple RedirectResult thet will be appropriate in those scenarios: public class AjaxRedirectResult : RedirectResult { public AjaxRedirectResult(string url): base(url){} public override void ExecuteResult(ControllerContext context) { if (context.RequestContext.HttpContext.Request.IsAjaxRequest()) { JavaScriptResult result = new JavaScriptResult() { Script = String.Format("window.location='{0}';", UrlHelper.GenerateContentUrl(Url, context.HttpContext)) }; result.ExecuteResult(context); } else base.ExecuteResult(context); } }


.Net | c# | ASP.NET | mvc | Web

Windows Azure Platform SDK 1.4 Released

by Rok Bermež 11. March 2011 09:14
SDK 1.4 is available and it  fixes several significant bugs including the nasty RDP bug and adds capabilities like multiple administrator support from the enhanced Windows Azure Connect portal. Bug Fixes: Resolved an issue that caused full IIS fail when the web.config file was set to read-only. Resolved an issue that caused full IIS packages to double in size when packaged. Resolved an issue that caused a full IIS web role to recycle when the diagnostics store was full. Resolved an IIS log file permission Issue which caused diagnostics to be unable to transfer IIS logs to Windows Azure storage. Resolved an issue preventing csupload to run on x86 platforms. User errors in the web.config are now more easily diagnosable. Enhancements to improve the stability and robustness of Remote Desktop to Windows Azure Roles. New Features Windows Azure Connect: Multiple administrator support on the Admin UI. An updated Client UI with improved status notifications and diagnostic capabilities. The ability to install the Windows Azure Connect client on non-English versions of Windows. Windows Azure CDN: Windows Azure CDN for Hosted Services: Developers can now use the Windows Azure Web and VM roles as"origin" for objects to be delivered at scale via the Windows Azure CDN. Static content in a website can be automatically edge-cached at locations through out the United States, Europe, Asia, Australia and South America to provide maximum bandwidth and lower latency delivery of website content to users. Serve secure content from the Windows Azure CDN: A new checkbox option in the Windows Azure management portal enables delivery of secure content via HTTPS through any existing Windows Azure CDN account. You may download the new Windows Azure 1.4 SDK here.


.Net | Azure

Windows Azure SDK 1.3 Refresh

by Rok Bermež 2. February 2011 19:09
Due to numerous bugs, the 'hotfix' for SDK was released recently. I havent tried it yet but sure hope that some annoying things will go away. "The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state" really is a mood killer. Hope for the best and upgrade here. (32 bit version also available here)


.Net | Azure

Windows Azure Full IIS

by Rok Bermež 10. January 2011 23:00
Some time ago I wrote how to deploy multitenant application to the Cloud. The process was tricky at best. With new Windows Azure SDK 1.3 things just got a lot simpler and I absolutely love it. The feature is called Full IIS and allows your web roles to access the full range of web server features that are available in on-premise IIS installations. However if you choose to use them, there are a few differences from the classic Azure Hosted Web Core (HWC) model. First you need to tell Windows Azure SDK to use Full IIS instead of HWC and you do this by adding a valid <Sites> section to your ServiceDefinition.csdef  file.  By default Visual Studio will create HWC model definition like this:     <Sites>       <Site name="Web">         <Bindings>           <Binding name="Endpoint1" endpointName="Endpoint1" />         </Bindings>       </Site>     </Sites> You can easily customize it to define multiple web sites, or virtual apps (virtual directories are also supported now): <Sites>   <Site name="MainSite">     <VirtualApplication name="WebApp1" physicalDirectory="D:\Delo\Projects\WebApp1\" />     <Bindings>       <Binding name="HttpIn" endpointName="HttpIn" />     </Bindings>   </Site>   <Site name="AnotherSiteOrSubDomain" physicalDirectory="D:\Delo\Projects\ AnotherSiteOrSubDomain ">     <Bindings>       <Binding hostHeader="" name="HttpIn" endpointName="HttpIn"/>     </Bindings>   </Site> </Sites> Things are much more similar to on-premises application then in HWC model. While RoleEntryPoint  runs under different process (WaIISHost.exe) than your web roles  (w3wp.exe), OnStart method still gets called but configuration settings work a bit differently. You cannot register or store some static values to be available to all websites. Remember its running in a different process... so you wont be able to access its data. What I mean by this is, probably everyone dealing with Azure Development has something similar to this in their role onstart method: CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>{     configSetter(RoleEnvironment.GetConfigurationSettingValue(configName)); }); While code is still perfectly sound, it wont do any good to our web roles, so the proper place to register it would by global.asax on ApplicationStart event. It all kind of makes sense, since different websites need different resources anyway.


.Net | c# | Azure


<<  January 2018  >>

View posts in large calendar

Page List

Month List