Using the same class library in WinForms and WebForms projects

Discussion in 'ASP .Net' started by David Jackson, Apr 29, 2009.

  1. Hello,

    Following advice from Gregory A. Beamer, I have written a C# class library
    which contains several base classes to provide functionality such as
    database access, mail, encryption and so on.

    This class library is intended to be reused unmodified in all subsequent
    WinForms and WebForms development in Visual Studio.NET 2008 / C'#.

    For WinForms projects, I want the class library to fetch the database
    ConnectionString from the main application's config file. No problem with
    that:
    connectionString =
    System.Configuration.ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString);

    But, for WebForms, I want the class library to fetch it from the main web
    app's Application cache. We're a small company and have only one database
    so, to avoid opening and reading web.config every time we need to access the
    database, we fetch the ConnectionString out of web.config in
    Application_Start and store it as an Application variable:
    protected void Application_Start(Object sender, EventArgs e)
    {
    Application["SQLConnectionString"] =
    System.Configuration.ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString);
    }

    Then when it's needed again it can be fetched out of Application memory:
    connectionString =
    System.Web.HttpContext.Current.Application["SQLConnectionString"].ToString();

    This now presents me with two problems.

    1. I can't seem to find any way for the class library to work out if it's
    being used by a WinForms project or a WebForms project. I could include an
    extra parameter with all the class library's methods, I suppose, but at that
    point I may as well just pass the connection string anyway! Can anyone
    please tell me if a class library knows what type of project it is being
    referenced in? I have tried to find a way with Reflection but confess that
    that is a bit out of my league at the moment.

    2. When the class library is being used in a WebForms project it seems to be
    able to work with the main web app's Application cache only when running on
    my local development machine. When I deploy it to the test webserver it is
    unable to find it. Has anyone seen this behaviour before and knows what I've
    missed?

    Thank you.

    DJ
     
    David Jackson, Apr 29, 2009
    #1
    1. Advertising

  2. David Jackson

    sloan Guest

    Write a simple factory, encapsulate the logic.



    public static ISomeObject GetSomeObject( )
    {


    if ( null == System.Web.HttpContext.Current ) // Add a reference to
    System.Web.dll (in the BAL assembly most likely)
    { //Non Web Environment
    return new NonWebSomeObjectConcrete();
    }
    else
    { //Web Environment
    return new WebSomeObjectConcrete();
    }

    }
    }


    ISomeObject is the interface. Create a concrete one for the web
    environment, a different concrete version for the nonweb environment.


    You could just return a string I guess instead of ISomeObject.





    "David Jackson" <> wrote in message
    news:%23PC%...
    > Hello,
    >
    > Following advice from Gregory A. Beamer, I have written a C# class library
    > which contains several base classes to provide functionality such as
    > database access, mail, encryption and so on.
    >
    > This class library is intended to be reused unmodified in all subsequent
    > WinForms and WebForms development in Visual Studio.NET 2008 / C'#.
    >
    > For WinForms projects, I want the class library to fetch the database
    > ConnectionString from the main application's config file. No problem with
    > that:
    > connectionString =
    > System.Configuration.ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString);
    >
    > But, for WebForms, I want the class library to fetch it from the main web
    > app's Application cache. We're a small company and have only one database
    > so, to avoid opening and reading web.config every time we need to access
    > the database, we fetch the ConnectionString out of web.config in
    > Application_Start and store it as an Application variable:
    > protected void Application_Start(Object sender, EventArgs e)
    > {
    > Application["SQLConnectionString"] =
    > System.Configuration.ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString);
    > }
    >
    > Then when it's needed again it can be fetched out of Application memory:
    > connectionString =
    > System.Web.HttpContext.Current.Application["SQLConnectionString"].ToString();
    >
    > This now presents me with two problems.
    >
    > 1. I can't seem to find any way for the class library to work out if it's
    > being used by a WinForms project or a WebForms project. I could include an
    > extra parameter with all the class library's methods, I suppose, but at
    > that point I may as well just pass the connection string anyway! Can
    > anyone please tell me if a class library knows what type of project it is
    > being referenced in? I have tried to find a way with Reflection but
    > confess that that is a bit out of my league at the moment.
    >
    > 2. When the class library is being used in a WebForms project it seems to
    > be able to work with the main web app's Application cache only when
    > running on my local development machine. When I deploy it to the test
    > webserver it is unable to find it. Has anyone seen this behaviour before
    > and knows what I've missed?
    >
    > Thank you.
    >
    > DJ
     
    sloan, Apr 30, 2009
    #2
    1. Advertising

  3. David Jackson

    sloan Guest

    More information here:
    http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!126.entry



    "David Jackson" <> wrote in message
    news:%23PC%...
    > Hello,
    >
    > Following advice from Gregory A. Beamer, I have written a C# class library
    > which contains several base classes to provide functionality such as
    > database access, mail, encryption and so on.
    >
    > This class library is intended to be reused unmodified in all subsequent
    > WinForms and WebForms development in Visual Studio.NET 2008 / C'#.
    >
    > For WinForms projects, I want the class library to fetch the database
    > ConnectionString from the main application's config file. No problem with
    > that:
    > connectionString =
    > System.Configuration.ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString);
    >
    > But, for WebForms, I want the class library to fetch it from the main web
    > app's Application cache. We're a small company and have only one database
    > so, to avoid opening and reading web.config every time we need to access
    > the database, we fetch the ConnectionString out of web.config in
    > Application_Start and store it as an Application variable:
    > protected void Application_Start(Object sender, EventArgs e)
    > {
    > Application["SQLConnectionString"] =
    > System.Configuration.ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString);
    > }
    >
    > Then when it's needed again it can be fetched out of Application memory:
    > connectionString =
    > System.Web.HttpContext.Current.Application["SQLConnectionString"].ToString();
    >
    > This now presents me with two problems.
    >
    > 1. I can't seem to find any way for the class library to work out if it's
    > being used by a WinForms project or a WebForms project. I could include an
    > extra parameter with all the class library's methods, I suppose, but at
    > that point I may as well just pass the connection string anyway! Can
    > anyone please tell me if a class library knows what type of project it is
    > being referenced in? I have tried to find a way with Reflection but
    > confess that that is a bit out of my league at the moment.
    >
    > 2. When the class library is being used in a WebForms project it seems to
    > be able to work with the main web app's Application cache only when
    > running on my local development machine. When I deploy it to the test
    > webserver it is unable to find it. Has anyone seen this behaviour before
    > and knows what I've missed?
    >
    > Thank you.
    >
    > DJ
     
    sloan, Apr 30, 2009
    #3
  4. "sloan" <> wrote in message
    news:%...

    Hi Sloan,

    Thank you for replying.

    >> 2. When the class library is being used in a WebForms project it seems to
    >> be able to work with the main web app's Application cache only when
    >> running on my local development machine. When I deploy it to the test
    >> webserver it is unable to find it. Has anyone seen this behaviour before
    >> and knows what I've missed?


    > Write a simple factory, encapsulate the logic.
    >
    > if ( null == System.Web.HttpContext.Current )


    I am essentially doing exactly that. The problem is that it seems to work
    only when running in Visual Studio 2008 on my development machine but when I
    deploy it to the test server it doesn't. By that I mean that on the test
    server it ALWAYS thinks it's running in a WinForms project even when it's
    running in a WebForms project.

    Any ideas why this might be?

    DJ
     
    David Jackson, May 1, 2009
    #4
  5. David Jackson

    sloan Guest

    How are you deploying?

    Are you using MSBUILD?


    http://groups.google.com/group/microsoft.public.dotnet.framework.aspnet/msg/5c2634d01c5cf829

    Try that basic outline, you'll have to change to path from (my) 2.0 msbuild
    version to your (3.5) msbuild version of course.

    ..............

    Do you have an IIS virtual directory on the test server?




    "David Jackson" <> wrote in message
    news:...
    > "sloan" <> wrote in message
    > news:%...
    >
    > Hi Sloan,
    >
    > Thank you for replying.
    >
    >>> 2. When the class library is being used in a WebForms project it seems
    >>> to be able to work with the main web app's Application cache only when
    >>> running on my local development machine. When I deploy it to the test
    >>> webserver it is unable to find it. Has anyone seen this behaviour before
    >>> and knows what I've missed?

    >
    >> Write a simple factory, encapsulate the logic.
    >>
    >> if ( null == System.Web.HttpContext.Current )

    >
    > I am essentially doing exactly that. The problem is that it seems to work
    > only when running in Visual Studio 2008 on my development machine but when
    > I deploy it to the test server it doesn't. By that I mean that on the test
    > server it ALWAYS thinks it's running in a WinForms project even when it's
    > running in a WebForms project.
    >
    > Any ideas why this might be?
    >
    > DJ
     
    sloan, May 1, 2009
    #5
  6. David Jackson

    sloan Guest

    http://www.velocityreviews.com/forums/showpost.php?p=3686287&postcount=2

    Here is a slightly cleaner version.


    I have no idea if it'll clear up your issue. I just know making a "real"
    release build is less troublesome in multiple areas.





    "sloan" <> wrote in message
    news:OpHoy$...
    >
    > How are you deploying?
    >
    > Are you using MSBUILD?
    >
    >
    > http://groups.google.com/group/microsoft.public.dotnet.framework.aspnet/msg/5c2634d01c5cf829
    >
    > Try that basic outline, you'll have to change to path from (my) 2.0
    > msbuild version to your (3.5) msbuild version of course.
    >
    > .............
    >
    > Do you have an IIS virtual directory on the test server?
    >
    >
    >
    >
    > "David Jackson" <> wrote in message
    > news:...
    >> "sloan" <> wrote in message
    >> news:%...
    >>
    >> Hi Sloan,
    >>
    >> Thank you for replying.
    >>
    >>>> 2. When the class library is being used in a WebForms project it seems
    >>>> to be able to work with the main web app's Application cache only when
    >>>> running on my local development machine. When I deploy it to the test
    >>>> webserver it is unable to find it. Has anyone seen this behaviour
    >>>> before and knows what I've missed?

    >>
    >>> Write a simple factory, encapsulate the logic.
    >>>
    >>> if ( null == System.Web.HttpContext.Current )

    >>
    >> I am essentially doing exactly that. The problem is that it seems to work
    >> only when running in Visual Studio 2008 on my development machine but
    >> when I deploy it to the test server it doesn't. By that I mean that on
    >> the test server it ALWAYS thinks it's running in a WinForms project even
    >> when it's running in a WebForms project.
    >>
    >> Any ideas why this might be?
    >>
    >> DJ

    >
    >
     
    sloan, May 1, 2009
    #6
  7. "sloan" <> wrote in message
    news:OpHoy$...

    >>> Write a simple factory, encapsulate the logic.
    >>>
    >>> if ( null == System.Web.HttpContext.Current )

    >>
    >> I am essentially doing exactly that. The problem is that it seems to work
    >> only when running in Visual Studio 2008 on my development machine but
    >> when I deploy it to the test server it doesn't. By that I mean that on
    >> the test server it ALWAYS thinks it's running in a WinForms project even
    >> when it's running in a WebForms project.
    >>
    >> Any ideas why this might be?



    > How are you deploying?


    Web Deployment Projects.


    > Do you have an IIS virtual directory on the test server?


    Yes.
     
    David Jackson, May 4, 2009
    #7
  8. David Jackson

    Joe Guest

    is there some reason why you are afraid to read from the web.config?
    If you're worried about it being a performance issue you're probably
    way off base.
     
    Joe, May 4, 2009
    #8
  9. "Joe" <> wrote in message
    news:...

    > Is there some reason why you are afraid to read from the web.config?


    I'm not afraid of anything - what a strange question!

    > If you're worried about it being a performance issue you're probably
    > way off base.


    That's possible. Can you please expand on this, specifically about how
    reading a value out of web.config once and then caching it in Application
    memory is not more efficient than reading it out of web.config thousands and
    thousands of times?
     
    David Jackson, May 5, 2009
    #9
  10. David Jackson

    Norm Guest

    On May 5, 2:19 am, "David Jackson" <> wrote:
    > "Joe" <> wrote in message
    >
    > news:...
    >
    > > Is there some reason why you are afraid to read from the web.config?

    >
    > I'm not afraid of anything - what a strange question!
    >
    > > If you're worried about it being a  performance issue you're probably
    > > way off base.

    >
    > That's possible. Can you please expand on this, specifically about how
    > reading a value out of web.config once and then caching it in Application
    > memory is not more efficient than reading it out of web.config thousands and
    > thousands of times?


    Test it empirically:
    DEFAULT.ASPX
    <%@ Page Language="VB" AutoEventWireup="false"
    CodeFile="Default.aspx.vb" Inherits="_Default" Trace="true" %>

    DEFAULT.ASPX.VB
    Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Me.PreRender

    Application("test") =
    Web.Configuration.WebConfigurationManager.ConnectionStrings
    ("test").ConnectionString
    Dim Iterations As Integer = 99999
    Dim ApplicationTestArr(Iterations) As String
    Dim WebConfigTestArr(Iterations) As String

    Trace.Write("Application Test Start")
    For i As Integer = 0 To Iterations
    ApplicationTestArr(i) = Application("test")
    Next
    Trace.Write("Application Test End")

    Trace.Write("Web.config Test Start")
    For i As Integer = 0 To Iterations
    WebConfigTestArr(i) =
    Web.Configuration.WebConfigurationManager.ConnectionStrings
    ("test").ConnectionString
    Next
    Trace.Write("Web.config Test End")

    End Sub
    End Class

    WEB.CONFIG
    ....
    <connectionStrings>
    <add name="test"
    connectionString="datasource=test1;database=test2;integrated
    security=true;"/>
    </connectionStrings>
    ....

    In the trace you will see something like this:

    Message From First(s) From Last(s)
    ....
    Application Test Start 0.00154236 0.001313
    Application Test End 0.13982672 0.138284
    Web.config Test Start 0.1399284 0.000102
    Web.config Test End 0.40322772 0.263299
    ....

    The difference is about 13 tenths of a second over a hundred thousand
    iterations!
    AKA it takes approx. 1 thousandth of a thousandth of a second longer
    to read it from the web.config (.000001 seconds)

    I realize that this test may be somewhat simplistic, but its a start.

    - Norm

    Note: There is one advantage (that I can think of) to putting it in
    memory: You can change it without restarting the application. (Editing
    the web.config forces restart)
     
    Norm, May 7, 2009
    #10
  11. "Norm" <> wrote in message
    news:...

    Hi Norm,

    > AKA it takes approx. 1 thousandth of a thousandth of a second longer
    > to read it from the web.config (.000001 seconds)


    Thank you very much.

    DJ
     
    David Jackson, May 7, 2009
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin Rosén-Lidholm

    Productivity: WebForms vs. WinForms

    Martin Rosén-Lidholm, Jun 21, 2004, in forum: ASP .Net
    Replies:
    20
    Views:
    2,892
    Rob Nicholson
    Jul 8, 2004
  2. Phil Winstanley [Microsoft MVP ASP.NET]

    Re: Productivity: WebForms vs. WinForms

    Phil Winstanley [Microsoft MVP ASP.NET], Jun 21, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    558
    Phil Winstanley [Microsoft MVP ASP.NET]
    Jun 21, 2004
  3. news.austin.rr.com
    Replies:
    0
    Views:
    1,012
    news.austin.rr.com
    Apr 26, 2005
  4. sumit chawla
    Replies:
    1
    Views:
    542
    Thomas Matthews
    Jul 13, 2004
  5. Andrew

    Embedding WinForms in WebForms ?

    Andrew, Jun 3, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    226
    Andrew
    Jun 3, 2004
Loading...

Share This Page