Getting Started with WCF (v)---creating a WCF Service

Source: Internet
Author: User
Tags integer numbers

Using Microsoft Visual Studio2012 to create a WCF service, understand all the necessary encodings below, and better create the concept of a WCF service, here's a simple task.

    • Start Visual Studio 2012.

    • Click New Project, and then in the Visual C # tab, select the WCF option.

WCF services are created to perform basic arithmetic operations such as addition, subtraction, multiplication, and division. The main code is in two different files-an interface and a class.

A WCF contains one or more interfaces and implementation classes.

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] IntSum(IntNum1, IntNum2); [OperationContract] Int Subtract(IntNum1, IntNum2); [OperationContract] Int Multiply(IntNum1, IntNum2); [OperationContract] Int Divide(IntNum1, IntNum2); } Use a data contract as illustrated in the sample below to add composite types to service operations. [DataContract] Public Class Compositetype { BoolBoolvalue= True; StringStringValue= "Hello"; [DataMember] Public bool Boolvalue { Get { ReturnBoolvalue; } Set {Boolvalue=Value; } } [DataMember] public  string stringvalue { get { return } StringValue;  } set { stringvalue = value;  } } }}< /c19> 

And behind it is the code for the class,

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 Return summation of numbersPublicint sum(IntNum1, IntNum2) { ReturnNum1+Num2; } This function returns subtraction of the numbers. If NUM1 is smaller than number and then this function returns 0.PublicintSubtract(IntNum1, IntNum2) { If (Num1>Num2) { ReturnNum1-Num2; } Else { Return 0; } } This function returns multiplication of both integer numbers.PublicintMultiply(IntNum1, IntNum2) { ReturnNum1*Num2; } This function returns the integer value of the number of the number. If num2 is 0 then this function returns 1.Publicintdivide(IntNum1, IntNum2) { if  (num2 !=  0)  {return  (num1 / Num2 } else  { return 1;} } }< Span class= "pun" >               

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

When we run this service, the following screen will appear.

On the click on the sum method, the following page will be opened. Here, you can enter any two integers, and then click the Invoke button. The service will return the sum of these two numbers.

Like sums, we can execute all the arithmetic operations that are listed in the menu. Here is catching them.

When you click on the next page will appear in the SUTBTARCT method. Enter an integer and click the Invoke button to get the output as shown below.

The next page will appear when the multiply method is clicked. Enter an integer and click the Invoke button to get the output as shown below.

The following page appears when you click the Divide method. Enter an integer and click the Invoke button to get the output as shown below.

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

Original address: http://www.yiibai.com/wcf/wcf_creating_services.html

Getting Started with WCF (v)---creating a WCF Service

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.