Architect's Log

I'm a Cloud Architect. I'm highly motivated to reduce toils with driving DevOps.

WCFサービスライブラリの作成

こちら(以下のエントリー)ではプロジェクトテンプレートを使わずに全てのソースコードを手動で記述して作成しました。
WCFサービスの作成 - プログラマーな日々
WCFサービスを作成、起動します。 ...

今回はWCFサービスライブラリのプロジェクトテンプレートを使用して、WCFサービスを作成します。

WCFサービスを作成する

新しいプロジェクトを作成します。

テンプレートは[WCFサービスライブラリ]を選択します。

IService1.csのソースコードを修正します。
using System.ServiceModel;

namespace WcfServiceLibrary1 {
    // メモ: ここでインターフェイス名 "IService1" を変更する場合は、App.config で "IService1" への参照も更新する必要があります。
    [ServiceContract]
    public interface IService1 {
        [OperationContract]
        string GetMessage(string inputMessage);

        // タスク: ここにサービス操作を追加します。
    }
}
Service1.csのソースコードを修正します。
using System;

namespace WcfServiceLibrary1 {
    // メモ: ここでクラス名 "Service1" を変更する場合は、App.config で "Service1" への参照も更新する必要があります。
    public class Service1 : IService1 {
        public string GetMessage(string inputMessage) {
            Console.WriteLine("'GetMessagega'が呼び出されました。");
            return "ようこそ、" + inputMessage;
        }
    }
}
App.configは修正しません。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- サービス ライブラリ プロジェクトの展開時に、構成ファイルの内容をホストの app.config ファイルに追加する
  必要があります。System.Configuration は、ライブラリの構成ファイルをサポートしていません。 -->
  <system.serviceModel>
    <services>
      <service name="WcfServiceLibrary1.Service1" behaviorConfiguration="WcfServiceLibrary1.Service1Behavior">
        <host>
          <baseAddresses>
            <add baseAddress = "http://localhost:8731/Design_Time_Addresses/WcfServiceLibrary1/Service1/" />
          </baseAddresses>
        </host>
        <!-- Service Endpoints -->
        <!-- アドレスは、完全修飾でない限り、上で指定されたベース アドレスに相対的なものとなります -->
        <endpoint address ="" binding="wsHttpBinding" contract="WcfServiceLibrary1.IService1">
          <!-- 
              展開時に、次の ID 要素を削除または置換して、展開されたサービスが 
              実行されている ID が反映されるようにする必要があります。削除されると、WCF は適切な IDを自動的に 
              推測します。
          -->
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <!-- Metadata Endpoints -->
        <!-- Metadata Exchange エンドポイントは、サービスが、そのサービス自体をクライアントに記述するために使用されます。 --> 
        <!-- このエンドポイントは、セキュリティで保護されたバインドを使用していないため、展開する前にセキュリティで保護するか、削除する必要があります -->
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="WcfServiceLibrary1.Service1Behavior">
          <!-- メタデータ情報の開示を避けるには、
          展開する前に、下の値を false に設定し、上のメタデータのエンドポイントを削除します -->
          <serviceMetadata httpGetEnabled="True"/>
          <!-- デバッグ目的で障害発生時の例外の詳細を受け取るには、
          下の値を true に設定します。例外情報の開示を避けるには、
          展開する前に false に設定します -->
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

次回は、

このWCFサービスライブラリをホストするアプリケーションを作成します。