how can i use svchost.exe to host my own services? - c#

I have written a service in C#. I kept the DLLs of my service in c:\windows\system32\myservice.dll. I have done the necessary registry changes for hosting in svchost.exe, but my service is not being executed. It gets a 1053 error code.
The following is the registry entry for myservice. I created a key and have given the path of the myservice.dll file.
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\svcsvc2]
"Type"=dword:00000010
"Start"=dword:00000002
"ErrorControl"=dword:00000001
"ImagePath"=hex(2):25,00,53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,6f,00,\
74,00,25,00,5c,00,73,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,5c,00,73,\
00,76,00,63,00,68,00,6f,00,73,00,74,00,2e,00,65,00,78,00,65,00,20,00,2d,00,\
6b,00,20,00,4c,00,6f,00,63,00,61,00,6c,00,53,00,65,00,72,00,76,00,69,00,63,\
00,65,00,00,00
"DisplayName"="#%SystemRoot%\\system32\\svcsvc.dll,-200"
"ObjectName"="NT AUTHORITY\\LocalService"
"ServiceSidType"=dword:00000001
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\svcsvc2\Parameter]
"ServiceDll"=hex(2):25,00,53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,6f,\
00,74,00,25,00,5c,00,53,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,5c,00,\
73,00,76,00,63,00,73,00,76,00,63,00,2e,00,64,00,6c,00,6c,00,00,00
"ServiceMain"="Main"

1053 is ERROR_SERVICE_REQUEST_TIMEOUT
The service did not respond to the start or control request in a
timely fashion.
StartServiceCtrlDispatcher was not called?
You also have type set to SERVICE_WIN32_OWN_PROCESS, not SERVICE_WIN32_SHARE_PROCESS.
And please respect this note from MSDN:
Note that Svchost.exe is reserved for use by the operating system and
should not be used by non-Windows services. Instead, developers should
implement their own service hosting programs.

svchost doesn't host the .NET framework, so is not able to execute your .NET assembly.
.NET services run with their own applications (.exe).

Why not creating a Setup Project in VS that installs your service?
Unless you want to install a service programatically, I would advise you to read this article.

Related

Topshelf and .net core under linux

I have a simple application that starts as a service using topshelf and it looks simple:
HostFactory.Run(x =>
{
x.Service<RequestService>();
x.RunAsLocalSystem();
});
Well it works, but under windows. When I tried this under Linux I am getting:
Topshelf.Runtime.Windows.WindowsHostEnvironment Error: 0 : Unable to get parent process (ignored), System.DllNotFoundException: Unable to load shared library 'kernel32.dll' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: libkernel32.dll: cannot open shared object file: No such file or directory
Has someone came across this problem?
I tried to google it but someone said it works other that it is tool only for windows.
Or maybe there is some other service hoisting framework for .net core?
Topshelf is not advertised as cross-platform and so it does not (or did not at the time of writing) official support .Net Core on non-Windows environments, even if it can run in them (at least at the time of writing, see below).
The solution is to change the environment builder when running on non-Windows hosts.
Here is an example from my project. When creating the service, pick the env builder at runtime based on the host OS.
HostFactory.Run(c =>
{
// Change Topshelf's environment builder on non-Windows hosts:
if (
RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ||
RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
)
{
c.UseEnvironmentBuilder(
target => new DotNetCoreEnvironmentBuilder(target)
);
}
c.SetServiceName("SelloutReportingService");
c.SetDisplayName("Sellout Reporting Service");
c.SetDescription(
"A reporting service that does something...");
c.StartAutomatically();
c.RunAsNetworkService();
c.EnableServiceRecovery(
a => a.RestartService(TimeSpan.FromSeconds(60))
);
c.StartAutomatically();
c.Service<SelloutReportingService>();
});
Assuming that you installed this version of Topshelf - you would notice under dependencies that it doesn't support .NET Core and therefore it will not run under a Linux environment.
It will only run under a Windows environment as you mentioned in your post. kernel32.dll is a Windows dependency that it cannot find, therefore it cannot run.

Initializing an external COM object causes w3wp.exe to crash with "Access Violation" error

