今天來看看WCF的配置方法。

上圖整理了服務配置過程中所用到的基本的元素,大致的步驟主要是首先要在調用服務的程序集中添加服務的一個引用,然后添加一個service并指定服務的名稱、終結點,如果添加了behavior(行為)的配置,那么也需要添加一個behaviorConfiguration的配置信息。在添加一個service時會在其中指定終結點的信息,終結點說的就是服務的具體信息訪問方式,在終結點中添加服務address及binding信息和contract(服務契約)信息等。在endpoint中添加的binding只是指定了服務綁定的訪問方式,例如:basicHttpBinding等,真正的binding配置是需要在另外的binding中添加的,添加好后需要配置在endpoint的bindingConfiguration。下面看一個服務在客戶端部署配置的示例。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<!--The bindings configuration
We deployed the basichttpbinding.
I added a binding that name is binding1.
The binding1 used the security.
-->
<bindings>
<basicHttpBinding>
<binding name="binding1">
<security mode="Message">
<message clientCredentialType="Certificate"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<!--The behaviors configuration
I added a serviceBehavior that has nothing configuration in.
-->
<behaviors>
<serviceBehaviors>
<behavior name="service1_behavior">
</behavior>
</serviceBehaviors>
</behaviors>
<!--The services configuration
I added a service that behaviorConfiguration is service1_behavior.
This service has an endpoint that added the property and the binding.
-->
<services>
<service name="Wcfservice.Service1" behaviorConfiguration="service1_behavior">
<endpoint address="http://localhost:64921/Service1.svc" name="ServiceReference1_IService1"
binding="basicHttpBinding" bindingConfiguration="binding1" contract="ServiceReference1.IService1"></endpoint>
</service>
</services>
</system.serviceModel>
</configuration>
上例將配置信息寫入到了xml中,分別添加了一個服務的binding、service和behavior,在添加時往往是自下向上添加,首先添加一個binding配置信息,然后添加相應的behavior行為,最后添加一個service并把綁定信息添加到其中。
service
service主要是用來配置endpoint和host的,它實際上代表了為程序集添加了一個服務的引用,其中的endpoint指定了服務的地址、綁定和協議,host則提供了服務寄宿的方式。
如下配置:
<services>
<service name="Wcfservice.Service1">
<host>
<baseAddresses>
<add baseAddress="http://localhost:64921/ConsoleApplication1"/>
</baseAddresses>
</host>
<endpoint address="http://localhost:64921/WcfService1.IService1" binding="basicHttpBinding"
bindingConfiguration="binding1" contract="ServiceReference1.IService1"></endpoint>
</service>
</services>
在上面的代碼中添加了一個service,并為service配置了一個host該host的應用程序即為當前引用服務的程序集。最后添加了一個endpoint,終結點中綁定了URI,URI的地址就是WCF的定義地址,其中的binding是指定了基本的綁定類型,另外還使用contract指定了服務契約的接口。
binding
service是對服務進行的配置,指定了服務的一些配置信息,另外很重要的和service同級還有binding,它是對消息訪問方式做的一些配置。
1、綁定方式
分為系統自帶的綁定和用戶自定義綁定兩種,系統自帶的綁定包括basicHttpBinding、WcHttpBinding等。
如果系統提供的綁定功能不完全,那么也可以使用用戶自定義的綁定功能,可以使用customBinding對象從預先存在的綁定元素中創建新的綁定,也可以通過從Binding派生類來創建完全由用戶自定義的綁定。
2、基本功能
綁定除了定義綁定的方式外,還可以指定傳輸協議的類型、安全性、編碼方式和事務等,通過綁定來配置WCF的基本操作類型,這樣能夠對服務做詳細的一些配置,使服務的功能更加健全。
<?xml version=”1.0” encoding=”utf-8” ?>
<configuration>
<system.serviceModel>
<services>
<service name =”WCFService.ServiceClass” behaviorConfiguration=”metadataSupport”>
<host>
<baseAddresses>
<add baseAddress=”net.pipe://localhost/WCFService”/>
<add baseAddress=”net.tcp://localhost:8000/WCFService”/>
<add baseAddress=”http://localhost:8080/WCFService”/>
</baseAddresses>
</host>
<endpoint address=”tcpmex” binding=”mexTcpBinding”
contract=”IMetadataExchange”/>
<endpoint address=”namedpipemex” binding=”
mexNamedPipeBinding” contract=”IMetadataExchange”/>
<endpoint address=”” binding=”wsHttpBinding”
contract=”WCFService.IServiceClass”/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name=”metadataSupport”>
<serviceMetadata httpGetEnabled=”false” httpGetUrl=””/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
Behavior
行為屬性,行為屬性可以控制服務的運行時特性,主要分為服務行為和操作行為,這些行為或特性,可以通過配置runtime屬性配置文件,或自定義行為來實現。
<?xml version=”1.0” encoding=”utf-8” ?>
<configuration>
<system.serviceModel>
<services>
<service name =”WCFService.ServiceClass” behaviorConfiguration=”metadataSupport”>
<host>
<baseAddresses>
<add baseAddress=”net.pipe://localhost/WCFService”/>
<add baseAddress=”net.tcp://localhost:8000/WCFService”/>
<add baseAddress=”http://localhost:8080/WCFService”/>
</baseAddresses>
</host>
<endpoint address=”tcpmex” binding=”mexTcpBinding” contract=”IMetadataExchange”/>
<endpoint address=”namedpipemex” binding=”mexNamedPipeBinding” contract=”IMetadataExchange”/>
<endpoint address=”” binding=”wsDualHttpBinding” contract=”WCFService.IServiceClass”/>
<!--<endpoint address=”mex” binding=”mexHttpBinding” contract=”IMetadataExchange”/>-->
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name=”metadataSupport”>
<!--允許客戶端偵聽服務器日志,在正式運行前最好關閉-->
<serviceDebug includeExceptionDetailInFaults=”true”/>
<serviceMetadata httpGetEnabled=”false” httpGetUrl=””/>
<serviceThrottling maxConcurrentCalls=”10” maxConcurrentInstances=”5” maxConcurrentSessions=”5”/>
<serviceSecurityAudit auditLogLocation=”Application” suppressAuditFailure=”false”/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
Note:serviceMetadata 是一種元數據啟用功能,它是配置元數據終結點,默認情況下是不公開元數據的,但是可以通過啟用配置來公開元數據的終結點。
上面的代碼都是使用的是配置文件做的服務的配置部署,另外也可在程序中編寫代碼來配置部署信息,但是并不贊成這種配置方式,因為這種配置方式不易更改,當你部署到客戶環境后就不能再更改內部的代碼結構,所以這種方式很不靈活,并不提倡使用這種方式來配置服務,但是可以作為了解,如下代碼:
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(ServiceReference1.Service1Client)))
{
host.AddServiceEndpoint(typeof(ServiceReference1.IService1),
new WSHttpBinding(), "http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1");
if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
{
ServiceMetadataBehavior serviceMetadata = new ServiceMetadataBehavior();
serviceMetadata.HttpGetEnabled = true;
serviceMetadata.HttpGetUrl = new Uri
("http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1/metadata");
host.Description.Behaviors.Add(serviceMetadata);
}
host.Open();
ServiceReference1.IService1 service1 = new ServiceReference1.Service1Client();
service1.GetData(1);
Console.Write("fdsf");
host.Close();
}
}
}
}
上面的代碼中使用的是WSHttpBinding方式它支持雙工通信,另外上面的宿主方式使用的是ConsoleApplication Host,這種host方式非常簡單。在創建宿主程序時,需要為宿主指定宿主服務的類型,這里的類型要使用實現服務的類,最好不要使用接口類型。
創建宿主對象后,接下來為宿主添加了一個服務的終結點,終結點中第一個參數是指定了協議實現的類型,本例使用的是接口協議,所以要配置為相應的接口類型;第二個參數指定了綁定的類型;第三個參數則指定了終結點的URI地址,URI地址要配置服務具體實現的類URL地址。
接下來為宿主添加了一個行為(Behavior),并為行為公開了元數據,這種行為在創建時也可以不強加給服務,也就是說在添加宿主時,服務的行為定義是可選的,也可以不定義。
結語
本文主要針對WCF在客戶端進行配置時所使用的基本的配置節做了詳細的討論,主要是Service、Binding和Behavior的應用,另外需要注意的是在客戶端進行服務配置時不建議采用代碼配置的方法,最好使用xml文件進行發布配置,這樣能很好的修改。最后還有在添加WCF時一定要根據不同的類別添加需要的WCF,WCF中有類庫和Application兩種,它們所針對的Host是不同的,如果是類庫的話往往用來配置Console Application作為Host,如果是Application類的往往用來配置網站服務,否則在使用時很容易出錯。
來自 http://www.uml.org.cn/zjjs/201503125.asp
相關參考資料:
http://www.uml.org.cn/zjjs/201503125.asp
http://www.cnblogs.com/weichuo/archive/2008/07/09/1238979.html