Architect's Log

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

構成ファイルを使用する(サービス編)

前回(以下のエントリー)で作成したWCFサービスの設定を構成ファイルに外出しします。
WCFをはじめました(サービス編) - プログラマーな日々
WCFをはじめました。 今回はWCFサービスを作成します。 ...

クライアントは次回作成します。

準備

  1. コンソールアプリケーションのプロジェクトを作成する。
  2. System.ServiceModelへの参照を追加する。

コントラクトのソースコード

前回から変更なし。

using System.ServiceModel;

namespace HelloWorld {
    [ServiceContract]
    public interface IStockService {
        [OperationContract]
        double GetPrice(string ticker);
    }

    public class StockService : IStockService {
        public double GetPrice(string ticker) {
            return 94.85;
        }
    }
}

Mainのソースコード

アドレス、バインディング、コントラクトの情報を外出ししたので、かなり短くなりました。

using System;
using System.ServiceModel;

namespace HelloWorld {
    public class Service{
        static void Main() {
            ServiceHost serviceHost
                = new ServiceHost(typeof(StockService));

            serviceHost.Open();
            Console.ReadLine();
            serviceHost.Close();
        }
    }
}

app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <services>
            <service name="HelloWorld.StockService">
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:8000/HelloWorld"/>
                    </baseAddresses>
                </host>
                <endpoint
                    address=""
                    binding="basicHttpBinding"
                    contract="HelloWorld.IStockService" />
            </service>
        </services>
    </system.serviceModel>
</configuration>

アプリ実行


クライアントからのアクセスを待機しています。