2010-02-13 24 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()

相關問題