Archive for July, 2011

Setting up Microsoft App Fabric for ASP.NET Session State

I had a few issues getting AppFabric setup to handle my ASP.NET session state. Here is a quick rundown of what it took. This was a single AppFabric server (Windows Server 2008 R2) in a work group, not on a domain.

  1. On your app fabric server, make a new local account called “AppFabric”
  2. Create a file share named AppFabric that gives the AppFabric user full control
  3. Install AppFabric, set the run as user to AppFabric
  4. Select the XML config provider and point it to the AppFabric Share
  5. In the start menu, there will an app fabric folder, with a windows power shell
    1. Run the power shell as administrator
    2. run the following command Set-CacheClusterSecurity -SecurityMode None -ProtectionLevel None
  6. Start your cache using the AppFabric Caching Admin Tool

That is everything on the server side. On the client ASP.NET app, it took a bit of tweaking. Here is my complete web.config file. Also note, that your Client must resolve the host name of the AppFabric server. I had to add an entry to my hosts file.

<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit http://go.microsoft.com/fwlink/?LinkId=169433 -->
<configuration>
<configSections>
<!-- required to read the <dataCacheClient> element -->
<section name="dataCacheClient"
type="Microsoft.ApplicationServer.Caching.DataCacheClientSection,
Microsoft.ApplicationServer.Caching.Core, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35"
allowLocation="true"
allowDefinition="Everywhere"/>
</configSections>
<dataCacheClient>
<!-- cache host(s) -->
<hosts>
<host
name="ServerName or IP Address"
cachePort="22233"/>
</hosts>
<securityProperties mode="None" protectionLevel="None" />
</dataCacheClient>
<system.web>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.0">
<assemblies>
<add assembly="Microsoft.ApplicationServer.Caching.Client, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add assembly="Microsoft.ApplicationServer.Caching.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</assemblies>
</compilation>
<sessionState mode="Custom" customProvider="AppFabricCacheSessionStoreProvider">
<providers>
<!-- specify the named cache for session data -->
<add
name="AppFabricCacheSessionStoreProvider"
type="Microsoft.ApplicationServer.Caching.DataCacheSessionStoreProvider"
cacheName="Name1" sharedId="SharedApp"
/>
</providers>
</sessionState>
</system.web>
</configuration>

Tags: ,

Making forms more friendly with submit on enter

Many times when using a web form, you will want to capture the users input as they type, so that you can fire a custom function when the user hits enter, or tab.

In this example I want to submit a ASP.NET form as if the user clicked a link button, if the user hits enter while on the last form field.

To accomplish this, first lets create a function to look at the characters and check to see if it is a line break. This JS Function will then execute whatever command is passed into the second parameter.


function onEnter(input, funk) {
if (input.keyCode == 13) {
input.returnValue = false;
input.cancel = true;
eval(funk);
}
}

We then need to add an event handler to pass the keystroke data, and command to be run as the user types. This is done by using onKeyDown on the textbox. In this example, when enter is typed into the textbox, it will execute the JS function submitLinkButton passing in a parameter.


<asp:TextBox ID="LastTextBox" runat="server" onkeydown="javascript:onEnter(event, 'submitLinkButton(\'LinkButton1\')');"></asp:TextBox>

The last step of this tutorial is to emulate a asp:linkbutton click. Make sure you select ClientIDMode=”static” for the link button. When the anchor renders, the href will look something like this href=”javascript:__doPostBack(‘ctl00$Content$ctl00$LinkButton1′,”). In this function we find the anchor element, and execute whatever code is rendered in the href. This is really easy when using a JQuery selector.


function submitLinkButton(linkButton) {
eval($('#' + linkButton).attr('href'));
}

Tags: , ,