Solibulo

Uninteresting things

A better DeleteExpiredSession stored procedure for ASP.NET sessions in SQL Server database

by softlion 6. January 2009 14:52

The standard ASP.NET 2/3/3.5 SP locks the ASPStateTempSessions table for a long time, which in turns locks running applications. This becomes very useful when SQL Server Agent jobs (especially xxx_Job_DeleteExpiredSessions) were disabled for some time for some reason.

A better SP looks like this:

   1: SET ANSI_NULLS ON
   2: GO
   3: SET QUOTED_IDENTIFIER ON
   4: GO
   5:  
   6: ALTER PROCEDURE [dbo].[DeleteExpiredSessions]
   7: AS
   8:  
   9: DECLARE @now datetime
  10: SET @now = GETUTCDATE()
  11: declare @count int
  12: set @count = 1
  13:  
  14: set ROWCOUNT 1000
  15: while @count != 0
  16: begin
  17:     begin tran
  18:     DELETE [Softlion].dbo.ASPStateTempSessions
  19:         WHERE Expires < @now
  20:     set @count = @@ROWCOUNT
  21:     commit
  22: end
  23: set ROWCOUNT 0
  24:  
  25: RETURN 0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    

You may also add the hint WITH (ROWLOCK) on the delete statement to be sure no lock are set on a range which may contains rows currently used by the application.

Tags: , ,

Script# or JSC ?

by softlion 4. October 2008 00:28

Have you ever dreamed to write javascript code in C# (or VB#) ?
Have you ever dreamed to write a Flash application in C# instead of ActionScript ?
It is now possible with either Script# or the JSC projects, both having a different approach to this same starting idea.

 

As was said, the idea is to write C# code which gets transformed somehow into javascript files. All of this using the powerful Visual Studio code editor with all its goodies like intellisense, snippets, refactoring, and the power of the C# language with support for LINQ in JSC !
How is it done behind the scene ?

 

In Script#, the C# code is parsed and compiled by the Script# compiler which outputs javascript code. Standard assemblies like system.dll must be replaced by specific Script# assemblies - this is done automatically using the correct project template in VS.

 

In JSC,  which stands for "Javascript Compiler" (which is now more than that as it generates also ActionScript Java and PHP !), the C# code is compiled by the regular C# compiler into assemblies (in MSIL), which in turn are parsed and "recompiled" by the jsc compiler into the output language (js files for javascript, as files for actionscript, ...).That means LINQ (.NET 3.5) is fully supported as it is only a syntax evolution which is completely transformed into "MSIL 2.0" (.NET 2.0) by the 3.5 compiler. You can reference any .NET assembly you want, but many built-in functions won't work as they depend on low-level system functions. JSC provides assemblies declaring specificities of each target language in its own namespace, as well as extension methods to help working with the target language objects.

Example:

    [Script, ScriptApplicationEntryPoint]
    [SWF(backgroundColor = 0xcccccc)]
    public class MyFlashMainClass : Sprite
    {
        Sprite mc;
        Sprite currentDraggedSprite;
        TextField t4;

        public MyFlashMainClass ()
        {
            var c = new Sprite();
            c.graphics.beginFill(0xf8f8f8);
            c.graphics.drawRect(1, 1, stage.stageWidth-2, stage.stageHeight-2);
            c.graphics.endFill();
            c.AttachTo(this);
            c.doubleClickEnabled = true;

            stage.frameRate = 30;
            stage.focus = this;
.....

 

Which one is better ? My heart goes to JSC as I'm fond of LINQ, it's anonymous delegates syntax and extension methods, even if Script# seems better documented.
I've successfully created my first Flash application using JSC and I'm very proud ! So it really works ! Even if there are syntax tricks and the needs to know basic flash programming to make it work.

 

References

JSC: http://jsc.sourceforge.net/

Script#: http://projects.nikhilk.net/ScriptSharp/

 

Gravatar ASP.NET custom control with source

by softlion 19. August 2008 10:48

Today I present an easy to use open source gravatar control.
To show a gravatar logo of one of your visitor you'll need only the visitor's email.

 

I took the source code from the reference implementation advertised on Gravatar from freshclickmedia (see references at the bottom of this article), updated it to match the new gravatar API, and among other things fixed the ViewState implementation.

 

Basic usage
1) In your project add a reference to the assembly FreshClickmedia.Web.dll
2) At the top of your aspx or ascx page add:
<%@ Register TagPrefix="fcm" Assembly="FreshClickmedia.Web" Namespace="FreshClickMedia.Web.UI.WebControls" %>
3) In your page add this tag where you want the logo to appear:
<fcm:Gravatar ID="GravatarImage" runat="server" Email="thisemaildoesnotexist@thisemaildoesnotexist.com" />

 

References
Gravatar: http://www.gravatar.com/
Original ASP.NET control from freshclickmedia: http://www.freshclickmedia.com/blog/2008/06/aspnet-gravatar-control-update-full-source-included/

 

Downloads
.NET Assembly only
Full source code with example web site

 

Examples

My email address, size of 80 pixels:
<fcm:Gravatar ID="Gravatar1" runat="server" Email="putexistingemailhere@putexistingemailhere.com" OutputGravatarSiteLink="true" Size="80" />
Gravatar

No email address, with default image (absolute url) specified:
<fcm:Gravatar ID="Gravatar2" runat="server" Size="80" DefaultImageUrl="http://farm3.static.flickr.com/2375/2552064340_192825f989_o.jpg" />
Gravatar

Email address not associated with Gravatar, with no default image (identicon):
<fcm:Gravatar ID="Gravatar3" runat="server" Email="thisemaildoesnotexist@thisemaildoesnotexist.com" />
Gravatar

Email address not associated with Gravatar, with no default image (wavatar):
<fcm:Gravatar ID="Gravatar4" runat="server" Email="thisemaildoesnotexist@thisemaildoesnotexist.com" DefaultImageType="Wavatar" />
Gravatar

Email address not associated with Gravatar, with no default image (Monsterid):
<fcm:Gravatar ID="Gravatar5" runat="server" Email="thisemaildoesnotexist@thisemaildoesnotexist.com" DefaultImageType="Monsterid" />
Gravatar 

 

Tags: ,