Wcf-creating-service

提供:Dev Guides
移動先:案内検索

WCF-WCFサービスの作成

WCFサービスの作成は、Microsoft Visual Studio 2012を使用した簡単なタスクです。 以下に、概念をよりよく理解するために、必要なすべてのコーディングとともにWCFサービスを作成するための段階的な方法を示します。

  • Visual Studio 2012を起動します。 *新しいプロジェクトをクリックし、Visual C#タブでWCFオプションを選択します。

WCF Creating Service1

加算、減算、乗算、除算などの基本的な算術演算を実行する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;
         }
      }
   }
}

このサービスを実行するには、Visual Studioの[スタート]ボタンをクリックします。

Wcf Creating Service4

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

Wcf Creating Service5

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

Wcf Creating Service6 Wcf Creation Service7

合計と同様に、メニューにリストされている他のすべての算術演算を実行できます。 そして、ここにそれらのスナップがあります。

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

Wcf Creating Service8

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

Wcf Creating Service9

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

Wcf Creating Service10

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

Wcf Creating Service11