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

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

单击Subtract方法时会出现以下页面.输入整数,单击Invoke按钮,得到如下所示的输出 :

Wcf Creating Service8

单击"乘法"方法时会出现以下页面.输入整数,单击Invoke按钮,得到输出,如下所示 :

Wcf Creating Service9

单击Divide方法时会出现以下页面.输入整数,单击Invoke按钮,得到如下所示的输出 :

Wcf Creating Service10

调用服务后,您可以直接从这里切换它们.

Wcf Creating Service11