Archive

Archive for the ‘Development’ Category

SignalR–Configuring timeout for connections

March 2, 2012 1 comment

Based on the SignalR wiki, in order to make SignalR work with Azure or load balancers, you will need to tune it such that the polling timeout is lesser than the default polling time of 110 seconds.

To do this, the wiki at https://github.com/SignalR/SignalR/wiki/Configuring-SignalR tells you to do the following

ASP.NET Example (Global.asax)

 var config = AspNetHost.DependencyResolver.Resolve<IConfigurationManager>(); 
 config.ReconnectionTimeout = TimeSpan.FromSeconds(25);

 

However if you were to just copy and paste that, you will find that visual studio reports the following 2 errors and wurlies on your code

The name ‘AspNetHost’ does not exist in the current context

and

The type or namespace name ‘IConfigurationManager’ could not be found (are you missing a using directive or an assembly reference?)

 

image

 

Actually what happens is that the wiki actually forgot to mention that you will actually need to import a few namespaces to global.asax.

They are

  1. SignalR.Configuration – Fixes the IConfigurationManager
  2. SignalR.Hosting.AspNet – Fixes AspNetHost
  3. SignalR.Infrastructure – This is where the Resolve extension reside
      So after adding all these, your global.asax will look like

      <%@ Application Language="C#" %>

      <%@ Import Namespace="SignalR.Configuration" %>

      <%@ Import Namespace="SignalR.Hosting.AspNet" %>

      <%@ Import Namespace="SignalR.Infrastructure" %>

      <script runat="server">

          void Application_Start(object sender, EventArgs e)

          {  
              // Code that runs on application startup

              var config = AspNetHost.DependencyResolver.Resolve<IConfigurationManager>();

              config.ReconnectionTimeout = TimeSpan.FromSeconds(25);

          }

      </script>

    Enjoy!

    Categories: ASP.NET, Development Tags:

    XCode and TFS 2010 Part 1 – Setting up TFS 2010

    November 25, 2010 7 comments

    There have been many cases where i heard of users wanted to get XCode working with TFS 2010.

    Obviously out of the box, XCode does not work with TFS 2010. However there is a way to make both of them talk :)

    I’m outlining the steps below on how to configure both of them to communicate properly.

    For those who are lazy to download from Codeplex, you can get the required files at http://cid-895931b65435bb3d.office.live.com/self.aspx/Public/SvnBridge%20-%20Files.zip

    Step 1: Download SVNBridge from Codeplex

    The first step is to configure TFS to have a SVN bridge. To do that, head down to http://svnbridge.codeplex.com/ and download the source files (DO NOT USE THE DOWNLOAD OPTION).

    Once you downloaded all the source files, unzip them to a directory and run the build.bat

    Once the build is complete, we have are ready to deploy the SVN website.

    Step 2: Create a local user with read rights to the whole TFS

    Login to your TFS server and create a local user e.g svnbridge

    Step 3: Create folder for SVN bridge website

    Now create a folder which will store the files for the SVN bridge (e.g c:\svnbridge)

    Copy the files from SvnBridgeViaIis\bin to c:\svnbridge\bin

    Copy SvnBridgeViaIis\web.config to c:\svnbridge

    Open port 8080 on your firewall if neccessary

    Edit c:\svnbridge\web.config

    Change the TFSUrl to point to your TFS Server, e.g http://localhost:8080/tfs/DefaultCollection

    Change DomainIncludesProjectName to False

    Change LogPath to a path for the SVNBridge Logs

    Update the ReadAllUser* properties to the local user you have just created

    So now your web.config should look like

    Step 4: Setup IIS

    Run IIS Manager and Create a new website. The source directory should point to the svnbridge folder (e.g c:\svnbridge) and the port should be 8081

    Click on the authentication tab and make sure that ONLY ASP.NET impersonation and digest authentication is enabled

    Now goto Configuration editor and select the system.webServer/validation section

    Set validateIntegratedModeConfiguration to false

    Step 5: Install the Perfmon Counters

    Open the SVNBridge solution and build the SvnBridge.PerfCounter.Installer project.

    Run the SvnBridge.PerfCounter.Installer.exe found inside the bin\debug folder

    Step 6: iisreset the system

    Trigger an iisreset to flush everything and get it ready for SVN!

    Categories: Development

    Community Server 2.x – AD Forms Authentication

    September 16, 2010 Leave a comment

    For those that are looking for the AD Forms Authentication library which i wrote quite a while back, here’s the link

    http://cid-895931b65435bb3d.office.live.com/self.aspx/Public/Jeffery.FormsADMemberRole.zip

    the original post can be found at http://telligent.com/support/communityserver/archives/community_server_add_ons_archive_read_only_1/f/172/t/1030382.aspx

    The main purpose of this library is to provide a way to use the normal forms authentication, but instead of querying the backend sql server for the data, it will query the AD instead.

    Tested to work with Community server 2.x, there’s a high possibility of it working with the newer versions as well since it is still based on the asp.net membership providers

    Categories: Development

    The interesting app_offline.htm file

    November 12, 2009 Leave a comment

    For those that’s still in the dark about this cool feature, here’s the lowdown on it.

     

    If you put a app_offline.htm file inside your asp.net application, on the next request, ASP.NET will shutdown the app domain and return the contents of the file in response to all dynamic asp.net requests. This way, you can easily take down your site for maintenance and updates and then bring it right up by deleting the file once you are done!

    Categories: ASP.NET, Development

    Disabling the program has stopped working promot

    November 12, 2009 Leave a comment

    Ever so often, especially when you are using interop inside .net applications, you may trigger some code which will totally crash your system.

    This prompt usually does not help in any way whatsoever.

    So here’s how to take it out permanently.

    1. Change HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Windows\ErrorMode from 0 to 2

    2. Change HKEY_CURRENT_USER\Software\ Microsoft\Windows\Windows Error Reporting\DontShowUI from 0 to 1

    Categories: Development

    ASP.NET and Web Gardens

    March 26, 2009 2 comments

    When you are running your ASP.NET applications inside an application pool with only 1 worker process, everything works as expected. But things usually do not go so smoothly when you run the same application inside a web garden.

    What is a web garden

    Simply put, a web garden is a single web server running multiple IIS processes. The good thing about this is, there are multiple process which can service your http/https calls. So if a client is holding up one process, the other processes can still continue. Also if a particular process were to hang and restart itself, your website will still continue to serve clients as the other processes are running.

    Read more about Microsoft’s explanation of web garden here

    The article clearly states that

    all processes have their own copy of application state, in-process session state, caches, and static data

    Let us examine each one of these and see what we can do to workaround it

    Application state

    Problem Statement

    As each process has their own application state, whatever you change in one process will not be updated in another process. As this exists purely for backward compatibility with classic ASP applicationstate, Microsoft does not recommend using this alot, especially for read/write operations.

    Workaround

    Use Cache instead of ApplicationState if you can.
    Cached Items

    Problem Statement

    Be it HttpRunTime.Cache or Enterprise Library Cache, the cache will not reflect latest updates inside a web garden. This is because only the worker process which updated the Cache Item will see the updated values, the other worker processes will not be notified that a change has happened and will continue using the value stored in their in memory cache.

    Workaround

    There are a few paid solutions, and the only free one so far which is able to address this is can be found at http://enyimmemcached.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=13095 or http://www.codeproject.com/KB/aspnet/memcached_aspnet.asp

    Session state

    Problem Statement

    Using web gardens, you will find that the session values gets rotated around. Again this is due to the fact that different worker process is handling your request, and each worker process has its own session state management.

    Workaround

    Simply change the inproc to either State Server or SQL Server to store the session state outside of the worker process. Performance will degrade when using either of these 2 options, but at least you can be assured of the persistancy of the data

    Categories: ASP.NET, Development

    Custom Controls

    March 25, 2009 Leave a comment

    ASP.NET makes custom controls extremely easy to write, but if you are not careful, hard to manage.

    There are 2 ways to create a custom control

    1. Writing your own HTML code
    2. Using the controls property

    Writing your own HTML code

    Using your own properties, you can easily write HTML code to generate the
    output

    e.g consider a simple scenario where you take in a text and color input and
    render the control.

    You can simply create a class and extend it from the WebControl base class to
    have the functions available to your custom control.
    Thereafter, just create the properties you require and use them inside the
    Render function.
    Do note that you can render different output when in design mode and actual
    rendering, all you need is to check the DesignMode property.
    If however you need input boxes, do take note that you need to handle the
    postback event and put the value back into the inputs manually.

    public class
    SimpleControl :
    WebControl

    {

    public
    SimpleControl()

    {

    }

    [BindableAttribute(true),
    Category("UI"),
    DefaultValue("")]

    public
    string Text

    {

    get

    {

    object obj =
    this.ViewState["Text"];

    if (obj ==
    null)
    return
    “”;

    else
    return obj.ToString();

    }

    set {
    this.ViewState["Text"]
    =
    value; }

    }

    [BindableAttribute(true),
    Category("UI"),
    DefaultValue("")]

    public
    string Color

    {

    get

    {

    object obj =
    this.ViewState["Color"];

    if (obj ==
    null)
    return
    “”;

    else
    return obj.ToString();

    }

    set {
    this.ViewState["Color"]
    =
    value; }

    }

    protected
    override
    void Render(HtmlTextWriter
    writer)

    {

    writer.Write(“<font color=’”
    + Color +
    “‘>” +
    Text +
    “</font>”);

    //base.Render(writer);

    }

    }

    Using the controls property

    The good thing about using the controls property is that you do not need to
    care about ViewState (it is handled by the individual control), Postback
    inputs handling (handled again by the individual control).

    The bad thing is

    1. Have to write your own custom event handlers
    2. Everything needs to be control based, you cannot emit your own html.

    public class
    LabelExtendedControl
    :
    Label

    {

    public
    SimpleControl()

    {

    }

    [BindableAttribute(true),
    Category("UI"),
    DefaultValue("")]

    public
    string xText

    {

    get

    {

    object obj =
    this.ViewState["Text"];

    if (obj ==
    null)
    return
    “”;

    else
    return obj.ToString();

    }

    set {
    this.ViewState["Text"]
    =
    value; }

    }

    [BindableAttribute(true),
    Category("UI"),
    DefaultValue("")]

    public
    string xColor

    {

    get

    {

    object obj =
    this.ViewState["Color"];

    if (obj ==
    null)
    return
    “”;

    else
    return obj.ToString();

    }

    set {
    this.ViewState["Color"]
    =
    value; }

    }

    protected
    override
    void Render(HtmlTextWriter
    writer)

    {

    this.Text = xText;

    this.ForeColor =
    System.Drawing.
    Color.FromKnownColor(xColor);

    base.Render(writer);

    }

    }

    Custom Events

    To create your own custom events, you need
    to write 3 main items

    1. A readonly object containg the event
    2. A function which will call the Event
    3. The actual event handler itself

    private
    readonly
    object EventCreate =
    new
    object();

    protected
    virtual
    void OnCreate(EventArgs
    e)

    {

    EventHandler
    onCreateHandler = (
    EventHandler)Events[EventCreate];

    if
    (onCreateHandler !=
    null)
    onCreateHandler(
    this,
    e);

    }

    [Category("Action"),Description("Raised
    when a CommandEvent occurs within an item."
    )]

    public
    event
    EventHandler Create

    {

    add

    {

    Events.AddHandler(EventCreate, value);

    }

    remove

    {

    Events.RemoveHandler(EventCreate, value);

    }

    }

    Application Caching

    March 22, 2009 Leave a comment

    Introduction

    ASP.NET offers several forms of caching. They are output caching, HttpRuntime.Cache and finally Enterprise Library Cache. Each of these aims to meet the requirements of different scenarios.

    This article shall explore the scenarios which each of these best fit.

    Output caching

    There are 2 variants of output caching, you can either cache the whole page or portions of the page (these portions must be in the form of user controls)

    This is commonly used when the steps to reproduce the page is huge (e.g large database calls, long file read times, huge processing requirements), but the output does not change frequently, e.g a FAQ page listing all the FAQs and employing javascript for inline fuzzy search (employing different scripts for different browsers)

    When you use output caching, the output is cached for a specific duration e.g 120 seconds and can be set to vary based on different parameters e.g querystring, browser. In the previous scenario of the FAQ page, you can use the VaryByCustom=”browser” parameter to cache and vary the output based on different browsers.

    MSDN on Output Caching

    HttpRuntime.Cache

    HttpRuntime.Cache is used to store serializable objects in a cache for future use. This cache is more powerful compared to output caching, but using it requires more work. This is frequently used to cache the return data in business layers.

    If you do not wish to bother about the technicalities behind this cache, you can easily add to it via the following code

    HttpRuntime.Cache["key"] = value

    This will add the item to the cache with default values for all parameters. Alternatively you can look at the complete  declaration for adding an item to the cache

    public Object Add(
    string key,
    Object value,
    CacheDependency dependencies,
    DateTime absoluteExpiration,
    TimeSpan slidingExpiration,
    CacheItemPriority priority,
    CacheItemRemovedCallback onRemoveCallback
    )

    This allows you to add a serializable object to the cache with

    • Dependencies: This indicates that this cache item is dependent on another object (usually a file), so when the dependency is changed, the cache object is automatically removed from the cache.
    • Absolute Expiration: This indicates that the cache item will expire after a fixed amount of time. Once the object expires, it is removed from the cache
    • Sliding Expiration: This tells the cache to extend the cache item a fixed amount of time each time the cache item is accessed. Once this amount of time is reached and the cached item is not accessed, it will expire
    • Priority: This tells the cache the priority of the cache item. This comes into effect when the cache is full and items needs to be removed. Items with a lower priorities are removed first.
    • CacheItemRemovedCallback: This is a delegate function which is run when the item is removed from the cache, the key, value and reason for removal is sent to this function so that you can do the appropriate processing.

    MSDN on HttpRuntime.Cache

    Enterprise Library Cache

    There are 5 main differences between this and HttpRuntime.Cache. If your needs meet any one of these points, then it is recommended that you use Enterprise Library Cache.

    1. The cache is stored independently of the web server, so even if you were to do an iisreset, the cache still remains
    2. You can configure the cache to be stored in a persistent backing store e.g SQL Server
    3. There can be multiple caches serving different needs in the same web application
    4. You need more control over expiration policies
    5. You do not need to know when the item expires, but you want to be able to reload the item if required

    MSDN on Enterprise Library Caching Block

    Summary

    1. Output caching: Used to store the page output so that ASP.NET does not need to render the content again
    2. HttpRuntime.Cache: Used by your web application to store items in a cache, the items will disappear when the website is reset
    3. Enterprise Library Cache: Separate cache mechanism which has its own memory and data store, is not affected by server restarts (if using persistent backing store)

    Caveats

    1. All of these caches is restricted to application domain scope, i.e you cannot share them across different application domains (e.g in web garden or multi server environment)
    Categories: ASP.NET, Development
    Follow

    Get every new post delivered to your Inbox.