以编程方式在IIS中配置和托管WCF服务

时间:2013-07-19 07:13:08

标签: wcf wcf-hosting

如何在IIS中以编程方式配置和托管WCF服务。我已经创建了我的WCF服务示例/WCFServices/Service1.svc“。我想以编程方式在IIS中配置和托管此服务。任何人都可以帮我这个吗?

3 个答案:

答案 0 :(得分:3)

您想要的课程是Microsoft.Web.Administration.ServerManager

http://msdn.microsoft.com/en-us/library/microsoft.web.administration.servermanager(v=VS.90).aspx

它具有操作IIS大多数方面的方法,例如,添加应用程序池和应用程序。例如,此代码配置新的IIS应用程序

//the name of the IIS AppPool you want to use for the application  - could be DefaultAppPool
string appPoolName = "MyAppPool";

//the name of the application (as it will appear in IIS manager)
string name = "MyWCFService";

//the physcial path of your application
string physicalPath = "C:\\wwwroot\mywcfservice";


using (ServerManager serverManager = new ServerManager())
    {
      Configuration config = serverManager.GetApplicationHostConfiguration();
      ConfigurationSection sitesSection = config.GetSection("system.applicationHost/sites");
      ConfigurationElementCollection sitesCollection = sitesSection.GetCollection();
      ConfigurationElement siteElement = sitesCollection[0]; 
      ConfigurationElementCollection siteCollection = siteElement.GetCollection();
      ConfigurationElement applicationElement = siteCollection.CreateElement("application");
      applicationElement["path"] = name;
      applicationElement["applicationPool"] = appPoolName;
      ConfigurationElementCollection applicationCollection = applicationElement.GetCollection();
      ConfigurationElement virtualDirectoryElement = applicationCollection.CreateElement("virtualDirectory");
      virtualDirectoryElement["path"] = @"/";
      virtualDirectoryElement["physicalPath"] = physicalPath;
      applicationCollection.Add(virtualDirectoryElement);
      siteCollection.Add(applicationElement);
      serverManager.CommitChanges();
    }

通常,calss只是IIS配置文件的一个薄包装器。您可以通过查看现有文件,甚至通过查看IIS管理器中必须手动配置服务,然后将其转换为生成的配置更改来理解它。

您可以通过这种方式完成所有(至少很多)IIS配置(例如,配置应用程序限制,启用身份验证方案等)。

配置的WCF部分只是普通的WCF。您可以在代码或配置中执行此操作。

答案 1 :(得分:0)

您要找的是Publish。您可以从WCF服务项目上的右键单击上下文菜单中找到它。您可以从那里发布或创建一个包以便稍后发布或将其分发到远程站点。网上有很多教程。

如果您对此功能有特定疑问,请随时提出。

答案 2 :(得分:0)

相关问题