Link to home
Start Free TrialLog in
Avatar of devo00
devo00Flag for United States of America

asked on

Central code library in ASP.NET

Using ASP.NET and C#:
I have multiple web projects, on a server, inside a network, that all need to reference the same classes. Some of these methods will be database routines and need to be secure (e.g. record insert methods, whatever). What is the best way to go about this?

Web services seemed to accomplish this with code in a central place, but they are not secure for data operations and add bloat from what I understand, and are also more for use with disparate systems across the cloud, not so much for internal compatible .NET applications.

Class libraries seem to be a possible answer and I see simple tutorials on this on the web for console apps, but not for web applications (so far, I only did a cursory search). If I go this route, does each web application reference one piece of code in one place, or do they all make a copy when the library is referenced? If the latter, do I have to go update every single application every time I add a new class, method, etc or change an existing one?

Many thanks for the assistance.

Avatar of rashmi_vaghela
rashmi_vaghela
Flag of India image

You could go the web service route and require a "password" parameter to help secure it, but web services seem to slow things down in web apps (at least in my experience).

My thought is that you have one Class library project.  Add that library project to all of your solutions.  That way, whenever you compile it will compile the latest version of the class project.  When you change the class library project, you'll have to recompile your other projects and upload each project to their respective bin folders, but since you can set a reference to the project instead of just a DLL you won't have to remove the reference, copy the DLL, and add the reference back in for each project.

Even if you went the web service route, you would still have the update the reference in each project whenever you updated the web service.
Avatar of devo00

ASKER

Thanks for your input!
I agree, we have learned that web services are inappropriate for this situation. -Even if they asked for a password, that would be transmitted in the clear I believe, not to mention the extra size / conversion time when data is changed to / from XML.
So:
- Our uses are mostly for web applications, does that change anything?
- When I add the reference prior to publication and the class sits on the web server, can I still point / browse to it?
- Do I have to recompile all apps when any change is made to the library (new methods, etc), or only when methods used by a specific web app are updated? (With web services the latter is the case I believe.)
Thank you!

ASKER CERTIFIED SOLUTION
Avatar of quizwedge
quizwedge
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of devo00

ASKER

Extremely helpful, thank you! I'm sure I'll have additional questions if you don't mind.
Thanks for the points.  I don't mind follow up questions.
Avatar of devo00

ASKER

Thanks I had to figure out that only console, libraries or windows apps can be a 'project'. All my items so far are web sites that apparently each sit under a solution. I did see where you could reference a project rather than the DLL, but my list was empty (all web sites so far).

So will I have to re-reference the library every time I compile the web code as I did with web services?

Get some sleep! :)

Shoot... I just realized something.  When I do ASP.NET development I use the "web application" template which allows for more than one project in a solution.  You're probably using the website template.  Here is an article on the difference between "web applications" and websites: http://vishaljoshi.blogspot.com/2009/08/web-application-project-vs-web-site.html  Would you be opposed to switching to a web application project?  Instructions to convert can be found at http://webproject.scottgu.com/CSharp/Migration2/Migration2.aspx

If you can't / don't want to, I don't think my solution will work because website projects don't appear to allow more than one project to load at a time.  Web application projects allow multiple projects as part of a solution.

If you do need to keep it as a website, I think the best you can do is have a library project and then on each of your websites browse for the DLL in the bin folder of the library project.  You'll have to compile your library project first.  In theory, when you compile your website, it may grab whatever is the latest compiled version of the DLL.  I'm not sure on that though, so you may want to check it with some small test code before you go fully forward with this idea.

Sorry... I'm so used to using web application projects that I forget there is a website project.
Avatar of devo00

ASKER

Hey thanks for the links! (I know your job is done. :)

Hmm, I'm not sure, I usually just say 'New Web SIte', then an 'ASP.NET Web Site' template, which ends up under a new default solution of some sort. The Project / web site / solution relationship seems nebulous to me still.....

I use a C# AJAX enabled template, at work and the attached screenshots don't show that, but you get the idea.

-I guess you select 'New Project', then 'ASP.NET Web Application' template? Should I be doing this for web applications?

Thanks again, this is very helpful!

new-web-site.png
Avatar of devo00

ASKER

Sorry, missed the second attachment.
web-site-template.png
That's correct.  Instead of New Website, you choose New Project and then ASP.NET Web Application.  I like the Web Application project type, but then that's what I started with and I typically build web software instead of web sites.  I can see the benefits of the Web Site project type, but it seems like for anything that needs more advance behind the scenes stuff (such as a shared class like you're looking to do) that the Web Application lends itself better.

A project holds one or more files and lets Visual Studio know what files are part of your web site / web application / windows application / etc.  A solution holds one or more projects.  So, if you're building a web site project you'll have a solution that has one web site project and doesn't appear to be able to have any more projects.  If you're building a web application project, you'll have a solution that starts out with one project, but you can add others such as the shared code mentioned above.

Hopefully that clears things up.  Feel free to ask if you need anything clarified.
Avatar of devo00

ASKER

Excellent, thank you again for taking the time to expand on all of this! (I hope you got some sleep, have a great weekend!)