"Invalid Class" Error while Starting FTp Site in IIS 7.0 using WMI and ASP.Net C# (windows Server 2008)
I am working on an ASP.Net Web Site Provisioning software using WMI(System.Managment) and C#.
I am trying to create FTP site on my "Target Server" located somewhere on LAN while executing code on my host machine. I have trouble starting the FTP Site after its creations.
The error being reported is "Invalid Class" on the following line:
ManagementObject site = new ManagementObject(
new ManagementPath(string.Format(@"IIsFtpServer.Name='MSFTPSVC/{0}'", siteId)), null);
**site.InvokeMethod("Start", null);**
Here is my Complete Function.
public static String CreateFtpsite(String serverName,
String ip,String ServerComment,int AccessFlags,
String pathToRoot, String hostName, String domainName, int port)
{
//ConnectionOptions options = new ConnectionOptions();
//options.Authentication = AuthenticationLevel.Connect;
//options.EnablePrivileges = true;
//options.Impersonation = ImpersonationLevel.Impersonate;
ConnectionOptions options = SetUpAuthorization();
ManagementScope scope =
new ManagementScope(string.Format(@"\\{0}\root\MicrosoftIISv2", serverName), options);
scope.Connect();
ManagementObject oW3SVC = new ManagementObject(scope,
new ManagementPath(@"IIsFtpService='MSFTPSVC'"), null);
ManagementBaseObject[] serverBindings = new ManagementBaseObject[3];
/*serverBindings[0] = CreateServerBinding(scope,
string.Format("{0}.{1}", hostName, domainName), ip, port);
*/
serverBindings[0] = CreateServerBinding(scope,
string.Format("{0}", hostName, domainName), ip, port);
serverBindings[1] = CreateServerBinding(scope,
string.Format(ip, hostName, domainName), ip, port);
serverBindings[2] = CreateServerBinding(scope,
string.Format("127.0.0.1", hostName, domainName), ip, port);
ManagementBaseObject inputParameters = oW3SVC.GetMethodParameters("CreateNewSite");
inputParameters["ServerBindings"] = serverBindings;
inputParameters["ServerComment"] = ServerComment;
inputParameters["PathOfRootVirtualDir"] = pathToRoot;
ManagementBaseObject outParameter =
oW3SVC.InvokeMethod("CreateNewSite", inputParameters, null);
string siteId = Convert.ToString(
outParameter.Properties["ReturnValue"].Value).Replace(
"IIsFtpServer='MSFTPSVC/", "").Replace("'", "");
ManagementObject oFtpVirtDir = new ManagementObject(scope,
new ManagementPath(string.Format(
@"IIsFtpVirtualDirSetting.Name='MSFTPSVC/{0}/root'", siteId)), null);
oFtpVirtDir.Properties["AccessFlags"].Value = AccessFlags ;
oFtpVirtDir.Properties["Path"].Value = pathToRoot;
ManagementObject oFtpVirtDirProperties = new ManagementObject(scope,
new ManagementPath(string.Format(@"IIsFtpServerSetting.Name='MSFTPSVC/{0}'", siteId)), null);
oFtpVirtDirProperties.Properties["AllowAnonymous"].Value = true;
oFtpVirtDirProperties.Properties["AnonymousOnly"].Value = true;
oFtpVirtDirProperties.Properties["AnonymousUserName"].Value = @"DevIIS\Administrator";
oFtpVirtDirProperties.Properties["AnonymousUserPass"].Value = "Passw0rd";
oFtpVirtDirProperties.Properties["MaxConnections"].Value = 555;
oFtpVirtDirProperties.Properties["ServerAutoStart"].Value = true;
oFtpVirtDirProperties.Properties["UserIsolationMode"].Value = 1;
oFtpVirtDirProperties.Properties["ConnectionTimeout"].Value = 1234 ;
oFtpVirtDirProperties.Properties["LogFileTruncateSize"].Value = 54321;
oFtpVirtDirProperties.Put();
ManagementObject site = new ManagementObject(
new ManagementPath(string.Format(@"IIsFtpServer.Name='MSFTPSVC/{0}'", siteId)), null);
site.InvokeMethod("Start", null); //Error occurs Here (Invalid Class)
return siteId;
}
public static ConnectionOptions SetUpAuthorization()
{
ConnectionOptions options = new ConnectionOptions();
options.Authentication = AuthenticationLevel.PacketPrivacy;
options.EnablePrivileges = true;
options.Impersonation = ImpersonationLevel.Impersonate;
options.Username = @"DevIIS\Administrator";
options.Password = "Passw0rd";
return options;
}
On analyzing the Code, i find that the "site" object throws exception of "Invalid Class" on "site.ClassPath".
I have also tried to use the following line but the same error is there.
ManagementObject site = new ManagementObject(scope,
new ManagementPath(Convert.ToString(
outParameter.Properties["ReturnValue"].Value)), null);
site.InvokeMethod("Start", null);
But the FTP Site is created and all its properties are set but it doesn't start via code. One may manually start it by going to IIS Manager .
The Code works perfectly fine on IIS 6.0 and (since i am using using WMI ) i expect that it shou开发者_StackOverflow社区ld run fine on IIS 7.0 or later too.
I am doing something wrong but couldn't guess where. Kindly help. Thanks.
Thanks
in iis7 it does not create "MSFTPSVC" folder in the metabase. it places ftpsite in the website container in the database. so the answer will be : use @"IIsWebService='W3SVC'" as a management path, instead of what you are using.
I figured the problem myself as i was calling the function on the wrong class. The "Start" method has to be called on the server class. i.e IIsWebServer.Start() or IIsFtpServer.Start()
精彩评论