WCF - 创建 WCF 服务



使用 Microsoft Visual Studio 2012 创建 WCF 服务是一项简单的任务。以下是用于创建 WCF 服务的分步方法,包括所有必需的编码,以便更深入地理解该概念。

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

创建了一个 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 Creating Service4

在运行此服务时,将出现以下屏幕。

Wcf Creating Service5

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

Wcf Creating Service6 Wcf Creating Service7

与求和类似,我们可以执行菜单中列出的所有其他算术运算。以下是它们的快照。

单击减法方法时,将出现以下页面。输入整数,单击“调用”按钮,并获取如下所示的输出 -

Wcf Creating Service8

单击乘法方法时,将出现以下页面。输入整数,单击“调用”按钮,并获取如下所示的输出 -

Wcf Creating Service9

单击除法方法时,将出现以下页面。输入整数,单击“调用”按钮,并获取如下所示的输出 -

Wcf Creating Service10

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

Wcf Creating Service11
广告
© . All rights reserved.