📜  WCF-创建WCF服务

📅  最后修改于: 2020-11-19 09:20:12             🧑  作者: Mango


创建WCF服务是使用Microsoft Visual Studio 2012的一项简单任务。下面是逐步创建WCF服务以及所有必需的编码的逐步方法,以更好地理解该概念。

  • 启动Visual Studio 2012。
  • 单击新项目,然后在Visual C#选项卡中,选择WCF选项。

WCF创建服务1

将创建一个WCF服务,该服务执行基本的算术运算,例如加,减,乘和除。主要代码位于两个不同的文件中-一个接口和一个类。

WCF包含一个或多个接口及其实现的类。

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创建服务4

当我们运行此服务时,将显示以下屏幕。

Wcf创建服务5

单击sum方法后,将打开以下页面。在这里,您可以输入任意两个整数,然后单击“调用”按钮。该服务将返回这两个数字的总和。

Wcf创建服务6Wcf创建服务7

像求和一样,我们可以执行菜单中列出的所有其他算术运算。这是给他们的短片。

单击“减法”后将显示以下页面。输入整数,单击Invoke按钮,并获得输出,如下所示:

Wcf创建Service8

单击“乘”方法时,将显示以下页面。输入整数,单击Invoke按钮,并获得输出,如下所示:

Wcf创建Service9

单击“除法”时将出现以下页面。输入整数,单击Invoke按钮,并获得输出,如下所示:

Wcf创建服务10

调用服务后,您可以从此处直接在它们之间切换。

Wcf创建服务11