简体   繁体   中英

C# create site in IIS programatically in asp.net project

I am trying to create a site in IIS using Microsoft.Web.Administration dll in C#. I am using following code

using (var serverManager = new ServerManager())
        {
            ApplicationPool pool = serverManager.ApplicationPools.Add(ClientDomain);
            pool.ManagedRuntimeVersion = "v4.0";
            Site site = serverManager.Sites.Add(SiteName, httpProtocol, httpBindingInformation, physicalPath);
            site.Bindings.Add(httpsBindingInformation, httpsProtocol);
            serverManager.CommitChanges();
        }

I am getting following exception "The method or operation is not implemented" when I run the code from asp.net project but if same code is run from console project, it works fine. Here is the details of the exception

   at Microsoft.Web.Administration.Interop.IAppHostProperty.get_Value()
   at Microsoft.Web.Administration.ConfigurationElement.GetPropertyValue(IAppHostProperty property)
   at Microsoft.Web.Administration.ConfigurationElement.GetAttributeValue(String attributeName)
   at Microsoft.Web.Administration.Binding.get_CertificateHash()
   at Microsoft.Web.Administration.BindingCollection.Add(Binding binding)
   at Microsoft.Web.Administration.BindingCollection.Add(String bindingInformation, String bindingProtocol)
   at TestProject.BLL.CRMSiteManagement.CRMSiteIISSetup.Process() in e:\CRMSiteManagement\CRMSiteIISSetup.cs:line 35
   at TestProject.BLL.CRMSiteManagement.CRMSiteService.CreateNewCRMSite(CRMSite newSite) in e:CRMSiteManagement\CRMSiteService.cs:line

Seems like an issue with rights but I have not idea how to fix it. Update 1: Error is coming on following line of code on localhost

site.Bindings.Add(httpsBindingInformation, httpsProtocol); 

I think you're trying to set the same bindings twice :

  1. Once by calling the Add method

Site site = serverManager.Sites.Add(SiteName, httpProtocol, httpBindingInformation, physicalPath);

  1. Then you add again the same binding:

site.Bindings.Add(httpsBindingInformation, httpsProtocol);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM