2010-02-13 15 views
0

我正在使用WMI(System.Managment)和C#编写ASP.Net网站配置软件。使用WMI和ASP.Net在IIS 7.0中启动FTp站点时出现“Invalid Class”错误

我想在位于局域网某处的“目标服务器”上创建FTP站点,同时在主机上执行代码。创建FTP后,我无法启动FTP站点。

所报告的错误是“无效类”以下行:

ManagementObject site = new ManagementObject(
      new ManagementPath(string.Format(@"IIsFtpServer.Name='MSFTPSVC/{0}'", siteId)), null); 
     **site.InvokeMethod("Start", null);** 

这里是我的完整功能。

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; 
    } 

在分析代码,我发现“网站”对象抛出“无效类”的例外,在"site.ClassPath".

我也曾尝试使用下面的行,但同样的错误是存在的。

ManagementObject site = new ManagementObject(scope, 
     new ManagementPath(Convert.ToString(
     outParameter.Properties["ReturnValue"].Value)), null); 

     site.InvokeMethod("Start", null); 

但FTP站点已创建并且其所有属性都已设置,但它不通过代码启动。可以通过转到IIS管理器来手动启动它。

该代码在IIS 6.0上工作得非常好(因为我使用的是WMI),我希望它能在IIS 7.0或更高版本上正常运行。

我做错了什么,但不知道在哪里。请帮助。谢谢。

感谢

回答

1

在IIS7它不会创建配置数据库 “MSFTPSVC” 文件夹中。它将ftpsite放置在数据库的网站容器中。所以答案将是: 使用@“IIsWebService ='W3SVC'”作为管理路径,而不是您正在使用的。

+0

我自己想到了这个问题,因为我在错误的类上调用函数。必须在服务器类上调用“开始”方法。即IIsWebServer.Start()或IIsFtpServer.Start() – 2010-02-15 09:24:42

0

我在错误的类上调用函数时自己想到了这个问题。必须在服务器类上调用“开始”方法。即IIsWebServer.Start()或IIsFtpServer.Start()

相关问题