Cannot install windows service - c#

I have created a very simple window service using visual studio 2010 and .NُET 4.0.
This service has no functionality added from the default windows service project, other than an installer has been added.
If I run "installutil.exe appName.exe" on my dev box or other windows 2008 R2 machines in our domain the windows service installs without issue.
When I try to do this same thing on our customer site, it fails to install with the following error.
Microsoft (R) .NET Framework Installation utility Version 4.0.30319.1
Copyright (c) Microsoft Corporation. All rights reserved.
Exception occurred while initializing the installation:
System.IO.FileLoadException: Could not load file or assembly 'file:///C:\TestService\WindowsService1.exe' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515).
This solution has only 1 project and no dependencies added.
I have tried it on multiple machines in our environment and two in our customers. The machines are all windows 2008 R2, both fresh installs. One machine has just .net 2.0 and .net 4.0. The other .net 2, 3, 3.5 and 4.
I am a local admin on each of the machines.
I have also tried the 64bit installer but get the following error, so I think the 32 bit one is the one to use.
System.BadImageFormatException
Any guidance would be appreciated. Thanks.

Another reason for this error might be that you copied your program from a source which windows considers untrusted.
You can unblock the assembly by clicking "unblock" after you right-click and select properties on the file.

Need to unblock files. or at least that's what fixed mine.
To do 'bulk' unblock in Powershell
get-childitem *.* | Unblock-File
or in DOS:
FOR %a in (*.*) do (echo.>%a:Zone.Identifier)

When we copy executable from another computer, Windows mark a flag on them for protection. Right Click on executable and in properties Click Unblock. It would clear flag and service executable would Install.

This issue came about for me because I was trying to install the service from a network location. By copying the service.exe to the local machine prior to using installutil, it fixed my problem and the service installed successfully.

don't forget to vote up...
Try the SC as follows:
1. open command line
2. write the below-
sc create ServiceName BinPath= "ExePath".
report on results...

I also faced the same issue. In my case I was deploying new version of utilities , i copied new exe , installog files and then trying to uninstall the utils.
I copied back the old version , uninstal the utils , copied the new version and installed it again. Anyway it is the logical sequence I should have followed in first place.

i have this issue and it for different between of .net version of util and my service
i use util for .net 2 and my service build with .net4

In my case this error was caused by accidentally passing the directory containing the service to InstallUtil instead of passing the service exe.
Obviously human error but I found the error message quite misleading.

you can use this command in developer command prompt run administrator
installutil C:\...\MyService\bin\Debug\MyService.exe

As per #doublehelix's response above, this answer put me on track for my particular issue which related to a service issue running in Windows XP (Unblock is not an option in the security tab that I could find). In my case I was attempting to run a service from a separate drive (i.e. not on the same drive as Windows and not under program files) although the actual physical disk was the same. Moving my service into the Program Files folder solved my underlying issue. (I intended to 'comment' against #doublehelix's answer but I don't seem to be able to in my current status).

Simply right click on your service files and select properties and then check the unblock checkbox
https://brianseekford.azurewebsites.net/2011/07/13/hresult-0x80131515-when-running-installutil-to-install-a-net-service/

