Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,
I have one requirement sending a news letter for subscribed users for every month, when admin uploads a new pdf file from the admin site. Here subscribed users are nearly 5lacks users.
So, I created a windows service for background process. When admin uploads a new pdf, click Send button then invoking Window Service and sending mails to all the users by this mail service.
This is my code in web application
C#
private void StartServiceByName(string serviceName)
        {
            ServiceController serviceController = new ServiceController(serviceName);
            try
            {
                serviceController.MachineName = ConfigurationManager.AppSettings["ServerName"]; //this is my computer name "dt-corp-pms-04";
                serviceController.ServiceName = ConfigurationManager.AppSettings["ServiceName"]; //This is my Service name"Service1";
                serviceController.Start();
            }
            catch (Exception ex)
            {
                ExceptionLog objExceptionLog = new ExceptionLog();
                objExceptionLog.SaveException(ex.ToString(), ex.Message);
                if (serviceController.Status == ServiceControllerStatus.Running)
                    serviceController.Stop();
            }

        }

and in my web.config I added key also
<identity impersonate="true" />


This is working fine in my local machine when I am testing my web application, the windows service is running, and sending the mail to all the users.
But When I deployed this web application on my client server, I am getting the message Can not open "MyService" Service on computer "MyClientComputerName"
I hosted my web application and installed my windows service on my client's same machine.

Please help me.
Posted

The issue can arise if the server is in Shared Hosting environment. Please make sure that the application you have hosted is a Full Trust application. I believe interacting with the services is like a Admin task. Most, if not all hosting providers keep the application in the medium trust levels.

If you have a dedicated server, you need to configure an account which can enable and disable services and then provide its credentials in the identity tag like this:

XML
<identity impersonate="true" userName="accountname" password="password" />


On your local machine it works fine because most probably you are running Visual Studio with Administrator rights.
 
Share this answer
 
v2
Comments
saichaitanya.kumar 4-Jul-12 2:05am    
This is not working, i add username and password of my production server. but I am unable to get the solution
Pankaj Nikam 4-Jul-12 2:08am    
What exception or error are you getting?
saichaitanya.kumar 4-Jul-12 2:14am    
when I dont add <identity> key in web.config, the web application can not open the "Service" on the "ServerName",(it is working fine in my local mechine, but failed on the production)
if i add <identity> key in web.config with my production server username and passowrd, totally my web application gives yellow pages in live, giving red heighlet on the <identity> column.
Plz help me what i do, Can i add any other properties to serviceControler in web application?
Pankaj Nikam 4-Jul-12 2:18am    
When you dont add the keys, it is obvious that you get the exception.
I did not get the part where you mention that after you put the identity tag, what red highlight does it give? Can you post a screenshot of it?
saichaitanya.kumar 4-Jul-12 3:02am    
when i add identity key in web.config, i am getting the error , so that the reason I remove the identity key in web.config. Is it mandatory to run windows service through the web application?

Server Error in '/' Application.
Configuration Error
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.

Parser Error Message: Unrecognized attribute 'username'. Note that attribute names are case-sensitive.

Source Error:


Line 285: <!--For file upload controll to handle Files upto 18mb-->
Line 286: <httpruntime usefullyqualifiedredirecturl="true" maxrequestlength="1048576" executiontimeout="3600">
Line 287:<identity impersonate="true" username="rahim.k" password="ram">
Line 288: <!-- <trust level="Medium" originUrl=".*" />-->
Line 289: <trace enabled="true" requestLimit="10" pageOutput="false" traceMode="SortByTime" localOnly="true"/>


Source File: D:\HydWebsite\WebPages\web.config Line: 287

Version Information: Microsoft .NET Framework Version:2.0.50727.3634; ASP.NET Version:2.0.50727.3634

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900