WCF如何配置

这篇文章将为大家详细讲解有关WCF如何配置,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

如果WCF配置为空,那么endpoint的地址就是默认的基地址(Base Address)。例如WCF配置的地址就是http://localhost/servicemodelsamples/service.svc,而IMetadataExchange服务的地址则为http://localhost/servicemodelsamples/service.svc/mex。这里所谓的基地址可以在<service>中通过配置<host>来定义:

<service name="Microsoft.ServiceModel.Samples.CalculatorService" behaviorConfiguration="CalculatorServiceBehavior"> <host> <baseAddresses> <add baseAddress=  "http://localhost/ServiceModelSamples/service.svc"/> </baseAddresses> </host> <endpoint … /> </service>

当我们在定义一个实现了Service Contract的类时, binding和address信息是客户端必须知道的,否则无法调用该服务。然而,如果需要指定服务在执行方面的相关特性时,就必须定义服务的behavior。在WCF中,定义behavior就可以设置服务的运行时属性,甚至于通过自定义behavior插入一些自定义类型。例如通过指定ServiceMetadataBehavior,可以使WCF服务对外公布Metadata。WCF配置如下:

<behaviors> <serviceBehaviors> <behavior name="metadataSupport"> <serviceMetadata httpGetEnabled="true" httpGetUrl=""/> </behavior> <serviceBehaviors> <behaviors>

WCF配置中,behavior被定义为Attribute,其中,System.ServiceModel.ServiceBehaviorAttribute和System.ServiceModel.OperationBehaviorAttribute是最常用的behavior。虽然,behavior作为Attribute可以通过编程的方式直接施加到服务上,但出于灵活性的考虑,将behavior定义到WCF配置文件中才是***的设计方式。

利用ServiceBehavior与OperationBehavior可以控制服务的如下属性:

<behaviors> <serviceBehaviors> <behavior name="metadataSupport"> <instanceContextMode httpGetEnabled="true" httpGetUrl=""/> </behavior> <serviceBehaviors> <behaviors>

关于“WCF如何配置”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。