Windows Service with CSLA

Windows Service with CSLA

Old forum URL: forums.lhotka.net/forums/t/1569.aspx


rbellow posted on Monday, October 23, 2006

Are there any known issues when using CSLA in a Windows Service? I created a new Windows Service and made a project reference to my existing CSLA business objects. I continue to get a DataPortal.Fetch error. I hard coded the connection string in the object just to verify it was not an issue with the app.config. Am I missing something obvious? Any help would be greatly appreciated.

Bayu replied on Tuesday, October 24, 2006

What kind of Exception do you get? What message?

My guess is that it is a security issue. I believe that by default Windows services are run with the permissions of the LOCAL SERVICE account, which most probably has no access rights to your database.

If this is the case, then your can resolve this:
- alter the rights on your DB to allow access for LOCAL SERVICE user (this is NOT recommended)
- the recommended approach would to make your service run under a dedicated account name and grant DB access for this particular account.

Bayu

rbellow replied on Tuesday, October 24, 2006

As you know debugging a windows service is not easy. I wrapped the script in a try catch and had it email me the exception. The exception is the basic DataPortal.Fetch Failed error. I have tested the object in other projects and it works as expected.

The service is using the same connection string as the web project, so I am not sure how the permissions would be denied from the service.

I have tried LOCAL SERVICE and LOCAL SYSTEM as the Account and both have the same result. I also tried USER and supplied the Administrator login information, but it failed to install. At this point I don't know what else to try. Any ideas?

Michael Hildner replied on Tuesday, October 24, 2006

Check out the details of the DataPortalException. The DataPortalException.BusinessException will give you more details.

Mike

rbellow replied on Tuesday, October 24, 2006

Thanks that was very helpful. It appears that my service is attempting to reference my business object from the development server. When I made the references in the Windows Service project to my BO class library I set Copy :Local = True. Do I need to make each DLL an Embedded Resource for this to work?

It would seem to me that if Copy Local = True that this would be incorporated into the service. Am I missing something here?

Bayu replied on Tuesday, October 24, 2006

My guess is that copylocal=true should do the trick, but trial-and-error will give you the definite answer ...

Otherwise, the exceptions thrown will give you clues on how to proceed. Make sure you not only email yourself details about the top-most exception, but also (recursively) all inner-exceptions.

Bayu

rbellow replied on Tuesday, October 24, 2006

Ok. Thanks for your help. After a half day of trial and error and a little research I found the problem. I will explain in case any other novice windows service developers have the same problem.

1. By default the Windows Service does not include System.Configuration. Make a reference and you should be good. CSLA uses the following as a function to return the database name from your app.config file:

ConfigurationManager.AppSettings("DB:" & DatabaseName)

And this function requires System.Configuration. Make sure you have all the other necessary references as well.

2. You need to copy your app.config file and place it in the bin directory. You should rename it XXXXX.exe.config where XXXXX is the name of your service.

 

DansDreams replied on Friday, October 27, 2006

rbellow:

As you know debugging a windows service is not easy.

It's actually not that hard.  You just start the service and use Debug -> Attach to process from the menu.  The only challenge really is having enough delay in the service so you can attach to it fast enough to catch the bug.  :)

Copyright (c) Marimer LLC