I'm trying to implement Brother b-Pac printing library for label printing devices into my web application. I need to create/load the label template on server and print from the client's machine over a browser.
My code is totally working on my development environment. It only occurs on the published version. It's an access denied error thrown when the COM object got called by the app. I suspect there's an inner exception I couldn't reach.
The event got logged as follows:
Faulting application name: w3wp.exe, version: 8.5.9600.16384
Faulting module name: bpac.dll, version: 3.2.0.20
Exception code: 0xc0000005
Fault offset: 0x0010beea
Faulting application path: C:\Windows\SysWOW64\inetsrv\w3wp.exe
Faulting module path: C:\Program Files (x86)\Common Files\Brother\b-PAC\bpac.dll
I also used DebugDiag on the dump file and here's the report. Though it says here debugger couldn't locate debug symbols for bpac.dll, so it may be incomplete (If it may help, I'd like to know where and how to get symbols for a 3rd party DLL)
Application is a multilayer ASP.NET MVC app targeting the .NET Framework 4.6.1, running on a virtual machine with Windows Server 2012 R2 (x64) and IIS 8. (If it matters the project is derived from this template)
I searched about the issue and tried the following solutions:
Give read/write permissions of wwwroot and DLL's own folder to the IIS APPPOOL/user of the faulting w3wp.exe.
Give same permissions to IIS_IUSRS, NETWORK, NETWORK SERVICE users.
Impersonate Administrator user.
Uninstall/install 32bit/64bit versions of the b-Pac SDK.
Change bitness of my ASP app and all projects within to x86/x64.
Enable 32bit on the Application Pool.
Patch the Windows for July 2018 update issue
Setting Anonymous Authentication, App pool identity etc. all to the default pool user.
Loading the DLL dynamically. (not sure if I did this correctly though)
The COM object I'm trying to initialize is bpac.Document. Interface is IDocument and the concrete class is DocumentClass. I wasn't familiar with any of these concepts before. And I'm still struggling to understand but I tried all of these following lines for initialization:
Document label = new Document();
DocumentClass label = new DocumentClass(); //embed interop types: false
IDocument label = new Document();
After that, I'm using label object to open a template and etc. But as soon as the application hits any of the above lines it crashes the server and restarts the app.
Incidentally, the DLL file is referenced as a COM library in my WebService (App) project (not in the Web project). So it doesn't get copied on bin folder (I guess that's the correct behaviour?). Tried changing isolation/interop settings but I'm not sure if those are related to the issue at hand.
I read things about Registering COM objects into GAC and Marshalling for Remote Access but I couldn't grasp how to apply these properly. Thing is, the official documentation doesn't say anything about this. And there isn't a single example for using b-Pac library on a modern C# ASP.NET setting (only a little on VB ~eww~).
I'm a developer, not a DBA so I'm not too confident with tweaking the server's settings. But for this issue alone, I modified too many things too many times.
So, the problem was about the app id permissions all along. I changed the Application Pool's identity to LocalSystem and now it works as expected.
Still, I'm not sure what kind of security flaws this change would cause in the future. I posted this link to the vendor's technical staff. But they didn't give me an answer yet.

Calling Web Service in a Windows Service

