How can I register a WCF service programmatically within an IIS environment

风格不统一 提交于 2019-12-04 06:51:36

问题


Let's say we have 2 projects with following layout

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

web.config would look like this

<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>

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.

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" %>

In e.g. a console application by

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

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

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?


回答1:


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

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

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

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

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>

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.



来源:https://stackoverflow.com/questions/11522733/how-can-i-register-a-wcf-service-programmatically-within-an-iis-environment

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!