zoukankan      html  css  js  c++  java
  • WCF

    http://www.tutorialspoint.com/wcf/wcf_creating_service.htm

    Creating a WCF service is a simple task using Microsoft Visual Studio 2012. Given below is the step-by-step method for creating a WCF service along with all the requisite coding, to understand the concept in a better way.

    在VS2012中创建一个wcf服务是一个简单的任务。为了更好的理解之前的概念,按照下面的方法,一步一步的创建一个wcf服务,同时也包含了所有必须的编码

    • Launch Visual Studio 2012.   首先启动vs2012
    • Click on new project, then in Visual C# tab, select WCF option.   新建项目,然后选择模板-->C#-->WCF  右侧选择wcf库

    A WCF service is created that performs basic arithmetic operations like addition, subtraction, multiplication, and division. The main code is in two different files – one interface and one class.

    A WCF contains one or more interfaces and its implemented classes.

    我们创建一个执行基础算数运算的wcf服务:实现加减乘除。主要的代码在2个不同的文件中,一个接口另外是类

    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 numer1, int number2);
    
            [OperationContract]
            int Substract(int number1, int number2);
    
            [OperationContract]
            int Multiply(int number1, int number2);
    
            [OperationContract]
            int Divide(int number1, int number2);
        }
    
        // 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; }
            }
        }
    }

    The code behind its class is given below.  实现了接口的类如下:

    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 class name "Service1" in both code and config file 
        // together.
        public class Service1 : IService1
        {
            /// <summary>
            /// This Function Returns summation of two integer numbers
            /// </summary>
            /// <param name="number1"></param>
            /// <param name="number2"></param>
            /// <returns></returns>
            public int Sum(int number1, int number2)
            {
                return number1 + number2;
            }
    
            /// <summary>
            /// This function returns subtraction of two numbers. 
            /// If number1 is smaller than number2 then this function returns 0
            /// </summary>
            /// <param name="number1"></param>
            /// <param name="number2"></param>
            /// <returns></returns>
            public int Substract(int number1, int number2)
            {
                if (number1 > number2)
                {
                    return number1 - number2;
                }
                else
                {
                    return 0;
                }
            }
    
            /// <summary>
            /// This function returns multiplication of two integer numbers.
            /// </summary>
            /// <param name="number1"></param>
            /// <param name="number2"></param>
            /// <returns></returns>
            public int Multiply(int number1, int number2)
            {
                return number1 * number2;
            }
    
            /// <summary>
            /// This function returns integer value of two integer number. 
            /// If num2 is 0 then this function returns 1
            /// </summary>
            /// <param name="number1"></param>
            /// <param name="number2"></param>
            /// <returns></returns>
            public int Divide(int number1,int number2)
            {
                if (number2 != 0)
                {
                    return number1 / number2;
                }
                else
                {
                    return 1;
                }
            }
        }
    }

    To run this service, click the Start button in Visual Studio.

    While we run this service, the following screen appears.

    服务运行起来之后,会出现以下的界面。  双击某一个函数来执行相应的操作

    On clicking the sum method, the following page opens. Here, you can enter any two integer numbers and click on the Invoke button. The service will return the summation of those two numbers.

    双击了Sum方法后,会出现下面的页面。可以输入number和number2的值,然后点击调用。然后服务会返回2个数字的和。

    Like summation, we can perform all other arithmetic operations which are listed in the menu. And here are the snaps for them.

    The following page appears on clicking the Subtract method. Enter the integer numbers, click the Invoke button, and get the output as shown here:

    The following page appears on clicking the Multiply method. Enter the integer numbers, click the Invoke button, and get the output as shown here:

    The following page appears on clicking the Divide method. Enter the integer numbers, click the Invoke button, and get the output as shown here:

    Once the service is called, you can switch between them directly from here.

    一旦某一个方法的服务被调用后,可以直接通过tab和切换它们,也可以重新打开一个新的

  • 相关阅读:
    JVM运行参数优化详细教程
    idea 代码热启动配置方法
    Spring MVC拦截器完整代码示例
    Spring MVC异常处理代码完整实例
    Python3基础语法(20190617)
    Java代码生成器Easy Code
    Mybatis @Many注解一对多关联映射
    Mybatis @One注解使用
    Mybatis @ResultMap复用@Result
    Mybatis @Result注解的使用案例
  • 原文地址:https://www.cnblogs.com/chucklu/p/4629344.html
Copyright © 2011-2022 走看看