前两篇写了在IIS7下部署SVC文件引导WCF服务,采用了net.tcp文件,但是实际的开发中,由于项目的架构已经确定,不宜做大的改动,且为了试项目组成员开发方便,我们之前的项目架构是这样的:
主体是WebHost,这是用来托管WCF服务的,放弃了ConsoleHost,那么现在的问题是能不能仅仅通过配置文件来托管服务,不必通过SVC文件的连接呢,MSDN上给出了一个很好的解决方案:http://msdn.microsoft.com/zh-cn/library/ee816902(en-us,VS.100).aspx
主要使用的是一个配置元素<serviceActivations>,MSDN给出的说明是:
一个配置元素,用于添加可定义虚拟服务激活设置(映射到 Windows Communication Foundation (WCF) 服务类型)的设置。使用此配置元素可以在不使用 .svc 文件的情况下激活承载在 WAS/IIS 中的服务。
主要语法是:
< SERVICEHOSTINGENVIRONMENT > |
< ADD factory = "String" service = "String" /> |
</ SERVICEHOSTINGENVIRONMENT > |
那么有了这个好组件,我们就拿来改造现有的服务吧。
首先,之前的服务配置是
< SERVICE behaviorconfiguration = "MyBehavior" name = "WCFLib.Add" > |
< ENDPOINT address = "" binding = "netTcpBinding" contract = "WCFLib.IAdd" bindingconfiguration = "netTcpBindConfig" ></ ENDPOINT > |
< ENDPOINT address = "mex" binding = "mexTcpBinding" contract = "IMetadataExchange" ></ ENDPOINT > |
我们为这个配置增加服务激活配置
< SERVICEHOSTINGENVIRONMENT > |
< ADD service = "WCFLib.Add" relativeaddress = "NewAddService.svc" /> |
</ SERVICEHOSTINGENVIRONMENT > |
来看一下现在完整的配置
< BINDING name = "netTcpBindConfig" > |
< TRANSPORT clientcredentialtype = "Windows" protectionlevel = "EncryptAndSign" /> |
< MESSAGE clientcredentialtype = "Windows" /> |
< SERVICEHOSTINGENVIRONMENT > |
< ADD service = "WCFLib.Add" relativeaddress = "NewAddService.svc" /> |
</ SERVICEHOSTINGENVIRONMENT > |
< SERVICE behaviorconfiguration = "MyBehavior" name = "WCFLib.Add" > |
< ENDPOINT address = "" binding = "netTcpBinding" contract = "WCFLib.IAdd" bindingconfiguration = "netTcpBindConfig" ></ ENDPOINT > |
< ENDPOINT address = "mex" binding = "mexTcpBinding" contract = "IMetadataExchange" ></ ENDPOINT > |
< BEHAVIOR name = "MyBehavior" > |
< SERVICETHROTTLING maxconcurrentcalls = "1000" maxconcurrentinstances = "1000" maxconcurrentsessions = "1000" /> |
< SERVICEMETADATA httpgetenabled = "true" /> |
< SERVICEDEBUG includeexceptiondetailinfaults = "true" /> |
< DATACONTRACTSERIALIZER maxitemsinobjectgraph = "6553600" /> |
删除SVC文件后的目录是这样的
那么怎么使用这个没有SVC文件的WCF服务呢,很简单,我们只要认为有这个服务就可以了,删除之前的Client中的引用,我们重新添加引用一下
再运行一下试试看