[ エンドポイントを複数用意する方法 ]
エンドポイントを複数用意するには、WCFサービスのコンフィギュレーションに endpoint セクションを複数用意し、address 属性にそれぞれ重複しないように適当な値を設定します。また、必須ではありませんが、各 endpoint セクションの name 属性にてエンドポイントのコンフィギュレーション名を設定しておくと、WCF クライアントアプリケーションのコンフィギュレーションにもそのコンフィギュレーション名が反映され、クライアントで利用しやすくなります。 ( 用意しない場合、クライアントのコンフィギュレーションでは、 "バインディングセット名_コントラクト名" という命名規則に、連番が振られた名称が設定されます。 )
エンドポイントを複数用意する場合、WCF クライアントアプリケーションのサービスプロキシクラスでは、どのエンドポイントを利用するかを明示的に指定する必要があります。これは、サービスプロキ シクラスのコンストラクタで、エンドポイントコンフィギュレーション名を指定します。
[ 例 ]
例えば、一つのWCFサービスでSOAP1.1とSOAP1.2の両方に対応させたい場合、以下のように設定します。
<services>
<service behaviorConfiguration="MyServiceBehavior" name="MyService">
<endpoint name="MyServiceEndpointSoap1.2" binding="customBinding" bindingConfiguration="MyServiceBindingSoap1.2" contract="IMyService" address="soap1.2" />
<endpoint name="MyServiceEndpointSoap1.1" binding="customBinding" bindingConfiguration="MyServiceBindingSoap1.1" contract="IMyService" address="soap1.1" />
</service>
</services>
<bindings>
<customBinding>
<binding name="MyServiceBindingSoap1.2" >
<mtomMessageEncoding messageVersion="Soap12" />
<httpTransport />
</binding>
<binding name="MyServiceBindingSoap1.1" >
<mtomMessageEncoding messageVersion="Soap11" />
<httpTransport />
</binding>
</customBinding>
</bindings>
このWCFサービスのクライアントアプリケーションでは、以下のように、どのエンドポイントを利用するかを明示的に指定する必要があります。
// SOAP1.2 が適用されるエンドポイントを利用する場合
using (MyServiceClient client = new MyServiceClient("MyServiceEndpointSoap1.2"))
{
client.MyOperation1();
}
[ おまけ ]
セルフホストで設定をハードコーディングする場合の例を以下に示します。
{
Type serviceContractType = typeof(IMyService);
Type serviceImplementedType = typeof(MyService);
HttpTransportBindingElement myServiceHttpTransportBindingElement = new HttpTransportBindingElement();
MtomMessageEncodingBindingElement myServiceMtomMessageEncodingBindingElementSoap12 = new MtomMessageEncodingBindingElement(MessageVersion.Soap12, Encoding.UTF8);
MtomMessageEncodingBindingElement myServiceMtomMessageEncodingBindingElementSoap11 = new MtomMessageEncodingBindingElement(MessageVersion.Soap11, Encoding.UTF8);
CustomBinding myServiceBindingSoap12 = new CustomBinding();
myServiceBindingSoap12.Elements.Add(myServiceMtomMessageEncodingBindingElementSoap12);
myServiceBindingSoap12.Elements.Add(myServiceHttpTransportBindingElement);
CustomBinding myServiceBindingSoap11 = new CustomBinding();
myServiceBindingSoap11.Elements.Add(myServiceMtomMessageEncodingBindingElementSoap11);
myServiceBindingSoap11.Elements.Add(myServiceHttpTransportBindingElement);
ServiceMetadataBehavior myServiceMetadataBehavior = new ServiceMetadataBehavior();
myServiceMetadataBehavior.HttpGetEnabled = true;
Uri myServiceBaseAddress = new Uri("http://localhost:8080/Test/MyService");
using (ServiceHost host = new ServiceHost(serviceImplementedType, myServiceBaseAddress))
{
host.AddServiceEndpoint(serviceContractType, myServiceBindingSoap12, "soap1.2");
host.AddServiceEndpoint(serviceContractType, myServiceBindingSoap11, "soap1.1");
host.Description.Behaviors.Add(myServiceMetadataBehavior);
host.Open();
Console.WriteLine("WCF サービスのホスティングを開始しました。");
Console.WriteLine("{0} にてアクセスできます。", myServiceBaseAddress.AbsoluteUri);
Console.WriteLine("終了するにはEnterを押してください。");
Console.ReadLine();
host.Close();
}
}
トラックバックURL↓
http://csharper.blog57.fc2.com/tb.php/95-115528c4