如何在IIS环境中以编程方式注册WCF服务 [英] How can I register a WCF service programmatically within an IIS environment

查看:77
本文介绍了如何在IIS环境中以编程方式注册WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有2个具有以下布局的项目

Let's say we have 2 projects with following layout


  • 项目 web

    • global.asax(我想到了要在例如 void Application_Start(System.Object sender,System.EventArgs e )内进行注册的目的地)

    • web.config

    • Project "web"
      • global.asax (I thought of this destination for registration within eg void Application_Start(System.Object sender, System.EventArgs e)
      • web.config

      • DemoService.cs

      • IDemoService.cs

      web.config 看起来像这样

      <configuration>
          <system.serviceModel>
              <behaviors>
                  <serviceBehaviors>
                      <behavior name="fooBehavior">
                          <serviceMetadata httpGetEnabled="true" />
                      </behavior>
                  </serviceBehaviors>
              </behaviors>
              <services>
                  <service name="wcf.DemoService"
                           behaviorConfiguration="fooBehavior">
                      <endpoint address="mex"
                                binding="mexHttpBinding"
                                contract="IMetadataExchange" />
                      <endpoint address=""
                                binding="wsHttpBinding"
                                contract="wcf.IDemoService" />
                  </service>
              </services>
          </system.serviceModel>
      </configuration>
      

      所以...现在...某处(如上所述,我想到了 global.asax )我需要注册,当浏览到 URI wcf.DemoService 时,解决该问题并获得mex-request wcf.IDemoService 被解析为读取属性以获得WSDL。

      So ... now ... somewhere (as mentioned above I thought of global.asax) I need to register, that when browsing to URI wcf.DemoServiceget's resolved and for a mex-request the wcf.IDemoService gets resolved for reading the attributes to get a WSDL.

      通常通过创建 .svc 文件并将标题放在第一行来完成,例如:

      This would usually be done by creating a .svc file and put header in the first line, e.g.:

      <%@ ServiceHost Language="C#" Debug="true" Service="wcf.DemoService" %>
      

      例如控制台应用程序

      var serviceHost = new ServiceHost(typeof (wcf.DemoService));
      serviceHost.Open();
      

      并将其与 host 元素组合 service 元素来指定 URI -或使用 ServiceHost

      And combine this with a host element within the service element to specify the URI - or use another ctor-overload of ServiceHost

      但是我宁愿进行静态注册(或任何适用于IIS 7.5的 web.config 注册)-这可能吗?如果是,怎么办?

      But I would rather go for a static registration (or any web.config registration which works for IIS 7.5) - is this possible? If so, how?

      推荐答案

      WCF 4(.NET 4.0)提供基于代码的服务注册和基于配置的服务注册

      WCF 4 (.NET 4.0) offers both code based registration of services and configuration based registration of services.

      基于代码的配置是通过新的 ServiceRoute

      Code based configuration is achieved through ASP.NET Routing by new ServiceRoute:

      RouteTable.Routes.Add(new ServiceRoute("DemoService", 
                                new ServiceHostFactory(), typeof(wcf.DemoService));
      

      路由通常与REST服务一起使用,但它也适用于SOAP服务。

      Routes are usually used with REST services but it works for SOAP services as well.

      在配置中注册服务称为< a href = http://msdn.microsoft.com/en-us/library/dd807499.aspx>基于配置的激活。您将在web.config中定义虚拟.svc文件:

      Registering service in configuration is called configuration based activation. You will define virtual .svc file in web.config:

      <serviceHostingEnvironment>
         <serviceActivation>
            <add relativeAddress="DemoService.svc" service="wcf.DemoService" />
         </serviceActivation>
      </serviceHostingEnvironment>
      

      在这两种情况下,您都只定义了服务的相对路径,因为基址始终由网络指定网站托管在IIS中。

      In both cases you are defining only relative path to your service because base address is always specified by your web site hosted in IIS.

      这篇关于如何在IIS环境中以编程方式注册WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