Unblocking files did the job here. Weird thing is that the blocked files came from a signed installer run with admin privileges. Had to unblock several files in several folders at once.
I used the following registry tweaks to add context menus to a folder or file when Shift + right-click is pressed (it doesn't show up if just right-clicked).
Credits and references to the author in each code block.
Folder tweak:
Just save it as ShiftUnblockFolder.reg and import into Registry.
Windows Registry Editor Version 5.00
; Created by: Shawn Brink
; Created on: March 17th 2019
; Tutorial: https://www.tenforums.com/tutorials/129101-add-unblock-file-context-menu-windows-10-a.html
[HKCR\Directory\shell\unblock]
"MUIVerb"="Unblock"
"Extended"=""
"SubCommands"=""
[HKCR\Directory\shell\unblock\shell\001flyout]
"MUIVerb"="Unblock files only in this folder"
[HKCR\Directory\shell\unblock\shell\001flyout\command]
#="powershell.exe get-childitem -LiteralPath '%L' | Unblock-File"
[HKCR\Directory\shell\unblock\shell\002flyout]
"MUIVerb"="Unblock files in this folder and subfolders"
[HKCR\Directory\shell\unblock\shell\002flyout\command]
#="powershell.exe get-childitem -LiteralPath '%L' -recurse | Unblock-File"
Single file tweak:
ShiftUnblockFile.reg
Windows Registry Editor Version 5.00
; Created by: Shawn Brink
; Created on: March 17th 2019
; Tutorial: https://www.tenforums.com/tutorials/129101-add-unblock-file-context-menu-windows-10-a.html
[HKCR\*\shell\unblock]
"MUIVerb"="Unblock"
"Extended"=""
[HKCR\*\shell\unblock\command]
#="powershell.exe Unblock-File -LiteralPath '%L'"
[HKCR\Directory\shell\unblock]
"MUIVerb"="Unblock"
"Extended"=""
"SubCommands"=""
[HKCR\Directory\shell\unblock\shell\001flyout]
"MUIVerb"="Unblock files only in this folder"
[HKCR\Directory\shell\unblock\shell\001flyout\command]
#="powershell.exe get-childitem -LiteralPath '%L' | Unblock-File"
[HKCR\Directory\shell\unblock\shell\002flyout]
"MUIVerb"="Unblock files in this folder and subfolders"
[HKCR\Directory\shell\unblock\shell\002flyout\command]
#="powershell.exe get-childitem -LiteralPath '%L' -recurse | Unblock-File"

You can try to make a Setup Project for your service and run the MSI file on that server.

Related

Dotnet: Works in VSCode's OWN TERMINAL, but OMNISHARP: 'dotnet.exe' is not recognized

How is this possible?
I installed .net sdk 6.0.405: dotnet-sdk-6.0.405-win-x64.exe
I can in windows terminal and power shell run: dotnet --version: 6.0.405 ✅
I CAN ALSO IN VSCODE IN ITS BUILTIN TERMINAL, run: dotnet --version: 6.0.405 ✅✅
But in the OUTPUT tab, choosing OmniSharp Log from the dropdown, it says:
[ERROR] A .NET 6 SDK for x86_64 was not found. Please install the latest x86_64 SDK from ...
I switched vscode explorer settings to use external terminal.
And this terminal setting to command prompt:
Terminal › Integrated › Default Profile: Windows
The default profile used on Windows. This setting will currently be ignored if either terminal.integrated.shell.windows or terminal.integrated.shellArgs.windows are set.
I have dotnet in the PATH (and it works in terminals and in vscode's terminal)
I restarted vscode, AND REINSTALLED the dotnettools "C# for Visual Studio Code (powered by OmniSharp)" extension. I went to the online store and from the right column under Resources I downloaded the ms-dotnettools.csharp-1.25.2#win32-x64.vsix to make sure I get the right one. And restarted vscode.
I tried this setting:
Omnisharp: Dotnet Path
Specified the path to a dotnet installation to use when "useModernNet" is set to true, instead of the default system one. This only influences the dotnet installation to use for hosting Omnisharp itself. Example: "/home/username/mycustomdotnetdirectory".
C:/Program Files/dotnet/sdk/6.0.405
And this setting:
Omnisharp: Sdk Path
Specifies the path to a .NET SDK installation to use for project loading instead of the highest version installed. Applies when "useModernNet" is set to true. Example: /home/username/dotnet/sdks/6.0.300.
C:/Program Files/dotnet/sdk/6.0.405
And restarted vscode. And now it says:
[ERROR] Error: Command failed: dotnet.exe --version
'dotnet.exe' is not recognized as an internal or external command,
operable program or batch file.
What? W H A T? lol
What could be wrong? Why isn't it saying where it expected to find it and what to do to locate it? Is there a setting to specify the dotnet.exe path to this C# extension? Does it need always-on internet or something? Does it have DRM? Does it need Edge installed? Wtf?🙂
🙃😭 I just need intellisense; and I've wasted 2h, on only microsoft-owned products.
#Microsoft 0/5 stars
Still don't know why it happens, but here's how to solve it:
The problem was from this: I like to keep all my visual studio code extensions as .vsix files locally in folders so I can remember and get them easily & offline. Sensible enough choice, and I had the correct ms-dotnettools.csharp-1.25.2#win32-x64.vsix for my platform.
But in VSCode, when I install this particular extension via VSIX, even though it succeeds, it can't find .net. If you instead install the extension "normally", ie via vscode's own download, it seems vscode does some extra configuration steps while installing, and probably sets the .net paths correctly or something. Because now it works.
So TL;DR: don't install ms-dotnettools.csharp via vsix, it seems bugged. 😶
Would love to see an answer explaining what's wrong / how to actually do via vsix; but I don't have any more time to debug this rn.

c# Visual Studio 2015 - how to create a Setup which uninstall other application

Initially I created an application that I completely rewrite in a second version. It is a complete different Visual studio solution.
Now I would like that its Setup installer uninstall the previous version but because it was not created using the same solution, the automatic uninstallation of previous version does not work.
Is there any way to force the installer to uninstall certain application based on product name or product code?
I found a WMIC command that works when run from command line
wmic product where name="XXXX" call uninstall /nointeractive
So I created a VBS script which execute a bat file containing the WMIC code and I added it to the Setup project
dim shell
set shell=createobject("wscript.shell")
shell.run "uninstallAll.bat",0,true
set shell=nothing
but when I run the result MSI, it fires an error 1001, meaning that a service already exists. , in other words the uninstallation didn't work.
The old program is still present and they create a service with the same name. :/
any suggestion?
There are 2 options:
You can increase the version of MSI project so it will treat as upgrade and it will not throw any error while installing.
another way out is the write some in the installer project as follows:
protected override void OnBeforeInstall(IDictionary savedState)
{
//Write uninstall powershell script
//installutil /u <yourproject>.exe
using (PowerShell PowerShellInstance = PowerShell.Create())
{
PowerShellInstance.AddScript("");
PowerShellInstance.AddParameter("");
}
PowerShellInstance.Invoke();
}
Note: This InstallUtil is available with the .NET Framework, and its path is %WINDIR%\Microsoft.NET\Framework[64]\<framework_version>.
For example, for the 32-bit version of the .NET Framework 4 or 4.5.*, if your Windows installation directory is C:\Windows, the path is C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe.
For the 64-bit version of the .NET Framework 4 or 4.5.*, the default path is C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe
I decided to go for the option of introducing c# code in the project installer. Firstly I added the reference for System.Management.Automation via nuget
https://www.nuget.org/packages/System.Management.Automation
After this, I just created a string variable containing the PS code I need to uninstall several programs with a similar name.
string unInstallKiosk = #"$app = get-WMIObject win32_Product -Filter ""name like 'KIOSK'""
foreach ($program in $app){
$app2 = Get-WmiObject -Class Win32_Product | Where -Object { $_.IdentifyingNumber -match ""$($program.identifyingNumber)""
}
$app2.Uninstall()}";
and passed this variable to the method PowerShellInstance.AddScript()
PowerShellInstance.AddScript(unInstallKiosk);
The installation ends but the uninstallation simply dont happens.
anyone has an idea how to solve this?

.NET executables do not work after overwritten with new versions

I faced very strange behaviour - after I overwrite .NET exectables with new versions from network drive it cannot start.
When try to start from Windows Explorer it shows me following error:
[Window Title]
C:\Programs\zzz\clients.net\zzzNet.exe
[Content]
C:\Programs\zzz\clients.net\zzzNet.exe
The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log or use the command-line sxstrace.exe tool for more detail.
I tried to execute following commands:
SxsTrace Trace -logfile:SxsTrace.etl
SxStrace Parse -logfile:SxSTrace.etl -outfile:SxSTrace.txt
And got following result:
=================
Begin Activation Context Generation.
Input Parameter:
Flags = 0
ProcessorArchitecture = AMD64
CultureFallBacks = en-US;en;ru-RU;ru
ManifestPath = C:\Programs\zzz\clients.net\zzzNet.exe
AssemblyDirectory = C:\Programs\zzz\clients.net\
Application Config File = C:\Programs\zzz\clients.net\zzzNet.exe.Config
-----------------
INFO: Parsing Application Config File C:\Programs\zzz\clients.net\zzzNet.exe.Config.
INFO: Parsing Manifest File C:\Programs\zzz\clients.net\zzzNet.exe.
INFO: Manifest Definition Identity is (null).
ERROR: Line 0: XML Syntax error.
ERROR: Activation Context generation failed.
End Activation Context Generation.
It is quite simple .NET application (1 exe + 8 dll files). It was built for .NET 3.5 Client Profile.
I not defined any special "manifest" there. Just clicked "New Windows Forms Project" in Visual Studio and developed it.
Also app.config does not contain anything special - only primitive standard settings - appSettings and userSettings sections.
On PC where I developed it all is perfectly works. Problems only began when I copy these binaries to this particular VM and try to start.
Also please note - these executables were not installed in GAC or such, I just copied them into a folder on VM and started. And when it was 1st time all was working fine.
So, the problem pattern is following:
Copy .NET execuatbles to new VM (it is Win 7 x64), run it, all is working fine. Close it.
Build new version of .NET execuatbles on host PC, copy new .NET execuatbles to VM (with files overwriting).
Try to start - got mentioned problem.
After some shaman-style actions (like OS reboot, etc) it begin to work but why that happened at all?!
Why replacing .NET executables with new versions is causing SUCH HUGE PROBLEMS?!
Also the BIG QUESTION - is there any special procedure to replace .NET executables to keep them working? Because it is a new app development, I do not want lost so much time on every new executables installation. :-\
Could you please help? Because it looks completely weird!
Thank you in adance.
PS. I checked all VS projects - all they have PlatformTarget=AnyCPU. Also in run-time I can see ProcessType=MSIL (I show this info in AboutBox for application). So, there is no mix of x86/x64 binaries.
It seems that was related to mapped network drive behavior.
When I copied new files from network drive folder it copied wrong files - a strange random mess of new files and older ones (which were there before I updated them on VM host).
The scenario to make it working:
on VM: delete all files in a folder on network drive
on VM host: copy new files into a folder which is mapped as network drive on VM
on VM: copy files into target folder
on VM: run application - it works now
Weird thing. I remember I have seen something similar with Windows Explorer on Windows 2008 behaviour when copying updated win32 binaries.

DebugAdapter bin folder not found on path "....vscode/extensions/ms-vscode.csharp-0.3.7/coreclr-debug/debugAdapters/OpenDebugAD7.exe."

OS: Windows 10
dotnet-cli:
Version: 1.0.0-beta-001598
Commit Sha: 7582649f88
This is after following the instructions here for C# debugging. Does it work yet for windows?
According to the error message, it looks like you did not install the C# extension as per the instructions https://microsoft.gallery.vsassets.io/items?itemName=ms-vscode.csharp
Open your command pallet and verify that the correct version is installed. Or, if it is, remove it and try installing again

MonoMac Build Error From Default Template

I am trying to use MonoDevelop 3.0.3.2 to create a MonoMac application. I created a new MonoMac solution and tried running the template created after creating the solution, but this is my build output:
Building Solution: MAC1 (Debug|x86)
Building: MAC1 (Debug|x86)
Performing main compilation...
/Library/Frameworks/Mono.framework/Versions/2.10.9/bin/dmcs /noconfig
"/out:/Users/zgillis/Projects/MAC1/bin/Debug/MAC1.exe"
"/r:/Library/Frameworks/Mono.framework/Versions/2.10.9/lib/mono/4.0/System.dll"
"/r:/Library/Frameworks/Mono.framework/Versions/2.10.9/lib/mono/4.0/System.Xml.dll"
"/r:/Library/Frameworks/Mono.framework/Versions/2.10.9/lib/mono/4.0/System.Core.dll"
"/r:/Library/Frameworks/Mono.framework/Versions/2.10.9/lib/mono/4.0/System.Xml.Linq.dll"
"/r:/Library/Frameworks/Mono.framework/Versions/2.10.9/lib/mono/4.0/System.Drawing.dll"
"/r:/Applications/MonoDevelop.app/Contents/MacOS/lib/monodevelop/AddIns/MonoDevelop.MonoMac/MonoMac.dll" /nologo /warn:4 /debug:full /optimize- /codepage:utf8 /platform:x86
"/define:DEBUG" /t:exe "/Users/zgillis/Projects/MAC1/MainWindow.cs"
"/Users/zgillis/Projects/MAC1/MainWindowController.cs"
"/Users/zgillis/Projects/MAC1/MainWindow.designer.cs"
"/Users/zgillis/Projects/MAC1/Main.cs" "/Users/zgillis/Projects/MAC1/AppDelegate.cs"
"/Users/zgillis/Projects/MAC1/AppDelegate.designer.cs"
Copying content files
Copying '/Users/zgillis/Projects/MAC1/bin/Debug/MAC1.exe' to
'/Users/zgillis/Projects/MAC1/bin/Debug/MAC1.app/Contents/MonoBundle/MAC1.exe'
Copying '/Users/zgillis/Projects/MAC1/bin/Debug/MAC1.exe.mdb' to
'/Users/zgillis/Projects/MAC1/bin/Debug/MAC1.app/Contents/MonoBundle/MAC1.exe.mdb'
Build complete -- 0 errors, 0 warnings
Updating application manifest
---------------------- Done ----------------------
Build successful.
Build failed. The given key was not present in the dictionary.
I am running Mac OS X Snow Leopard (10.6.8), and my Xcode version is 3.2.2.
It will not allow me to compile even the default MonoMac template, so I know I must have something configured incorrectly. I appreciate the help.
It looks like your version of Xcode is old - it's likely that MonoDevelop's support for reading values from the older Apple SDKs' plists has regressed. I would suggest you either upgrade Xcode, or file a bug with the MonoDevelop log file (found in ~/Library/Logs/MonoDevelop-3.0) attached.

Categories