I've used a simple windows service to make a method work in specific time and it works fine. Following that I've already tried:
protected override void OnStart(string[] args)
{
this.WriteToFile("Simple Service started {0}");
this.ScheduleService();
}
protected override void OnStop()
{
this.WriteToFile("Simple Service stopped {0}");
this.Schedular.Dispose();
}
private Timer Schedular;
public void ScheduleService()
{
try
{
Schedular = new Timer(new TimerCallback(SchedularCallback));
string mode = ConfigurationManager.AppSettings["Mode"].ToUpper();
this.WriteToFile("Simple Service Mode: " + mode + " {0}");
//Rest of the code here
}
catch(Exception ex)
{
WriteToFile("Simple Service Error on: {0} " + ex.Message + ex.StackTrace);
//Stop the Windows Service.
using (System.ServiceProcess.ServiceController serviceController = new System.ServiceProcess.ServiceController("SimpleService"))
{
serviceController.Stop();
}
}
}
This is done in a simple windows application. So what I am trying to do is to call a web service (A specific method to operate in a specific time) in a windows service. The application I am building is web-based and am little bit confused how would I integrate the windows service into it? Do I need any alternatives or any suggestions would be appreciated.
Note: What I would like to know is it required to create another project for windows service in the web application or any other way to implement?
To call a web service from a Windows Service application, you would first generate a DLL from that web service, then instantiate its namespace. Assuming you have the code for that web service and/or know its namespace, you can perform these commands to do this:
Perform these lines on a command line:
cd C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools
wsdl /l:CS /protocol:SOAP %svc%?WSDL
where %svc% is the URL for your web service, i.e. http://localhost:777/MyWebService.asmx
If the code is in VB instead of C#, change /l:CS to /l:VB.
This will output a proxy class file that can be converted to a DLL.
Move the MyWebService.cs file from C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools to the C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\ directory.
Run these two commands on the command line:
cd C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
csc /t:library %name%.cs /reference:System.Web.Services.dll /optimize
where %name% is the name of the class (without the .cs, since the command will append this). In our case, we'd use MyWebService. (Change .cs to .vb for a VB class.)
Navigate to C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 via Windows Explorer. You should see a DLL created in that folder with the name of the class (MyWebService.dll). Copy this file to the bin folder of your Service project. You will need to set the bin folder to be included in your project, and right-click the folder to Add > Existing Item. Select the DLL. Once imported, select the DLL and change its properties to:
Build Action: Content
Copy to Output Directory: Copy if newer (or Copy always, as you prefer)
Right-click References > Add References. Navigate to the DLL in the bin folder for your web service.
Right-click References > Add Service References. Assuming your web service is running, take its full URL (i.e. http://localhost:777/MyWebService.asmx) and put that on the Address line. In the Namespace textbox, give it something more meaningful than ServiceReference1, but it should not be the same as MyWebService (the name/namespace of the ASMX file). Perhaps MWS.
Instantiate your web service in your Windows Service:
MWS.MyWebServiceSoapClient webService = new MWS.MyWebServiceSoapClient();
webService.Open();
string someDataYouWant = webService.SomeMethodToGetData();
webService.Close();
Or you can probably do:
MyWebService webService = new MyWebService();
string someDataYouWant = webService.SomeMethodToGetData();
webService.Dispose();
In answer to your query on my comment;
Another approach is to use an IIS Auto-Start website contaning your Windows Service logic. The IIS Auto-start is supierior to using a Windows Service as it contains all the IIS application hosting logic including auto-restart, and aggressive resource management. A poorly written Windows Service can take down a Server but it takes a lot for an ASP.net IIS hosted application to take down its host (its almost impossible).
Your Auto-Start website need not be visibile to the outside world - it just needs to have an internal timer that keeps it alive when it starts up. Note that the web application might be started and stopped by IIS for various reasons; but the outcome is that it will be running whenever your other web service application is running. The internal timer can wait for a specific time to execute the logic you need to call your second web service.
The key thing to remember is that a Windows Service is designed to be an application that is hosted by Windows and is continually running. An IIS application is designed to be run by Windows but runs only when called. The IIS Auto-Start website concept allows you to provide a "continually running" website but hosted by the robust IIS application hosting components, instead of it running directly as an OS process.
Generally people dont do this because either they dont know about it, or want to avoid needing the IIS infrastructure to run "Windows Service" type applications, but in your case you have already paid the cost of using IIS to host your second web service, so you may as well make full use of IIS (and avoid the second technology stack and deployment headaches of Windows Service deployment).
So I suggest using an IIS Auto Start in preference to a Windows Service in your situation because;
You only need to use on tech stack in your solution, which was what your OP was asking about
IIS carries out active resource management on all its applications, terminating, restarting as neccessary if they become non-functional. Windows Services do not have that capability.
Your IIS based service code is XCOPY deployable with no administrator access credentials on the target machine.
Your IIS service is hot upgradeable without needing OS level administrator rights - IIS handles the stopping and restarting on upgrade without you needing to do anything.

StaticCompressionModule and DynamicCompressionModule on 32bit IIS and 64bit Windows

I want to host a 32bit application in IIS 7 on 64 bit Windows 2008. When I visit the site with the default modules enabled I get this error -
HTTP Error 500.19 - Internal Server Error
The requested page cannot be accessed because the related configuration data for the page is invalid.
Module: DynamicCompressionModule
Notification: SendResponse
Handler: StaticFile
Error Code: 0x8007007e
If I remove the StaticCompressionModule and DynamicCompressionModule the site works.
Can I get it working without having to disable these modules?
Microsoft KB says
This problem occurs because the ApplicationHost.config file or the Web.config file references a module or a DLL that is invalid or that does not exist.
Try enabling 32bit application in Application Pool configuration
EDIT:
Found the folowing
For above specific error (mentioned in this example), DynamicCompressionModule module is causing the trouble. This is because of the XPress compression scheme module (suscomp.dll) which gets installed with WSUS. Since Compression schemes are defined globally and try to load in every application Pool, it will result in this error when 64bit version of suscomp.dll attempts to load in an application pool which is running in 32bit mode.
This module entry looks like:
Hence to get rid of this problem:
Ø Remove/Disable the XPress compression scheme from the configuration using the command below:
%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/httpCompression /-[name='xpress']
OR
Ø Use a 32bit version of suscomp.dll
In the end I couldn't get it to work without disabling these two modules. It's worth noting that if you're using Visual Studio to deploy your application to an IIS server, your applications config is going to overwrite what is on the server and thus, reenable the two modules. You need to disable the modules in your config file.

Installing service using SC/ service control

I had a windows service installed on my computer. I deleted it with
sc delete myservice
Then I recreated it with
sc create collector binpath = "path of service"
but service is not listed under services in control panel.
I tried to recreate the service and got:
[SC] CreateService FAILED 1073:
The specified service already exists.
I am trying to delete the service and getting:
[SC] OpenService FAILED 1060:
The specified service does not exist as an installed service.
What is causing the problem and how do I solve it?
Try using installutil instead, this fixed similar problem for me.
I've had issues like that installing/uninstalling services before and often I found that I had the Services MMC open while trying command line operations. Closing it out and attempting the install/uninstall again has worked for me in the past. Not sure if that will aid you but it's something that's helped me many times.

Categories

Resources