Visual Studio 2015でWCFサービスを作成する手順を記載します。
WCFサービスを作成する
1.「ファイル」-「新規作成」-「WCFサービス」を選択します。
以上でWCfサービスが作成完了です。
動作テスト
ソリューションエクスプローラー上のService.svcを右クリックし「ブラウザで表示」を選択して表示したら動作テストOKです。
そのときのURL(http://localhost:54210/Service.svc)などをクライアントで使用します。
クライアントを作成する
1.「ファイル」-「新規作成」-「プロジェクト」を選択します。
2.Windowsフォームアプリケーション を選択し作成します。
3.WCFサービスのインターフェイス(IService.csなど)をクライアント側に記述します。
using System;
using System.Windows.Forms;
using System.ServiceModel;
namespace Client
{
[ServiceContract]
public interface IService
{
[OperationContract]
string GetData(int value);
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// エンドポイントを作成
EndpointAddress endPoint = new EndpointAddress("http://localhost:54210/Service.svc");
// プロキシを作成
IService proxy = ChannelFactory<IService>.CreateChannel(new BasicHttpBinding(), endPoint);
// WCFサービスのメソッドを呼び出します。
try
{
string outputMessage = proxy.GetData(10);
MessageBox.Show(outputMessage);
}
catch (Exception ex)
{
MessageBox.Show("呼び出しエラー:" + ex.Message);
}
}
}
}