3

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

4

3 回答 3

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。您可以在代码或配置中执行此操作。

于 2013-07-19T08:21:49.567 回答
0

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

如果您对此功能有任何具体问题,请随时提问。

于 2013-07-19T07:31:44.670 回答