Sharing code between ASP.NET application and Windows service app in the same solution

N

NWx

Hi,

I have a solution with two projects - an ASP.NET application, and a Windows
service

ASP.NET application was developed first, and for it I wrote some classes in
a Business logic layer

I want to share the same code with the Windows service, but if I drag the
BLL subfolder with all files form Web application project to Windows service
project, those files are actually copied to Windows service folder, So in
this case I'll have two copies of them and if I want to add some code, I
have to make changes in both places.

Is there any way to really "share" the source file between the two
applications? How?

And another issue

In those classes, I use SQL helper class to connect to database.
In Web application I pass the connection string, with a line as follows

myDS = SqlHelper.ExecuteDataset(

ConfigurationSettings.AppSettings(Global.CfgKeyConnString),

CommandType.StoredProcedure,

"MyMethod", ...)



But this uses a constant CfgKeyConnString, defined in ASP.NET Global
application object

The win service doesn't have this object. How can I solve this, to use
exactly the same code to retrieve different settings from configuration file

My thought is to create public class in Win service, assign it to a
variable named Global, and define some constants in that class.

Does anyone have a better thought?



Thank you
 
M

Martin Dechev

Hi, NWx,

Move the business logic layer code to a new Class Library project. Add
reference to this project in both the web project and the service project.
Change the method that uses the connection string to receive this string as
parameter. Then in the web project pass the stored value in the web.config.
In the service project either hard-code the value or create a .config file
for the service and store the value in it.

Hope this helps
Martin
 
J

John Timney \(Microsoft MVP\)

You could add the classes to a seperate dll and put it in the GAC, that
would make it accessible to both your code sets regardless of the project
using it.

--
Regards

John Timney
Microsoft Regional Director
Microsoft MVP
 
N

NWx

Hi,

I followed your advice, and I managed to use that DLL into my project (Web
app so far)
I already set the project dependences in Solution properties, and set build
order.

However, I have another problem.

If I make a change in library project source (for example I add a parameter
to a method of a class) and rebuild the whole solution, the changes are not
visible automatically in web application.
Even if I copy myself the new DLL from library project to Web app bin
folder, the changes are not visible

The only way to see them was to remove reference to DLL and add it again

There is something I did wrong?

I use VS.NET 2002

Thank you
 
N

NWx

Hi,
You could add the classes to a seperate dll and put it in the GAC, that
would make it accessible to both your code sets regardless of the project
using it.

I did this, as suggested in the other answer I got.
But what is GAC?

Thanks
 
M

Martin Dechev

Hi,

You should reference the project, not the output (the physical file .dll).

The GAC is the "global application cache" and "adding an assembly to the
GAC" makes this assembly accessible by any .NET application on the machine.
It is not recommended to do this at development stage though, because you
will have to remove and readd the assembly on every change. For more details
see:

http://msdn.microsoft.com/library/en-us/cpguide/html/cpconglobalassemblycache.asp

Hope this helps
Martin
 
M

Martin Dechev

Martin Dechev said:
Hi,

You should reference the project, not the output (the physical file .dll).

The GAC is the "global application cache"

Edit: GAC stands for "Global assembly cache". Still waking up...
 
J

John Timney \(Microsoft MVP\)

its the global assembly cache - the place that assemblies witha global
scope live

--
Regards

John Timney
Microsoft Regional Director
Microsoft MVP
 
N

NWx

Thank you for your answer.


John Timney (Microsoft MVP) said:
its the global assembly cache - the place that assemblies witha global
scope live

--
Regards

John Timney
Microsoft Regional Director
Microsoft MVP

code,
 
S

Shewmx

I have a followup question to this, since I'll have the same
requirements for a project I'll be starting soon.

Let's say I have an ASP app and a Windows service that both need to
access BLL objects X and Y. Now, X and Y may (depending on the
transaction) need access to BLL/DAL objects A, B and C. And this could
continue to recurse to other objects, so on and so forth.

So, is the recommendation to put my entire BLL/DAL layers into the
GAC, or is there something I'm missing that would make this not a
problem? Are there any other things I should take in to consideration?

Thanks,
Zk
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top