方法 : 構成にサービス エンドポイントを作成する
エンドポイントは、クライアントが Windows Communication Foundation (WCF) サービスによって提供される機能にアクセスできるようにします。エンドポイントの相対アドレスと絶対アドレスを組み合わせてサービスのエンドポイントを 1 つ以上定義できます。または、サービス エンドポイントを定義しない場合、ランタイムは既定で一部を提供します。このトピックでは、相対アドレスと絶対アドレスの両方を含んでいる構成ファイルを使用したエンドポイントの使用方法について説明します。
例
次のサービス構成では、1 つのベース アドレスと 5 つのエンドポイントを指定します。
<configuration>
<appSettings>
<!-- use appSetting to configure base address provided by host -->
<add key="baseAddress" value="https://localhost:8000/servicemodelsamples/service" />
</appSettings>
<system.serviceModel>
<services>
<!-- This section is optional with the default configuration introduced
in .NET Framework 4. -->
<service
name="Microsoft.ServiceModel.Samples.CalculatorService">
<host>
<baseAddresses>
<add baseAddress="https://localhost:8000/ServiceModelSamples/service"/>
</baseAddresses>
</host>
<endpoint address=""
binding="wsHttpBinding"
contract="Microsoft.ServiceModel.Samples.ICalculator" />
<endpoint address="/test"
binding="wsHttpBinding"
contract="Microsoft.ServiceModel.Samples.ICalculator" />
<endpoint address="https://localhost:8001/hello/servicemodelsamples"
binding="wsHttpBinding"
contract="Microsoft.ServiceModel.Samples.ICalculator" />
<endpoint address="net.tcp://localhost:9000/servicemodelsamples/service"
binding="netTcpBinding"
contract="Microsoft.ServiceModel.Samples.ICalculator" />
<!-- the mex endpoint is another relative address exposed at
https://localhost:8000/ServiceModelSamples/service/mex -->
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
<!--For debugging purposes set the includeExceptionDetailInFaults attribute to true-->
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
ベース アドレスは、次のサンプルのように add 要素を使用して service/host/baseAddresses の下に指定します。
<service
name="Microsoft.ServiceModel.Samples.CalculatorService">
<host>
<baseAddresses>
<add baseAddress="https://localhost:8000/ServiceModelSamples/service"/>
</baseAddresses>
</host>
次のサンプルの最初のエンドポイント定義では、相対アドレスを指定します。つまり、エンドポイント アドレスは、ベース アドレスと URI (Uniform Resource Identifier) 構造の規則に従った相対アドレスの組み合わせということを意味します。相対アドレスが空 ("") のため、エンドポイント アドレスはベース アドレスと同じになります。具体的には https://localhost:8000/servicemodelsamples/service です。
<endpoint address=""
binding="wsHttpBinding"
contract="Microsoft.ServiceModel.Samples.ICalculator" />
2 番目のエンドポイント定義でも、相対アドレスを指定します。次のサンプル構成を参照してください。相対アドレス "test" がベース アドレスの末尾に追加されています。具体的には https://localhost:8000/servicemodelsamples/service/test です。
<endpoint address="/test"
binding="wsHttpBinding"
contract="Microsoft.ServiceModel.Samples.ICalculator" />
3 番目のエンドポイント定義では、絶対アドレスを指定します。次のサンプル構成を参照してください。このアドレスでは、ベース アドレスは使用されていません。具体的には https://localhost:8001/hello/servicemodelsamples です。
<endpoint address="https://localhost:8001/hello/servicemodelsamples"
binding="wsHttpBinding"
contract="Microsoft.ServiceModel.Samples.ICalculator" />
4 番目のエンドポイント アドレスは、絶対アドレスと別のトランスポート (ここでは TCP) を指定しています。このアドレスでは、ベース アドレスは使用されていません。具体的には net.tcp://localhost:9000/servicemodelsamples/service です。
ランタイムによって提供された既定のエンドポイントを使用するには、コードまたは構成ファイルでサービス エンドポイントを指定しないでください。次の例では、サービスを開くときに、ランタイムは既定のエンドポイントを作成します。既定のエンドポイント、バインディング、および動作詳細情報、「簡略化された構成」および「WCF サービスの簡略化された構成」を参照してください。
<configuration>
<appSettings>
<!-- use appSetting to configure base address provided by host -->
<add key="baseAddress" value="https://localhost:8000/servicemodelsamples/service" />
</appSettings>
<system.serviceModel>
<!--For debugging purposes set the includeExceptionDetailInFaults attribute to true-->
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>