none
share a web app's App_Code class with a console app? RRS feed

  • Question

  • This is something I've had difficulty finding any good reading/reference material on...  We have a growing web app, all based on ASP.NET 2.0, C#, SQL Server 2005, etc.  Nothing new there.  Since we were (and still are) in somewhat of a start-up mode, we use the App_Code directory for our general classes that are used by various pages throughout the web site.

     

    No surprise, we also need to write some "utility" apps, aka console apps, to do various batch processes and what not.  Of course, I'd like to use the same business objects and other classes that have been developed.

     

    Is there any guidance on how to the share code in our web app (in the App_Code directory) with a console app?  That is, w/out making a copy of the code files and duplicating them into the Console app project?  I have the same question about ConnectionStrings and other "settings" found in the web.config of the web app... It's not entirely clear to me if I can move those into the machine.config or if there is another trick to be had.

     

    Any info, pointers to other reference material, etc.  would be appreciated.

     

    -- Margo Noreen

     

    Friday, July 6, 2007 1:39 PM

Answers

All replies

  • Add a 2nd project to your web app.  Make it a class library.  Move all the shared code into the class library.  Add a project reference from the web to the class library project.

     

    After you compile, you can grab the dll from the class library and reference that from your console apps.

     

    Evan

    Friday, July 6, 2007 4:07 PM
  • As far as the connection strings go, you might look at putting the connection string into the registry.  Given that the registry can be secured, you can both reuse the string across applications on the same machine and you can use windows to protect that string from prying eyes.
    Friday, July 6, 2007 4:08 PM
  • Here's a good article about putting the connection string in the registry:

    http://msdn2.microsoft.com/En-US/library/aa302406.aspx

    Friday, July 6, 2007 4:10 PM
  • By default asp.net runs under account that does not has access to the registry.

     

    Regarding author's question:

    As mentioned above refactor your App_Code code to the new project. Make configuration sections, so you will be able to configure your web application with web.config and console with app.config.

    Friday, July 6, 2007 5:31 PM
  •  Mike Chaliy wrote:

    By default asp.net runs under account that does not has access to the registry.

     

    Regarding author's question:

    As mentioned above refactor your App_Code code to the new project. Make configuration sections, so you will be able to configure your web application with web.config and console with app.config.

     

    That works but then the settings aren't shared across the asp.net/console apps.  The registry is a good place to centralize configuration info.

    Friday, July 6, 2007 9:51 PM
  • Thanks for the replies.   I pretty much expected the "refactor" answer (taking things out of App_Code and compiling them to standalone DLL's).  And, I can accept that and will will begin to work that way, but here's the real major stumbling block I'm wrestling with.

     

    Instead of the using the Registry, as mentioned, we were thinking of just using Machine.config.  (I like to avoid the Registery at all possible costs just from past history!)  And, in .NET 2.0, you can encrypt the connection strings section if that's a requirement (not for me, but I could see where it would be for others).

     

    What I can't fathom is why the API to read a "config" file, whether its web.config in a web app or an app.config in a console app, is different.  That almost makes it impossible to write a standalone biz object / DLL that could be used be either my web app or a console app if that general class needs to read a configuration setting from the appropriate context and return it to the calling app.

     

    For a web app, I have to use "System.Web.Configuration", but for my console app, I have to use "System.Configuration". 

     

    So, let's say I create a standard business class object that returns some sort of piece of information from a config file.  How can I possibly compile the business object when there are 2 completely different ways at getting at this, it just depends on context?

     

    Any ideas?

    Monday, July 9, 2007 6:18 PM
  • Use the System.Configuration.ConfigurationManager class for both.  Make sure you reference System.Configuration.

    http://msdn2.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx

     

    Specifically, notice the AppSettings and ConnectionStrings properties.

     

    This was new in 2.0.

     

    Evan

    Tuesday, July 10, 2007 3:56 AM