【WCF】【C#】WCF ~ App.configを付加 ~

■ はじめに

https://blogs.yahoo.co.jp/dk521123/31872515.html
に、App.configを使って、実装してみる。比較するといいかも。

■ サンプル

サービスの実装

https://d.hatena.ne.jp/tekk/20091101/1257050131
を参考にApp.configを修正する
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
      <behaviors >
      <serviceBehaviors>
        <behavior name ="SampleServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="SampleServiceBehavior"
               name="SampleWcfService.SampleWcfService">
        <endpoint address="Plus"
                  binding="basicHttpBinding"
                  name="basicHttp"
                  contract ="SampleWcfService.ISampleWcfService" />
        <host>
          <baseAddresses>
            <add baseAddress="">http://localhost:8888/SampleWcfService"/>
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>
</configuration>
Form1.cs
using System;
using System.ServiceModel;
using System.Windows.Forms;
using System.Diagnostics;

namespace SampleWcfService
{
    public partial class Form1 : Form
    {
        ServiceHost host;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                this.host = new ServiceHost(typeof(SampleWcfService));
                this.host.Open();
                this.label1.Text = "Start WCF service";
            }
            catch (Exception ex)
            {
                this.label1.Text = ex.Message;
            }
        }

        private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (this.host != null)
            {
               this.host.Close();
            }
        }
    }

    [ServiceContract]
    public interface ISampleWcfService
    {
        [OperationContract]
        int Plus(int value1, int value2);
    }

    public class SampleWcfService : ISampleWcfService
    {
        public int Plus(int value1, int value2)
        {
            Trace.WriteLine("Called me?");
            return value1 + value2;
        }
    }
}

クライアントの実装

http://d.hatena.ne.jp/tekk/20091101/1257087729
を参考にApp.configを修正する
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <client>
      <endpoint
        name="SampleClientEndpoint"
        address="http://localhost:8888/SampleWcfService/Plus"
        binding="basicHttpBinding"
        contract="SampleWcfClient.ISampleWcfService" />
    </client>
  </system.serviceModel>
</configuration>
Form1.cs
using System;
using System.ServiceModel;
using System.Windows.Forms;

namespace SampleWcfClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (ChannelFactory<ISampleWcfService> channel =
                new ChannelFactory<ISampleWcfService>("SampleClientEndpoint"))
            {
                ISampleWcfService service = channel.CreateChannel();
                int value1 = Convert.ToInt32(this.textBox1.Text);
                int value2 = Convert.ToInt32(this.textBox2.Text);
                this.label1.Text = service.Plus(value1, value2).ToString();
                channel.Close();
            }
        }
    }

    [ServiceContract]
    public interface ISampleWcfService
    {
        [OperationContract]
        int Plus(int value1, int value2);
    }
}