These two days of study WCF, I saw MSDN The introductory tutorial on , And looked up some examples given online , A simple implementation , I think it's very suitable for a rookie like me .

1、 Create a new project --WCF--WCF Service application

It's using MSDN Addition, subtraction, multiplication and division on , So in the name section it says Calculator

Delete the Service1 and IService1 Two documents , Right click to add -- New item

Because the options on the left don't have WCF, So search directly WCF, add to WCF service , The name is Cal

Delete or comment out Cal Inside

//public void DoWork()

ICal Inside

 // void DoWork();

Because I can't use the above content

stay ICal Write the following code in

public interface ICal
double Add(double n1, double n2);
double Subtract(double n1, double n2);
double Multiply(double n1, double n2);
double Divide(double n1, double n2);

stay Cal Write in

 public class Cal : ICal
public double Add(double n1,double n2)
double result = n1 + n2;
return result;
public double Subtract(double n1,double n2)
double result = n1 - n2;
return result;
public double Multiply(double n1,double n2)
double result = n1 * n2;
return result;
public double Divide(double n1,double n2)
double result = n1 / n2;
return result;

The above two parts , A rookie like me can write , I believe you can see it . Don't write notes

Press F5 function , The following screen will appear

double-click Add(), To test , Of course , All four can be tested

stay n1 Enter a value at ,n2 Enter a value at , Click to call , Here we use the simple 1+1 To demonstrate , Occurrence value 2, It proved successful .

Right click Cal.svc file , Select View in browser

Try not to close this page , Because this article is only about testing , So it's not IIS Upper Department . If this page is not open , If there is an error running the client behind , You can try running this page

2、 Create a new one project , It's called WebCalculator

Then right-click to quote -- Add service reference

Enter the address underlined in the previous picture in the address section , Then click to go to , There will be... In the list of services Cal Appearance , Click ok

Create a new blank WEB forms WebTest.aspx

The front desk code is as follows

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<asp:Button ID="BtnAdd" runat="server" Text=" Add " OnClick="BtnAdd_Click" />
<asp:Button ID="BtnSub" runat="server" Text=" reduce " OnClick="BtnSub_Click" />
<asp:Button ID="BtnMul" runat="server" Text=" ride " OnClick="BtnMul_Click" />
<asp:Button ID="Btndiv" runat="server" Text=" except " OnClick="Btndiv_Click" />

The background code is as follows

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebCalculator.ServiceReference1; namespace WebCalculator
public partial class WebTest : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
{ } protected void BtnAdd_Click(object sender, EventArgs e)
CalClient cal = new CalClient();
double result = cal.Add(double.Parse(TextBox1.Text.ToString()), double.Parse(TextBox2.Text.ToString()));
} protected void BtnSub_Click(object sender, EventArgs e)
CalClient cal = new CalClient();
double result = cal.Subtract(double.Parse(TextBox1.Text.ToString()), double.Parse(TextBox2.Text.ToString()));
} protected void BtnMul_Click(object sender, EventArgs e)
CalClient cal = new CalClient();
double result = cal.Multiply(double.Parse(TextBox1.Text.ToString()), double.Parse(TextBox2.Text.ToString()));
} protected void Btndiv_Click(object sender, EventArgs e)
CalClient cal = new CalClient();
double result = cal.Divide(double.Parse(TextBox1.Text.ToString()), double.Parse(TextBox2.Text.ToString()));

Press F5, function

stay textbox Enter any two values in , To test

notes :

You need to reference... In your code

using WebCalculator.ServiceReference1;
WebCalculator For the name of the project ,
ServiceReference1 Is the referenced namespace 

The thing to note in the code is

CalClient You'll find that it's not written by the user at all , It's system generated , You can see from the naming Cal It's the name of the service ,Client It's customer service , So when we write other code , You can easily write the name of this class .

