Tag: ASP.NET

  • Persisted Properties

    Back in classic ASP.NET I’d persist data extract from a web service in base class property as follows:

    private string m_stringData;
    public string _stringData
    { get {
    if (m_stringData==null)
    {
    //fetch data from my web service
    m_stringData = ws.FetchData()
    }
    return m_stringData;
    }
    }

    This way I could always simple use _stringData and know that I’d always get the data I was after (sometimes I’d use Session state as a store instead of a private member variable).

    In Silverlight with a WCF this doesn’t work, because the WCF service has to be called asynchronously.

    So, how do I get round this?… There are a number of routes to take. One method involves wrapping the messages synchronously and can be found at codeplex

    Another alternative that is less verbose, and arguably easier, to follow is using a lambda expression as follows:

    private string m_stringData;
    public string _stringData
    {
    get
    { //if we don't have a list of departments, fetch from WCF
    if (m_stringData == null)
    {
    StringServiceClient client = new StringServiceClient();
    client.GetStringCompleted +=
    (sender, e) =>
    {
    m_stringData = e.Result;
    };
    client.GetStringAsync();
    }

    return m_stringData;
    }
    }

    A final solution was posted on this stackoverflow thread

  • C# Reading content from text files

    This is something I do frequently, and I always forget the streaming syntax, so here it is. There are couple of ways to do this. Firstly, from an application:

    1. From the properties panel, set the Build Action as Resource and the Copy to Output Directory as Copy Always
    2. From code this can then be accessed with:
    public class ListOfStrings: List
    {
    //@ctor
    public ListOfStrings()
    {
    string line;

    StreamResourceInfo sri = App.GetResourceStream(new Uri("Namespace;component/filename.txt", UriKind.Relative));
    using (TextReader tr = new StreamReader(sri.Stream))
    {
    while ((line = tr.ReadLine()) != null)
    {
    Add(tr.ReadLine());
    }
    }
    }
    }

    NOTE: Namespace;component/filename.txt: the Namespace and filename have to be replaced. Leave in the component syntax

    In a class library, it is slightly different.

    1. From the properties panel, set the Build Action as Embedded Resource
    2. From code this can then be accessed with:

    public class ListOfStrings: List
    {
    //@ctor
    public ListOfStrings()
    {
    string line;

    Assembly assembly = Assembly.GetExecutingAssembly();
    Stream stream = assembly.GetManifestResourceStream("Namespace.ResourceFolder.Filename.txt");

    using (TextReader tr = new StreamReader(stream))
    {
    while ((line = tr.ReadLine()) != null)
    {
    Add(tr.ReadLine());
    }
    }
    }
    }

     
    NOTE: Namespace.ResourceFolder. has to be replaced with a suitable value.

    Obviously, both examples return at list of string objects, one object per line.

  • Using ASPNET_RegSQL.exe with SQL Express databases in APP_DATA

    As discussed at Lance’s Whiteboard, a really easy way to hook up an ASPNET membership provider database to an application … perfect for rapid prototyping…

    “Creating a new DB in VisualStudio.NET 2005 is as simple as “Select APP_DATA node -> Add New Item -> Sql Database” and wah-lah you have a new aspnet.mdf file located in your APP_DATA folder.”

    and to register the ASPNET membership provider content within this database:

    aspnet_regsql -A all -C "Data Source=.\SQLEXPRESS;Integrated Security=True;User Instance=True" -d "C:\MyProject\APP_DATA\aspnetdb.mdf

  • App_Offline.htm

    Cracking informational post from Scott Guthrie – and I can’t believe I’d never heard of this before!!

    Original post from Scott: http://weblogs.asp.net/scottgu/archive/2005/10/06/426755.aspx:

    Basically, if you place a file with this name in the root of a web application directory, ASP.NET 2.0 will shut-down the application, unload the application domain from the server, and stop processing any new incoming requests for that application. ASP.NET will also then respond to all requests for dynamic pages in the application by sending back the content of the app_offline.htm file (for example: you might want to have a “site under construction” or “down for maintenance” message).

    This provides a convenient way to take down your application while you are making big changes or copying in lots of new page functionality (and you want to avoid the annoying problem of people hitting and activating your site in the middle of a content update). It can also be a useful way to immediately unlock and unload a SQL Express or Access database whose .mdf or .mdb data files are residing in the /app_data directory.

    Once you remove the app_offline.htm file, the next request into the application will cause ASP.NET to load the application and app-domain again, and life will continue along as normal.

    And also worthy of note is that the returned content must be greater than 512kb, to get over an annoying anomoly within IE6. Simply flesh out the content of the file to greater than 512kb (use comments for example).

  • ASP.NET Write Permissions

    When writing a file from ASP.NET application, the target directory needs to have write permissions set for both the logged in account (maybe IUSER_[name]) and also the IIS_WPG account.

    For example, writing a file to http://localhost/app/results, where the application root (http://localhost/app/) maps to C:\Inetpub\wwwroot\app\

    The permissions on the root folder (C:\Inetpub\wwwroot\app\) can be readonly, but the permission on C:\Inetpub\wwwroot\app\results\ need to allow writing for IUSR and IIS_WPG accounts.