WCF は色んなところが関連付いていて、用語の定義が難しくなっている気がします。全体が見渡せるようになってからやっと用語の意味が理解できるようになる、というかなり高いハードルが WCF の普及を妨げているのかもしれませんね。自分なりのまとめをしておきます。
-
Service(サービス) とは
- ServiceContract 属性の付いた Interface を実装するクラス。次の例でいう MyWcf.Service1 のこと。
namespace MyWcf { [ServiceContract] public interface IService1 { [OperationContract] string GetData(int value); } } namespace MyWcf { public class Service1 : IService1 { public string GetData(int value) { return string.Format("You entered: {0}", value); } } }
- IIS でホストする場合の svc ファイル マークアップ内、Service 属性の値。次の例でいう MyWcf.Service1 のこと。
<%@ ServiceHost Language="C#" Debug="true" Service="MyWcf.Service1" CodeBehind="Service1.svc.cs" %>
- config ファイル、<system.serviceModel><serivces> 内の <service> 要素の name 属性値。次の例でいう MyWcf.Service1 のこと。
<system.serviceModel> <services> <service name="MyWcf.Service1"> <endpoint address="" binding="basicHttpBinding" contract="MyWcf.IService1" /> </service> </services>
- IServiceBehavior インターフェースの実装を適用することで拡張される、動作の単位。 例えば config ファイルで拡張設定をする場合、
次のように拡張設定を行います。 service 要素内の behaviorConfiguration で、どの serviceBehavior を使用するかを指定しています。(ここでは MyServiceBehavior を指定しています)<system.serviceModel> <services> <service name="MyWcf.Service1" behaviorConfiguration="MyServiceBehavior"> <endpoint address="" binding="basicHttpBinding" contract="MyWcf.IService1" /> </service> </services> <behaviors> <serviceBehaviors> <behavior name="MyServiceBehavior"> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> </behaviors>
- Endpoint を複数持つことができる。同一の Contract に対してアドレス違いや、Binding 違いなど。次の例はアドレス違いを示す。
もちろん contract 違いの Endpoint を作ることもできるが、その場合 service が複数の Interface を実装しているのが大前提。
というのも、name 属性の値は クラス名そのものなので、 contract 違い、ということは Interface 違い、ということになるから。
そして、それぞれの Interface (要するに contact )ごとに Endpointを作る、ということである。
でも、そんな Endpoint を必要とする要件はまずないんじゃないかなぁ・・・<system.serviceModel> <services> <service behaviorConfiguration="MyServiceBehavior" name="MyWcf.Service1"> <endpoint address="" binding="basicHttpBinding" bindingConfiguration="" contract="MyWcf.IService1" /> <endpoint name="anotherEP" address="another" binding="basicHttpBinding" bindingConfiguration="" contract="MyWcf.IService1" /> <host> <baseAddresses> <add baseAddress="http://localhost:2424/" /> </baseAddresses> </host> </service> </services>
ちなみにこの例のようにアドレス違いを設定する場合で 各 endpoint の address 属性の値を相対アドレスで設定する場合は <host> 要素内の <baseAddress> に相対アドレスの元となるベースアドレスを設定します。
- ServiceContract 属性の付いた Interface を実装するクラス。次の例でいう MyWcf.Service1 のこと。
-
Endpoint(エンドポイント)とは
- Service が外部に公開するもの。Address (アドレス)、Binding (バインディング)、Contract (コントラクト)をひとつにまとめたもの。
- IEndpointBehavior インターフェースの実装を適用することで拡張される、動作の単位。例えば config ファイルで拡張設定をする場合、
次のように拡張設定を行います。 endpoint 要素内の behaviorConfiguration で、どの endpointBehavior を使用するかを指定しています。(ここでは MyEndpointBehavior を指定しています)<system.serviceModel> <services> <service behaviorConfiguration="MyServiceBehavior" name="MyWcf.Service1"> <endpoint address="" binding="basicHttpBinding" behaviorConfiguration="MyEndpointBehavior" contract="MyWcf.IService1" /> </service> </services> <behaviors> <serviceBehaviors> <behavior name="MyServiceBehavior"> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> <endpointBehaviors> <behavior name="MyEndpointBehavior"> <dataContractSerializer ignoreExtensionDataObject="true" /> </behavior> </endpointBehaviors> </behaviors>
-
Address(アドレス)とは
- 外部からのアクセスできる、Endpoint の場所。
- 相対アドレスまたは絶対アドレスで指定。
- 細かく、そして正確には Address とは論理アドレスであり、物理アドレスとは異なる。WCF で指定しているのは 論理アドレス。でも物理アドレスが指定されていない場合は、論理アドレスを物理アドレスとして扱う。物理アドレスを別途指定する場合は endpoint 要素の ListernUri 属性を使用する。詳しくはWCF アドレス指定の詳細の物理アドレスと論理アドレスを参照。
-
Binding(バインディング)とは
- クライアントとの通信方法。プロトコル、エンコーディングなど。
- トランザクション、信頼性、セキュリティ、エンコード、トランスポートの5つの層から成る。 (必須はエンコードとトランスポートだけ)
- システム提供の既定 Binding と、自ら5つの層を構築して作成する CustomBinding がある。
多少は、MSDN も参考になる。
-
Contract(コントラクト)とは
- ~ Contract と付くものは意外にも多くて、ServiceContract, OperationConract, MessageContract, DataContract ほかにもあったかも。
- でも、Address, Binding, Contract (いわゆるABC)での Contract は、Interface を指すと思えばほぼ正解。 次の例でいう IService1 のこと。
namespace MyWcf { [ServiceContract] public interface IService1 { [OperationContract] string GetData(int value); } } namespace MyWcf { public class Service1 : IService1 { public string GetData(int value) { return string.Format("You entered: {0}", value); } } }
- 以下は MSDN からの引用。
- クライアントから呼び出すことができる操作。
- メッセージの形式。
- 操作を呼び出すために必要な入力パラメーターまたはデータの型。
- クライアントが予期できる処理メッセージまたは応答メッセージの種類