2

I have an asp.net project with is created using layered architecture (Presentation, Business and Data Access layer). Now I need to add a windows service to the project which will do a background process. The user can logout from the site, but the service will run in background. It may take hours to finish he task. I have the following questions.

  1. Can I include this windows service as a separate project in my asp.net web application? If so where it should be added? This windows service may call from the Business layer. And the service will interact to database. SO can I add it as a separate Service layer?

  2. As I said the windows service will work in background, so I can create thread inside 'OnStart' event of windows service to do the operation. Am I correct? I know that it’s not a good practice to write long running process in 'OnStart' event. That’s why I am using threads. But if another user login to the website, the service will again call and it will create new worker thread. Is there any performance issue in this approach?

Thanks.

Andrea
  • 11,801
  • 17
  • 65
  • 72
Dev
  • 309
  • 1
  • 8
  • 24

2 Answers2

1

Personally I'd put the service in it's own solution. I'd put a client in the web solution. Mainly because deployment will be radically different, and to help keep the service API uncorrupted.

You haven't said enough about what the service is going to do, but I'd expect.

When a user logins, a notification to be sent to the service. Login(user_Id)

OnStart in the service should kick off a Listener thread

Then when the service "hears" a login(User_id) notification it does something like

if (LogInTaskThread == null)
{
  LoginTaskThread = new Task("loginTask");
}

That sort of thing anyway...

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39
1

If the Windows Service is running in the background and does not serve any request from external callers, then you can treat your Windows Service application as a form of UI-less Presentation layer. It should be placed in a separate project and communicate with the business and data layer like any application.

However, if your Windows Service is serving calls from external programs, then you may need to treat you Windows Service as a Service Layer.

You may want to get a clearer picture of the layers from here http://serena-yeoh.blogspot.com/2013/06/layered-architecture-for-net.html