Archive for category Programming

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: , ,

Bulk transfering data in MS SQL 2008 using BCP

Recently I’ve been playing around with SQL Azure. One of the current limitations of Azure, is no support for replication. I’ve been looking into different solutions to try and sync Azure data with my local SQL Server. The BCP utility and bulk import seem to be promising. Here is a simple example to copy the contents of a table from one DB table / server to another.

To export data to a binary data file, use the following command,
bcp "[sql query]" queryout [filename].bin -S [serveraddress] -U [username] -P [password] -d [database] -n
To import the data via the BCP utility, you can use the following command line,
bcp [table] in [filename].bin -S [serveraddress] -U [username] -P [password] -d [database] -n
Alternatively, you can also import the data via a SQL Query by using BULK INSERT
BULK INSERT  [tablename]
FROM '[path and filename]'
WITH (DATAFILETYPE='native')

To use these commands with Azure, you must be using version 10. bcp /v will tell you your version number.

Tags: , , , , , ,

Head in the cloud

I got  a call this last week from a major hosting provider trying to get us to move all our servers into their cloud. I had not given it too much thought in the past, assuming that it would cost a lot of money. I started looking around and surprisingly it is not that expensive.

Something I find interesting about most of the cloud services are how they license Microsoft products. In the next few weeks I’ll be doing a good bit of research and possibly some trials with stress testing. It is a great concept but it is still hard to give up physically being able to touch your own server or hard disk.

Tags: , ,