service control permission problem as the same code works in vb.net application

hi,

I have the following code for my webmethod to control the Print Spooler service although Im getting a webby permission problem because copying the code into a vb.net application works perfectly.

Any ideas what permissions I need to set?  Many thanks


    <WebMethod()> _
    Public Function test() As String
        Dim sc As ServiceController

        Try
            For Each sc In ServiceController.GetServices()
                If sc.DisplayName.Equals("Print Spooler") Then Exit For
            Next

            If sc.Status = ServiceControllerStatus.Stopped Then
                ' Start the service if its stopped

                Try
                    ' Start the service, and wait until its status is "Running".
                    sc.Start()
                    sc.WaitForStatus(ServiceControllerStatus.Running)
                Catch ex As Exception
                    Return "11111 " & ex.Message
                End Try

            End If

        Catch ex As Exception
            Return "22222 " & ex.Message
        End Try
    End Function
jimbona27Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GavinMannionCommented:
Which user is running the service?

I generally setup a service user which has the permissions required to access the file system and event logs and such.

An exact error message always helps :)
jimbona27Author Commented:
thanks, im not sure about the permissions but I know the print spooler service is running as a local system account.
GavinMannionCommented:
Check your service in the Services Console. Go into it's properties and you will see under whch account it is running. Change this to Local System and you should have more luck.

If not change it to your logon details and try again.

Else show us the error message because it might be a permissions problem somewhere else.
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

jimbona27Author Commented:
how can I setup the permission so it should work when depolying the site on another server?
GavinMannionCommented:
Wait I got confused... I thought you where writing a windows service,...

To get permissions to access a service that is running you need to use impersonation.

This is a bit more complicated.

Take a look at this article on how to impersonate a user
http://www.eggheadcafe.com/articles/20020418.asp

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jimbona27Author Commented:
will do.

can you inform me how to move a website to another location, i.e. copy the files onto another server?
then I'll test whether the impersonation works (after reading the url)

thanks,
GavinMannionCommented:
In asp.net v1.1 you just build the site for release and then copy the entire structure (excluding all .vb or .cs files) to the server.

In asp.net v2 you need to publish your site to a local folder and then copy the structure from there.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.