“无效类别”使用 WMI 和 ASP.Net C# (windows Server 2008) 在 IIS 7.0 中启动 FTp 站点时出错

发布于 2024-08-21 14:05:28 字数 4629 浏览 7 评论 0原文

我正在使用 WMI(System.Managment) 和 C# 开发 ASP.Net 网站配置软件。

我正在尝试在位于 LAN 上某处的“目标服务器”上创建 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”对象在“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 或更高版本上也能运行良好。

我做错了一些事情,但无法猜测哪里错了。请帮忙。谢谢。

谢谢

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 should run fine on IIS 7.0 or later too.

I am doing something wrong but couldn't guess where. Kindly help. Thanks.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

似梦非梦 2024-08-28 14:05:28

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

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.

好听的两个字的网名 2024-08-28 14:05:28

我自己发现了这个问题,因为我在错误的类上调用了该函数。必须在服务器类上调用“Start”方法。即 IIsWebServer.Start() 或 IIsFtpServer.Start()

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()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文