Click here to Skip to main content
11,430,908 members (69,524 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Windows service
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
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 3-Jul-12 1:28am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:

<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.
  Permalink  
v2
Comments
saichaitanya.kumar at 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 at 4-Jul-12 2:08am
   
What exception or error are you getting?
saichaitanya.kumar at 4-Jul-12 2:14am
   
when I dont add 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 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 column.
Plz help me what i do, Can i add any other properties to serviceControler in web application?
Pankaj Nikam at 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 at 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:
Line 287:
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
saichaitanya.kumar at 4-Jul-12 3:05am
   
Line 287:

here I am getting red color heighlet
Pankaj Nikam at 4-Jul-12 3:23am
   
Check out the updated solution. It did not had the proper attribute, dont know how it changed the case. Do mark it as answer if it works else reply to this comment.
saichaitanya.kumar at 4-Jul-12 3:04am
   
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:
Line 287:
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)



Advertise | Privacy | Mobile
Web02 | 2.8.150428.2 | Last Updated 4 Jul 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100