Back in the early iis7 days, we created a customized provisioning tool in c#. We rely on shared configuration in our setup.
If you run this application locally on a webserver, all works well. However, parts of the software were updated (same story for the servers, now win2008R2X64/IIS7.5) to allow provisioning from a remote computer.
Thanks to the guide to change DCOM authentication found here:
http://learn.iis.net/page.aspx/268/shared-configuration-and-remote-provisioning/ 95% of the functionality (creation of webspaces,...) is going great on the remote computer.
However, altering the delegation of these sites always results in an exception: "specified cast is not valid". What am I missing here?
"specified cast is not valid". What am I missing here?
Check your souce code carefully. It appear to have no problem.
This error above hints that type converstion fails. I have tested these code in my machine. It does work well. Please have a check my full sample code:
Note: The "Mamba" is my another machine.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Web.Administration;
namespace MWA
{
class Program
{
static void Main(string[] args)
{
GetRomteConfiguration();
}
static void GetRomteConfiguration()
{
ServerManager serverManager = ServerManager.OpenRemote("Mamba");
Configuration configuration = serverManager.GetAdministrationConfiguration();
Microsoft.Web.Administration.ConfigurationSection configSection = configuration.GetSection("moduleProviders");
Microsoft.Web.Administration.ConfigurationElementCollection elementCollection = configSection.GetCollection();
Console.WriteLine("There are " + elementCollection.Count.ToString() +
" elements in the section.");
foreach (Microsoft.Web.Administration.ConfigurationElement element in elementCollection)
{
Console.WriteLine("\t {0}", element.Attributes["name"].Value);
}
}
static void GetApplicationHostConfiguration()
{
ServerManager serverManager = new ServerManager();
Configuration config = serverManager.GetApplicationHostConfiguration();
ConfigurationSection section = config.GetSection("system.webServer/defaultDocument", "Default Web Site");
ConfigurationAttribute enabled = section.GetAttribute("enabled");
ConfigurationElement elemetns = section.GetCollection();
//enabled.Value = false;
//serverManager.CommitChanges();
}
static void GetAdministrationConfiguration1()
{
Microsoft.Web.Administration.ServerManager serverManger = new Microsoft.Web.Administration.ServerManager();
//WebConfigurationMap webConfigMap = new WebConfigurationMap();
Microsoft.Web.Administration.Configuration configuration = serverManger.GetAdministrationConfiguration();
Microsoft.Web.Administration.ConfigurationSection configSection = configuration.GetSection("moduleProviders");
Microsoft.Web.Administration.ConfigurationElementCollection elementCollection = configSection.GetCollection();
Console.WriteLine("There are " + elementCollection.Count.ToString() +
" elements in the section.");
foreach (Microsoft.Web.Administration.ConfigurationElement element in elementCollection)
{
Console.WriteLine("\t {0}", element.Attributes["name"].Value);
}
}
}
}
Please mark the replies as answers if they help or unmark if not.
Feedback to us
Thank you for looking into this. I can't see much difference with my code. Are you also using shared configuration on the 'mamba' server? regards, Roelvs
Was this the accepted answer? It does not address the OP's problem.
We just hit the same issue - InvalidCastException in Microsoft.Web.Administration.ConfigurationManager.GetWebConfiguration when connecting remotely with a WebConfigurationMap specified.
From quick examination in reflector, it appears to be thrown when ConfigurationManager attempts to set the WebConfigurationMap metadata attribute on the newly created apphostadminmanager.
The full stack is:
at Microsoft.Web.Administration.Interop.IAppHostAdminManager.SetMetadata(String bstrMetadataType, Object Value)
at Microsoft.Web.Administration.ConfigurationManager.SetAdminManagerProperties(WebConfigurationMap webConfigMap, Boolean isAdminConfig, IAppHostAdminManager adminManager, Boolean isRemote)
at Microsoft.Web.Administration.ConfigurationManager.CreateAdminManager[TClass,TInterface](WebConfigurationMap webConfigMap, Boolean isAdminConfig)
at Microsoft.Web.Administration.ConfigurationManager.CreateConfiguration(WebConfigurationMap configMap, String configPathToEdit, Boolean isAdminConfig)
at Microsoft.Web.Administration.ConfigurationManager.GetWebConfiguration(WebConfigurationMap configMap, String configurationPath)
at Microsoft.Web.Administration.ServerManager.GetWebConfiguration(WebConfigurationMap configMap, String configurationPath)
Both machines are on the domain, the domain account running code is admin on both, and remote MWA works for other cases (e.g. loading application pool configuration).
Thanks,
Mike
Mike Volodarsky
CTO at LeanSentry
Former IIS/ASP.NET PM
Want to become an expert at monitoring and troubleshooting your IIS applications?
See the demo at www.leansentry.com!
roelvs
13 Posts
error in remote call to web.administration in c#
Apr 13, 2012 10:05 AM|LINK
Hi,
Back in the early iis7 days, we created a customized provisioning tool in c#. We rely on shared configuration in our setup.
If you run this application locally on a webserver, all works well. However, parts of the software were updated (same story for the servers, now win2008R2X64/IIS7.5) to allow provisioning from a remote computer.
Thanks to the guide to change DCOM authentication found here: http://learn.iis.net/page.aspx/268/shared-configuration-and-remote-provisioning/ 95% of the functionality (creation of webspaces,...) is going great on the remote computer.
However, altering the delegation of these sites always results in an exception: "specified cast is not valid". What am I missing here?
using (ServerManager serverManager = ServerManager.OpenRemote(Config.WebServer))
{
Configuration config = serverManager.GetAdministrationConfiguration(); ///////////////EXCEPTION: "Specified cast is not valid."
ConfigurationSection authorizationSection = config.GetSection("system.webServer/management/authorization");
ConfigurationElementCollection authorizationRulesCollection = authorizationSection.GetCollection("authorizationRules");
ConfigurationElement scopeElement = FindElement(authorizationRulesCollection, "scope", "path", pathOfTheSiteToDelegate);
if (scopeElement == null)
{
scopeElement = authorizationRulesCollection.CreateElement("scope");
scopeElement["path"] = pathOfTheSiteToDelegate;
authorizationRulesCollection.Add(scopeElement);
}
ConfigurationElementCollection scopeCollection = scopeElement.GetCollection();
Mamba Dai - ...
651 Posts
Microsoft
Re: error in remote call to web.administration in c#
Apr 17, 2012 09:07 AM|LINK
Hi,
Check your souce code carefully. It appear to have no problem.
This error above hints that type converstion fails. I have tested these code in my machine. It does work well. Please have a check my full sample code:
Note: The "Mamba" is my another machine.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Web.Administration;
namespace MWA
{
class Program
{
static void Main(string[] args)
{
GetRomteConfiguration();
}
static void GetRomteConfiguration()
{
ServerManager serverManager = ServerManager.OpenRemote("Mamba");
Configuration configuration = serverManager.GetAdministrationConfiguration();
Microsoft.Web.Administration.ConfigurationSection configSection = configuration.GetSection("moduleProviders");
Microsoft.Web.Administration.ConfigurationElementCollection elementCollection = configSection.GetCollection();
Console.WriteLine("There are " + elementCollection.Count.ToString() +
" elements in the section.");
foreach (Microsoft.Web.Administration.ConfigurationElement element in elementCollection)
{
Console.WriteLine("\t {0}", element.Attributes["name"].Value);
}
}
static void GetApplicationHostConfiguration()
{
ServerManager serverManager = new ServerManager();
Configuration config = serverManager.GetApplicationHostConfiguration();
ConfigurationSection section = config.GetSection("system.webServer/defaultDocument", "Default Web Site");
ConfigurationAttribute enabled = section.GetAttribute("enabled");
ConfigurationElement elemetns = section.GetCollection();
//enabled.Value = false;
//serverManager.CommitChanges();
}
static void GetAdministrationConfiguration1()
{
Microsoft.Web.Administration.ServerManager serverManger = new Microsoft.Web.Administration.ServerManager();
//WebConfigurationMap webConfigMap = new WebConfigurationMap();
Microsoft.Web.Administration.Configuration configuration = serverManger.GetAdministrationConfiguration();
Microsoft.Web.Administration.ConfigurationSection configSection = configuration.GetSection("moduleProviders");
Microsoft.Web.Administration.ConfigurationElementCollection elementCollection = configSection.GetCollection();
Console.WriteLine("There are " + elementCollection.Count.ToString() +
" elements in the section.");
foreach (Microsoft.Web.Administration.ConfigurationElement element in elementCollection)
{
Console.WriteLine("\t {0}", element.Attributes["name"].Value);
}
}
}
}
Feedback to us
Develop and promote your apps in Windows Store
roelvs
13 Posts
Re: error in remote call to web.administration in c#
Apr 17, 2012 08:50 PM|LINK
Mamba Dai - ...
651 Posts
Microsoft
Re: error in remote call to web.administration in c#
Apr 18, 2012 03:01 AM|LINK
Hi,
Sorry for forgetting this, I haven't used shared configuration.
Feedback to us
Develop and promote your apps in Windows Store
mvolo
629 Posts
Re: error in remote call to web.administration in c#
May 05, 2013 06:39 PM|LINK
Was this the accepted answer? It does not address the OP's problem.
We just hit the same issue - InvalidCastException in Microsoft.Web.Administration.ConfigurationManager.GetWebConfiguration when connecting remotely with a WebConfigurationMap specified.
The usage is:
using(var serverMgr = ServerManager.OpenRemote(machineName))
{
// this line throws the exception
Microsoft.Web.Administration.Configuration rootWebConfig = mgr.GetWebConfiguration(
new WebConfigurationMap(
machineConfigPath,
webRootConfigPath
),
null
);
From quick examination in reflector, it appears to be thrown when ConfigurationManager attempts to set the WebConfigurationMap metadata attribute on the newly created apphostadminmanager.
The full stack is:
at Microsoft.Web.Administration.Interop.IAppHostAdminManager.SetMetadata(String bstrMetadataType, Object Value)
at Microsoft.Web.Administration.ConfigurationManager.SetAdminManagerProperties(WebConfigurationMap webConfigMap, Boolean isAdminConfig, IAppHostAdminManager adminManager, Boolean isRemote)
at Microsoft.Web.Administration.ConfigurationManager.CreateAdminManager[TClass,TInterface](WebConfigurationMap webConfigMap, Boolean isAdminConfig)
at Microsoft.Web.Administration.ConfigurationManager.CreateConfiguration(WebConfigurationMap configMap, String configPathToEdit, Boolean isAdminConfig)
at Microsoft.Web.Administration.ConfigurationManager.GetWebConfiguration(WebConfigurationMap configMap, String configurationPath)
at Microsoft.Web.Administration.ServerManager.GetWebConfiguration(WebConfigurationMap configMap, String configurationPath)
Both machines are on the domain, the domain account running code is admin on both, and remote MWA works for other cases (e.g. loading application pool configuration).
Thanks,
Mike
CTO at LeanSentry
Former IIS/ASP.NET PM
Want to become an expert at monitoring and troubleshooting your IIS applications?
See the demo at www.leansentry.com!