And*_*air 0 wcf iis-7.5 c#-3.0
假设我们有两个具有以下布局的项目
void Application_Start(System.Object sender, System.EventArgs e)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>
Run Code Online (Sandbox Code Playgroud)
所以...现在......某个地方(如上所述,我想到global.asax)我需要注册,当浏览到URI wcf.DemoService得到解决时,对于mex-request,wcf.IDemoService解析读取属性以获得WSDL.
这通常通过.svc在第一行创建文件并放置标题来完成,例如:
<%@ ServiceHost Language="C#" Debug="true" Service="wcf.DemoService" %>
Run Code Online (Sandbox Code Playgroud)
在例如控制台应用程序中
var serviceHost = new ServiceHost(typeof (wcf.DemoService));
serviceHost.Open();
Run Code Online (Sandbox Code Playgroud)
并将其与host元素中的service元素结合以指定URI - 或者使用另一个ctor-overloadServiceHost
但我宁愿去静态注册(或任何web.config适用于IIS 7.5的注册) - 这可能吗?如果是这样,怎么样?
WCF 4(.NET 4.0)提供基于代码的服务注册和基于配置的服务注册.
基于代码的配置是通过新的ASP.NET路由实现的ServiceRoute:
RouteTable.Routes.Add(new ServiceRoute("DemoService",
new ServiceHostFactory(), typeof(wcf.DemoService));
Run Code Online (Sandbox Code Playgroud)
路由通常与REST服务一起使用,但它也适用于SOAP服务.
在配置中注册服务称为基于配置的激活.您将在web.config中定义虚拟.svc文件:
<serviceHostingEnvironment>
<serviceActivation>
<add relativeAddress="DemoService.svc" service="wcf.DemoService" />
</serviceActivation>
</serviceHostingEnvironment>
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,您只定义服务的相对路径,因为基址始终由IIS中托管的网站指定.