WCF-WCFサービスの作成
WCFサービスの作成は、Microsoft Visual Studio 2012を使用した簡単なタスクです。以下に、概念をよりよく理解するために、必要なすべてのコーディングとともにWCFサービスを作成するための段階的な方法を示します。
- Visual Studio2012を起動します。
- 新しいプロジェクトをクリックし、[Visual C#]タブで[WCF]オプションを選択します。

加算、減算、乗算、除算などの基本的な算術演算を実行するWCFサービスが作成されます。メインコードは、2つの異なるファイル(1つのインターフェイスと1つのクラス)にあります。
WCFには、1つ以上のインターフェイスとその実装済みクラスが含まれています。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WcfServiceLibrary1 {
// NOTE: You can use the "Rename" command on the "Refactor" menu to
// change the interface name "IService1" in both code and config file
// together.
[ServiceContract]
Public interface IService1 {
[OperationContract]
int sum(int num1, int num2);
[OperationContract]
int Subtract(int num1, int num2);
[OperationContract]
int Multiply(int num1, int num2);
[OperationContract]
int Divide(int num1, int num2);
}
// Use a data contract as illustrated in the sample below to add
// composite types to service operations.
[DataContract]
Public class CompositeType {
Bool boolValue = true;
String stringValue = "Hello ";
[DataMember]
Public bool BoolValue {
get { return boolValue; }
set { boolValue = value; }
}
[DataMember]
Public string StringValue {
get { return stringValue; }
set { stringValue = value; }
}
}
}
そのクラスの背後にあるコードを以下に示します。
using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Runtime.Serialization;
usingSystem.ServiceModel;
usingSystem.Text;
namespace WcfServiceLibrary1 {
// NOTE: You can use the "Rename" command on the "Refactor" menu to
// change the class name "Service1" in both code and config file
// together.
publicclassService1 :IService1 {
// This Function Returns summation of two integer numbers
publicint sum(int num1, int num2) {
return num1 + num2;
}
// This function returns subtraction of two numbers.
// If num1 is smaller than number two then this function returns 0
publicint Subtract(int num1, int num2) {
if (num1 > num2) {
return num1 - num2;
}
else {
return 0;
}
}
// This function returns multiplication of two integer numbers.
publicint Multiply(int num1, int num2) {
return num1 * num2;
}
// This function returns integer value of two integer number.
// If num2 is 0 then this function returns 1.
publicint Divide(int num1, int num2) {
if (num2 != 0) {
return (num1 / num2);
} else {
return 1;
}
}
}
}
このサービスを実行するには、VisualStudioの[スタート]ボタンをクリックします。

このサービスを実行している間、次の画面が表示されます。

合計方法をクリックすると、次のページが開きます。ここでは、任意の2つの整数を入力して、[呼び出す]ボタンをクリックできます。サービスは、これら2つの数値の合計を返します。


合計と同様に、メニューにリストされている他のすべての算術演算を実行できます。そして、ここに彼らのためのスナップがあります。
減算方法をクリックすると、次のページが表示されます。整数を入力し、[呼び出し]ボタンをクリックして、次のように出力を取得します-

Multiplyメソッドをクリックすると、次のページが表示されます。整数を入力し、[呼び出し]ボタンをクリックして、次のように出力を取得します-

Divideメソッドをクリックすると次のページが表示されます。整数を入力し、[呼び出し]ボタンをクリックして、次のように出力を取得します-

サービスが呼び出されると、ここから直接切り替えることができます。
