2010-11-11 20 views
1

J'ai écrit du code pour créer un nouveau site dans ftp. mais son don com exception. le code est joint pour référence.Création de site ftp par programme

Détails de l'exception sont

System.Runtime.InteropServices.COMException was unhandled 
    Message="Filename: \r\nError: Unrecognized element 'ftpServer'\r\n\r\n" 
    Source="" 
    ErrorCode=-2147023483 
    StackTrace: 
     at Microsoft.Web.Administration.Interop.IAppHostElement.GetElementByName(String bstrSubName) 
     at Microsoft.Web.Administration.ConfigurationElement.GetChildElement(String elementName) 
     at WindowsFormsTest.Form2.CreateFTPSite(String serverName, String siteName, String siteID) in D:\My Projects\WindowsFormsTest\WindowsFormsTest\Form2.cs:line 48 
     at WindowsFormsTest.Form2.button1_Click(Object sender, EventArgs e) in D:\My Projects\WindowsFormsTest\WindowsFormsTest\Form2.cs:line 25 
     at System.Windows.Forms.Control.OnClick(EventArgs e) 
     at System.Windows.Forms.Button.OnClick(EventArgs e) 
     at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 
     at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 
     at System.Windows.Forms.Control.WndProc(Message& m) 
     at System.Windows.Forms.ButtonBase.WndProc(Message& m) 
     at System.Windows.Forms.Button.WndProc(Message& m) 
     at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
     at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
     at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 
     at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 
     at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) 
     at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 
     at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 
     at System.Windows.Forms.Application.Run(Form mainForm) 
     at WindowsFormsTest.Program.Main() in D:\My Projects\WindowsFormsTest\WindowsFormsTest\Program.cs:line 18 
     at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) 
     at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ThreadHelper.ThreadStart() 
    InnerException: 
Code

pour créer le site ftp:

using(ServerManager serverManager = new ServerManager()) { 
     Microsoft.Web.Administration.Configuration config = serverManager.GetApplicationHostConfiguration(); 

     ConfigurationSection sitesSection = config.GetSection("system.applicationHost/sites"); 

     ConfigurationElementCollection sitesCollection = sitesSection.GetCollection(); 

     ConfigurationElement siteElement = sitesCollection.CreateElement("site"); 
     siteElement["name"] = @"eMentorftp"; 

     ConfigurationElementCollection bindingsCollection = siteElement.GetCollection("bindings"); 

     ConfigurationElement bindingElement = bindingsCollection.CreateElement("binding"); 
     bindingElement["protocol"] = @"ftp"; 
     bindingElement["bindingInformation"] = @"*:21:"; 
     bindingsCollection.Add(bindingElement); 

     ConfigurationElement ftpServerElement = siteElement.GetChildElement("ftpServer"); 

     ConfigurationElement securityElement = ftpServerElement.GetChildElement("security"); 

     ConfigurationElement sslElement = securityElement.GetChildElement("ssl"); 
     sslElement["serverCertHash"] = @"53FC3C74A1978C734751AB7A14A3E48F70A58A84"; 
     sslElement["controlChannelPolicy"] = @"SslRequire"; 
     sslElement["dataChannelPolicy"] = @"SslRequire"; 

     ConfigurationElement authenticationElement = securityElement.GetChildElement("authentication"); 

     ConfigurationElement basicAuthenticationElement = authenticationElement.GetChildElement("basicAuthentication"); 
     basicAuthenticationElement["enabled"] = true; 

     ConfigurationElementCollection siteCollection = siteElement.GetCollection(); 

     ConfigurationElement applicationElement = siteCollection.CreateElement("application"); 
     applicationElement["path"] = @"/"; 

     ConfigurationElementCollection applicationCollection = applicationElement.GetCollection(); 

     ConfigurationElement virtualDirectoryElement = applicationCollection.CreateElement("virtualDirectory"); 
     virtualDirectoryElement["path"] = @"/"; 
     virtualDirectoryElement["physicalPath"] = @"D:\Ftp"; 
     applicationCollection.Add(virtualDirectoryElement); 
     siteCollection.Add(applicationElement); 
     sitesCollection.Add(siteElement); 

     serverManager.CommitChanges(); 

Répondre

5

Vous pouvez dire ce que votre erreur est de l'exception. Regardez la première ligne dans la trace de pile d'exception:

System.Runtime.InteropServices.COMException was unhandled Message="Filename: \r\nError: Unrecognized element 'ftpServer'\r\n\r\n" Source="" ErrorCode=-2147023483

Tout d'abord, nous voyons « élément non reconnu « ftpserver » » - ce qui devrait nous dire beaucoup de choses sur ce qui est arrivé quand on regarde votre code. Mais en plus de cela, nous recevons un code d'erreur qui nous dit exactement pourquoi la exception COMException a été levée. Si on décode le code d'erreur dans un entier non signé (hexadécimal), nous obtenons:

0x80070585 

Cela correspond à un code d'erreur de:

  • 8: Le non
  • 7: Win32
  • 585: (1413 décimal): index non valide (ERROR_INVALID_INDEX)

Pour plus d'informations sur les codes d'erreur, voir HRESULT et Win32 error codes. Donc, nous avons lancé une exception COMException, car nous avons utilisé un index invalide. Et nous obtenons un "élément non reconnu 'ftpServer'" dans le message d'exception. En regardant votre code, vous créez un nouveau ConfigurationElement siteElement en racontant l'sitesCollection d'ajouter un nouvel élément nommé « site »:

Microsoft.Web.Administration.Configuration config = serverManager.GetApplicationHostConfiguration(); 
ConfigurationSection sitesSection = config.GetSection("system.applicationHost/sites"); 
ConfigurationElementCollection sitesCollection = sitesSection.GetCollection(); 
ConfigurationElement siteElement = sitesCollection.CreateElement("site"); 

Vous créez ensuite un nom pour le site, et de créer des liaisons dans la collection de liaisons du site:

siteElement["name"] = @"eMentorftp"; 
ConfigurationElementCollection bindingsCollection = siteElement.GetCollection("bindings"); 
ConfigurationElement bindingElement = bindingsCollection.CreateElement("binding"); 
bindingElement["protocol"] = @"ftp"; 
bindingElement["bindingInformation"] = @"*:21:"; 
bindingsCollection.Add(bindingElement); 

Malheureusement, vous essayez ensuite d'obtenir un élément enfant du site en utilisant le nom "fTPServer":

ConfigurationElement ftpServerElement = siteElement.GetChildElement("ftpServer"); 

Mais, le ConfigurationEle Le site siteElement - qui vient d'être créé - n'a pas d'enfant nommé ftpServer! Nous obtenons donc un index invalide et jetons une exception. Vouliez-vous créer un élément enfant dans cette ligne de code? En outre, j'imagine qu'il s'agit d'une exception COMException due à l'interopérabilité de l'interface .NET avec les objets COM IIS7 sous-jacents. Ne pas être un expert (du tout) sur l'espace de noms Microsoft.Web, c'est juste une supposition.

+1

+1 Pour obtenir de l'aide sur les codes d'erreur de décodage. –