ServerManager fails with 0x80040154 in c# Winforms app creating simple web application in IIS - c#

I am writing a small app that installs IIS and configures a website before deploying the files to it.
On a freshly reset Windows 10, the first attempt always fails with the 0x80040154 COM+ component failure as documented in This question
I looked at the version I am using and it is the latest and correct one for .net standard (4.8) and not the one meant for .net core
When I press the button to rerun the function it always finishes correctly. I tried using a retry routine, and it fails on each retry, yet runs fine again when the button is pressed. The reason for this I assume is that the server manager object isn't disposed when it hits the catch block since its in a using statement.
I can work around that, but I really want to understand the issue and make a permanent fix.
My routine simply creates a website in IIS and creates an app pool to assign to it.
And it is running with elevated privileges
For reference:
Machine is Windows 10 latest from the downloadable media creator.
Microsoft.Web.Administrator version is 7.0.0.0
App is .net 4.8 standard windows forms
using (var serverManager = new ServerManager())
{
string iisrootdir = drive;
//Check for inetpub/wwwroot
if (!Directory.Exists(iisrootdir)) //Check for Drive D
{
iisrootdir = #"C:\";
}
string iiscmsdir = Path.Combine(iisrootdir, "webdir", "appdir");
if (!Directory.Exists(iiscmsdir))
Directory.CreateDirectory(iiscmsdir);
var settings = new ApplicationSettings();
settings.ReadFromFile();
settings.CMSPATH = iiscmsdir;
settings.SaveToFile();
try
{
string poolName = "DefaultAppPool";
if (serverManager.Sites.Count > 0)
{
Site myDefualtWebsite = serverManager.Sites[0];
if (myDefualtWebsite != null)
{
OnRaiseInstallEvent(new InstallEventArgs("CreateWebsite", ProcessState.Started,
"Remove Default Website"));
serverManager.Sites.Remove(myDefualtWebsite);
serverManager.CommitChanges();
}
}
if (!WebsiteExists("sitename"))
{
mySite.ServerAutoStart = true;
}
Site site = serverManager.Sites["sitename"];
if (!AppPoolExists(poolName))
{
serverManager.ApplicationPools.Add(poolName);
}
ApplicationPool apppool = serverManager.ApplicationPools[poolName];
apppool.ManagedPipelineMode = ManagedPipelineMode.Integrated;
apppool.ManagedRuntimeVersion = "";
serverManager.Sites["sitename"].ApplicationDefaults.ApplicationPoolName = poolName;
foreach (var item in serverManager.Sites["sitename"].Applications)
{
item.ApplicationPoolName = poolName;
}
serverManager.CommitChanges();
apppool.Recycle();
serverManager.CommitChanges();
}
catch (Exception ex)
{
if (ex.Message.Contains("80040154") && errorCount < 4)
{
if (serverManager != null)
serverManager.Dispose();
errorCount++;
OnRaiseInstallEvent(new InstallEventArgs("CreateWebsite", ProcessState.Started,
"Error encountered with COM+ object, trying again: " + errorCount));
CreateWebsite(#"D:\");
}
else
{
if (serverManager != null)
serverManager.Dispose();
errorCount = 0;
OnRaiseErrorEvent(new InstallErrorEventArgs("CreateWebsite", ProcessState.Error, ex));
return false;
}
}
finally
{
serverManager?.Dispose();
}

Thanks for the help Guys. I found the problem.
DISM was running in its own thread. The Process object exited the moment it launched. My function was then attempting to configure IIS before it had finished installing.

Related

How to manage folders permissions on Mac OS with ACL in .NET Core 3.1.0

I have the following code in C#:
if (!Directory.Exists(inputFolder))
{
return true;
}
else
{
var me = new DirectoryInfo(inputFolder);
AuthorizationRuleCollection rules = null;
WindowsIdentity identity = null;
try
{
rules = me.GetAccessControl().GetAccessRules(true, true, typeof(SecurityIdentifier));
identity = WindowsIdentity.GetCurrent();
}
catch (UnauthorizedAccessException uae)
{
return false;
}
string userSID = identity.User.Value;
foreach (FileSystemAccessRule rule in rules)
{
if ((rule.FileSystemRights.HasFlag(FileSystemRights.Read) ||
rule.FileSystemRights.HasFlag(FileSystemRights.ReadAttributes) ||
rule.FileSystemRights.HasFlag(FileSystemRights.ReadData) ||
rule.FileSystemRights.HasFlag(FileSystemRights.ReadAndExecute)) && rule.AccessControlType == AccessControlType.Deny)
{
return false;
}
}
return true;
}
And it works correctly in Windows, however, when I try to run this on mac OS X (Visual Studio for Mac) it throws an exception when trying to get the information from the method "GetAccessRules" or even the "GetAccessControl" saying:
"Access Control List (ACL) APIs are part of resource management on Windows and are not supported on this platform."
So here is my question:
Is there an alternative to do this in .net core? Or how should i manage these kind of permissions in .net core but in mac OS X?
I am assuming you are using System.IO.FileSystem.AccessControl to make GetAccessControl() work on .NET Core? Unfortunately these APIs are closely tied to Windows low level APIs and won't work on Unix/Linux systems.
However, there is an alternative. You could give Mono.Posix.NETStandard a try. Here is the Nuget link.
You can call UnixDirectoryInfo() and get or set FileAccessPermissions that way.
Updated
I found the solution and it was the same provided by Ruv in the answer above.
So, installing the Nuget Package Mono.Posix.NETStandard works. Just get the information of the directory/file:
var unixDirInfo = new Mono.Unix.UnixDirectoryInfo('path');
Then you can check the permissions like this:
unixDirInfo.canAccess(AccessModes.F_OK) // is a file or directory
unixDirInfo.canAccess(AccessModes.R_OK) // accessible for reading
unixDirInfo.canAccess(AccessModes.W_OK) // accessible for writing
unixDirInfo.canAccess(AccessModes.X_OK) // accessible for executing
or from the FileAccessPermissions:
FileAccessPermissions permissions = unixDirInfo.FileAccessPermissions;
permissions.HasFlag(FileAccessPermissions.UserRead);
permissions.HasFlag(FileAccessPermissions.UserWrite);
permissions.HasFlag(FileAccessPermissions.UserExecute);
permissions.HasFlag(FileAccessPermissions.UserExecute .UserReadWriteExecute);
You can check the FileAccessPermission enum for more information.

Mapped drivelabel windows 7 issue

I am going crazy with an issue with LABEL of mapping a drive to windows with windows 7 OS. Scenario;
We need to map the drive as soon as user logs in to the machine. That seems to be working fine with other os versions except windows 7. Steps for windows 7;
EXE (c# made by us) launched in windows 7
EXE has mapped drive correctly
User logged out
Logged in back
Again exe is trying to map drive (Its registered as a startup exe)
The mapped drive label becomes "Network Drive" (Not sure how)
We are setting the proper values in registry as well as shown in below figure;
Problem is here;
Issue is only occurring when we do logout and login. If we manually launch exe, it works fine...
I have also tried to DELETE all these registry before mapping driving assuming it might be cache or something but nothing helped..
We are using zMapDrive to map a drive;
//create struct data
structNetResource stNetRes = new structNetResource();
stNetRes.iScope = 2;
stNetRes.iType = RESOURCETYPE_DISK;
stNetRes.iDisplayType = 3;
stNetRes.iUsage = 1;
stNetRes.sRemoteName = ls_ShareName;
stNetRes.sLocalName = ls_Drive;
//prepare params
int iFlags = 0;
if (lf_SaveCredentials) { iFlags += CONNECT_CMD_SAVECRED; }
if (lf_Persistent) { iFlags += CONNECT_UPDATE_PROFILE; }
if (ls_PromptForCredentials) { iFlags += CONNECT_INTERACTIVE + CONNECT_PROMPT; }
if (psUsername == "") { psUsername = null; }
if (psPassword == "") { psPassword = null; }
//if force, unmap ready for new connection
if (lf_Force) { try { zUnMapDrive(true); } catch { } }
//call and return
int i = WNetAddConnection2A(ref stNetRes, psPassword, psUsername, iFlags);
if (i > 0) { throw new System.ComponentModel.Win32Exception(i); }
Maybe a simple powershell script, renaming network drive will work for you? You can then use Task Scheduler to run it every time a user logs in.
$Rename = New-Object -ComObject Shell.Application
$Net = New-Object -ComObject WScript.Network
# map the drive if the path doesn't exist
If (!(Test-Path Z:))
{
$Net.MapNetworkDrive("Z:", '\\SERVER_ADDRESS\Directory', $false, "user", "password")
}
# change the drive name
$Rename.NameSpace("Z:\").Self.Name = "MyNetDriveLabel"
From my experience, support for network mapped drives is somewhat buggy in Windows 7, so I use similar workaround on a few of our Win7 machines.

Assign application pool to website in iis7 programmatically

I'm using the following method to create application pool and assign it to web site.
This is code for application pool creation:
WebSiteName = some website name which existed.
I see that all values are get where they should be in debug mode
private void ConfiugreAppPoolIIS7(targetMachine)
{
var mgr = ServerManager.OpenRemote(targetMachine);
if (AppPoolProp.ContainsKey("SomeTestAppPool"))
{
string reqAppPool = AppPoolProp["SomeTestAppPool"];
if (!mgr.ApplicationPools.Any(pool => pool.Name == reqAppPool))
{
ApplicationPool myApp = mgr.ApplicationPools.Add(reqAppPool);
myApp.AutoStart = true;
myApp.ManagedPipelineMode = ManagedPipelineMode.Classic;
myApp.ManagedRuntimeVersion = "V4.0";
myApp.ProcessModel.IdentityType = ProcessModelIdentityType.NetworkService;
myApp.Enable32BitAppOnWin64 = true;
mgr.CommitChanges();
foreach (Site site in mgr.Sites)
{
if(site.Name == WebSiteName)
{
site.Stop();
site.ApplicationDefaults.ApplicationPoolName = myApp.Name;
site.Start();
mgr.CommitChanges();
}
}
}
}
}
The result is, I see the Aplication pool created successfuly, and the Website created as well, but, I see in the advanced settings of my web site the default appplication pool is assigned.
Could you please help?
The problem has been fixed by fixing reference to right Microsoft.Web.Administration.dll
Cause of this server manage connected to express iis instead of my local iis.

virtual directory properties in c#

I did a quick search and got this link
From StackOverflow
I get this error message
"Unknown error (0x80005000)"
when it hits
bool canCreate = !(schema.Properties["Syntax"].Value.ToString().ToUpper() == "BOOLEAN");
My requirement is to create a virtual directory and set AccessRead, IsolationMode, Scripts and Executables, ApplicationProtection to medium.
Got it to work, here is the code:
Note: I still couldn't get it to work on windows 7, it throws the same error and the message includes reference to System.InterOp... At the moment I don't care about getting it to work on windows 7, I deployed it on windows 2003, it works.
public void CreateVirtualDirectory(string virtualdirectory, string physicalpath)
{
try
{
///check if path exists
if (!Directory.Exists(physicalpath))
{
Log(string.Format(#"CreateVirtualDirectory; Path not found - {0}", physicalpath));
return;
}
DirectoryEntry parent = new DirectoryEntry(string.Format("IIS://{0}/W3SVC/1/Root", Environment.MachineName));
foreach (System.DirectoryServices.DirectoryEntry v in parent.Children)
{
if (v.Name == virtualdirectory)
{
try
{
parent.Invoke("Delete", new string[] { v.SchemaClassName, virtualdirectory });
}
catch
{
}
}
}
DirectoryEntry newFolder = (DirectoryEntry)parent.Invoke("Create", "IIsWebVirtualDir", virtualdirectory);
newFolder.InvokeSet("Path", physicalpath);
newFolder.Invoke("AppCreate", false);
newFolder.InvokeSet("AppFriendlyName", virtualdirectory);
const int MEDIUM_POOL = 2;
newFolder.Properties["AccessRead"][0] = true;
newFolder.Properties["AccessExecute"][0] = true;
newFolder.Properties["AccessWrite"][0] = false;
newFolder.Properties["AccessScript"][0] = true;
newFolder.Properties["AuthNTLM"][0] = true;
newFolder.Properties["AppIsolated"].Clear();
newFolder.Properties["AppIsolated"].Add(MEDIUM_POOL);
newFolder.CommitChanges();
}
catch (Exception ex)
{
Log(string.Format(#"CreateVirtualDirectory '{0}' failed; {1}", virtualdirectory, ex.Message));
}
}
You're using DirectoryEntry to create websites which requires IIS Compat Mode to be installed/configured in IIS. That's compat mode for the IIS6 directory entry interfaces if you're on IIS7 or later.
If it's not installed, you'll get 80005000.
If you're on IIS7 or later (Windows Server 2008 or Vista and later), the newer (non-compat mode) approach is the new managed Microsoft.Web.Administration.
http://blogs.msdn.com/b/carlosag/archive/2006/04/17/microsoftwebadministration.aspx

When programmatically creating a new IIS web site, how can I add it to an existing application pool?

I have successfully automated the process of creating a new IIS website, however the code I've written doesn't care about application pools, it just gets added to DefaultAppPool. However I'd like to add this newly created site to an existing application pool.
Here is the code I'm using to create the new website.
var w3Svc = new DirectoryEntry(string.Format("IIS://{0}/w3svc", webserver));
var newsite = new object[] { serverComment, new object[] { serverBindings }, homeDirectory };
var websiteId = w3Svc.Invoke("CreateNewSite", newsite);
site.Invoke("Start", null);
site.CommitChanges();
<update>
Although this is not directly related to the question, here are some sample values being used above. This might help someone understand exactly what the code above is doing more easily.
webServer: "localhost"
serverComment: "testing.dev"
serverBindings: ":80:testing.dev"
homeDirectory: "c:\inetpub\wwwroot\testing\"
</update>
If I know the name of the application pool that I'd like this web site to be in, how can I find it and add this site to it?
You have to assign the AppPool on the virtual dir (not the webserver) and set the AppIsolated property to 2 which mean pooled-process ;)
http://msdn.microsoft.com/en-us/library/ms525598%28v=VS.90%29.aspx
Relevant code sample from link:
static void AssignVDirToAppPool(string metabasePath, string appPoolName)
{
// metabasePath is of the form "IIS://<servername>/W3SVC/<siteID>/Root[/<vDir>]"
// for example "IIS://localhost/W3SVC/1/Root/MyVDir"
// appPoolName is of the form "<name>", for example, "MyAppPool"
Console.WriteLine("\nAssigning application {0} to the application pool named {1}:", metabasePath, appPoolName);
try
{
DirectoryEntry vDir = new DirectoryEntry(metabasePath);
string className = vDir.SchemaClassName.ToString();
if (className.EndsWith("VirtualDir"))
{
object[] param = { 0, appPoolName, true };
vDir.Invoke("AppCreate3", param);
vDir.Properties["AppIsolated"][0] = "2";
Console.WriteLine(" Done.");
}
else
Console.WriteLine(" Failed in AssignVDirToAppPool; only virtual directories can be assigned to application pools");
}
catch (Exception ex)
{
Console.WriteLine("Failed in AssignVDirToAppPool with the following exception: \n{0}", ex.Message);
}
}
Note that if you are not explicitly adding a new virtual directory to the application, the metabasePath will simply be "IIS://<servername>/W3SVC/<siteID>/Root"
You need to get the AppPoolfrom IIS://{0}/W3SVC/AppPools, and attach it to the site's AppPoolId. Something like:
var appPool = new DirectoryEntry(
string.Format("IIS://{0}/W3SVC/AppPools/{1}", webServer, appPoolName)
);
site.Properties["AppPoolId"].Value = appPool;
site.Properties["AppPoolId"][0]= "poolname";

Categories