Rookie level WCF More articles about getting started

  1. WCF Introduction learning 2- Console as host

    Step1. Create a WCF Class library and a console program . Step2. quote WCF project . Step3. Add these two references . // The source of this code is No nonsense WCF Getting started series Uri baseAddress = new U ...

  2. I resigned years ago -WCF Introduction learning (4)

    Preface There is a problem in the study of the last article , Use address http://localhost:8080/mex Always prompt when accessing metadata 400 bad request error , Because it's too late , Checked the code several times , And there was no problem ...

  3. I resigned years ago -WCF Introduction learning (5)

    Preface The fifth episode is simple , The video is just 7 minute , But it's very useful . Now I will introduce . I wanted to introduce it in the sixth episode , Later I found out that the sixth episode has more content , More than half an hour , Not together . Website rules 6 Only one article can be published to the home page within hours ,, Then I ...

  4. I resigned years ago -WCF Introduction learning (3)

    Preface [2 了 , Just written , Hand base , Click delete , It's true TMD I've deleted , I have to rewrite ] Today, Saturday , I went to my colleague's house for a dinner , Feel all kinds of luxury . I don't say much nonsense , The first two episodes give a general introduction to what is WCF And the WCF dependent WebService ...

  5. I resigned years ago -WCF Introduction learning (1,2)

    Preface I quit on Monday , Left from 12 Years after graduation 8 From the beginning of this month to the present company . After resigning, of course, it's playing . Playing the game for a few days , It's really day and night , But it's too much fun , Angry delete game . Last time I played, I was 14 year 7 month . Next time, I'll go home for the new year ...

  6. WCF Introduction learning 3- Configuration files and deployment iis

    Profile Settings --------------------------------------------------- There will be a configuration file when it is created , There is actually one WCF Configuration editor , Right click to set . Need to be ...

  7. WCF Introduction learning 1- The simplest communication

    follow msdn I've tried this tutorial wcf, Sincere and easy to use 1. So let's create one wcf Service Library , It's a service class library , The remote lib 2. All by default , Don't modify . Then click publish , There will be one wcf Test client , We can see if it is released successfully . ...

  8. WCF Introductory tutorial ( Four ) adopt Host Code to host services One WCF Use TCP An example of agreement for communication jquery ajax call WCF, use System.ServiceModel.WebHttpBinding System.ServiceModel.WSHttpBinding agreement Study WCF Note 2 No nonsense WCF Introductory course 1 [ What is? WCF]

    WCF Introductory tutorial ( Four ) adopt Host Code to host services Posted on 2014-05-15 13:03  The wind that stays   read (7681) Comment on (0)  edit   Collection WCF Introductory tutorial ( Four ) adopt Host Code way to ...

  9. Study WCF The first example of getting started

    One . summary WCF To put it bluntly, it's an endpoint based means of communication ! Namely Service The end provides a certain function implementation , Then one or more endpoints are exposed ,Client End calls Service End function ( It can be understood as calling a function ), that Clie ...

Random recommendation

  1. Into the pit HttpServletRequest.getParameterMap

    I met a small pit during the project development , After sending the asynchronous request , When calling back, you pass me a parameter, which is directly HttpServletRequest Request , Hereinafter referred to as request: I think it's very simple when I use it , direct get It is good ...

  2. Linux build DNS The server

    Linux system information : Version: Centos 6.6 Ip address: Hostname: Configure the system hostname Vim /etc/sy ...

  3. ACM—— Palindrome

    Palindrome palindrome ! The time limit ( Ordinary /Java):1000MS/3000MS          Run memory limit :65536KByte General submission :967            The test passed :338 describe Palindrome is an interesting phenomenon ...

  4. [Sqlite]--&amp;gt;Java use jdbc contact Sqlite Data operation of various specific process databases

    lead :     1, Sqlite stay Windows.Linux and Mac OS X Installation process on     2. Installation of embedded database . Building database . Build table . Update table structure, data import and export and other specific process records     3, Embedded ...

  5. Hamiltonian cycle ~Lingo Program

    sets: c/1..15/:u; link(c,c):w,x; endsets data: w=@ole(' Question two group one .xls','d'); enddata n=@size(c); min=@su ...

  6. js Set the width of images in rich text

    var txt = layedit.getContent(ieditor);// Get the text in the editor var regex = new RegExp('<img', 'gi');txt = txt.r ...

  7. .NET Core + Abp Step on the pit and fill the pit record (1)

    1. Net Core Of DI and Abp Of DI Coexist Startup in  ConfigureServices Change the return value to IServiceProvider stay ConfigureServices Last call retur ...

  8. Haproxy+Heartbeat High availability cluster solution operation record

    I have described in detail before haproxy The basic knowledge of , Write down Haproxy+Heartbeat High availability web Cluster solution implementation process , To deepen understanding . The architecture sketch is as follows : 1) Basic environment preparation (centos6.9 System ) 1 ...

  9. Laravel firstOrNew And firstOrCreate The difference between

    for example : $item = App\Deployment::firstOrNew( ['name' => ' Question and answer applet '], ['delayed' => 1] ); firstOrNew need ...

  10. js Right click is forbidden prohibit f12 View the source code

    document.oncontextmenu = function () { return false; }; document.onkeydown = function () { if (windo ...