How to tell if Windows or Web GUI called library

Is there a system property/method that I can call from within my c# library assembly to tell if it is being called by a windows or asp.net .net program

p.s. I have found one way, and that is to put a try catch around a ::global or a webconfiguration call, but I am guessing that this adds overhead.

Evan Smith



Answer this question

How to tell if Windows or Web GUI called library

  • slender_bamboo

    I don't know why you should want to know it. It feels like a bad design to me and it should not need to be know for the Library.

    The only thing that i can imagine is that you use preprocessor directives to minimize file size, exclude some classes or use a other factory class.


  • RAi SWETA

    Not getting into the discussion about good or bad design, but you can check

    System.Web.HttpContext.Current

    being null if it's not a webapp.


  • haleyweb2005

    Actually, the reason I want to know is that the library has a DAL layer, and I want to know the connection string at run time. If the GUI application (which needs to know nothing about the DAL layer) is a windows application, then (as far as I understand) the latest .net repository for the connection string is the Settings.setting file, if it is a ASP.NET GUI, then the repository is the web.config file.

    Neither of these files are available to the library if the calling GUI application is the other type (i.e. a ASP.NET GUI will not allow the library to access a Settings.setting file).

    If you have any other suggestions (design or technical), other than hard coding the connection string ,I would appreciate them.

    Evan


  • Saishyam

    But the DAL shoulnd event have references to the System.Web Library. When you don't argu about good or bad design, it is a quick-N-dirty solution.


  • Surinder

    I assume you don't want to clutter up your methods with another parameter (the connectionstring).

    What you can do then is to make your own repository for the connectionstring inside your DAL. Expose a method for setting the string from the Web or the Windows application. The repository then takes the form of a singleton.

    Since your Web/Windows application references your DAL, it will also have access to the singleton repository.

    The code could be something like this:

    class Repository
    {
    static string connectionString;
    static public string ConnectionsString
    {
    get
    {
    if (connectionString == null)
    throw new Exception("DAL Connectionstring repository not initialized");
    return connectionString;
    }
    set
    {
    connectionString = value;
    }
    }
    }

    You use the setter from the Web/Windows application, and the getter from inside the DAL.

    You could generalize this for any type or class by converting the code above into a generic class:

    class Repository<T>
    {
    static T item;
    static public T Item
    {
    get
    {
    if (item == null)
    {
    string theType = typeof(T).ToString();
    throw new Exception("Repository for "+theType+" not initialized");
    }
    return item;
    }
    set
    {
    item = value;
    }
    }
    }


  • Kehama

    Use preprocessor directives to create a Web-DAL and a Win-DAL.

    In you ConnectionFactory class or something like that you just do this:


    public class ConnectionFactory
    {
    public static IDbConnection Create()
    {
    #if(WEB)
    string connectionString = Settings.ConnectionString;
    return new SqlConnection( connectionString );
    #else
    string connectionString = ReadConnectionStringFromINI();
    return new OleDbConnection( connectionString );
    #endif
    }
    }




  • How to tell if Windows or Web GUI called library