WCF Basic Tutorial--vs2013 creating a WCF Application

Source: Internet
Author: User

Introduction recently, in the project, I saw the knowledge of WCF, which brother Siang often talked about, and did not understand what WCF was when he was knocking on the code.
and why we would use such a framework in the project, just write code in the same format according to the code that Brother Siang them, and as the code knocks
more and more slowly understand how to implement the client and the service side together, but the real operating mechanism of WCF still do not understand, recently in the listening Division
Dude, when I was lecturing, I had some knowledge about WCF, and I felt that I should have a good understanding of this knowledge, and that this thing was in the project .
often common, the following is a combination of my implementation of a small demo to share with you.
some of the most basic concepts, we can refer to the Baidu Encyclopedia on the introduction of WCF, here to give you two pictures contrast, can be very good embodiment of
purpose of WCF:
ASP-based application and service-oriented development                     In ASP. NET-based application development, we access the application server from the client's browser and then through the data in the application server
Library connection to connect to the database server, read or manipulate the data, sometimes more than one file server. You can observe that basically all the
applications are placed on a single server, but for one, it is difficult for a server to support all of the servers due to business needs (such as interacting with external systems)
application. Let's look at a picture like this                     The client uses a browser to access server A, and server A is deployed in Servers B, C, D ... for business needs with a variety of other applications. And then through the WCF technology to each other
communication, mutual access ... However, the benefits of service-oriented are not only here, but it also provides interoperability for different operating systems in different languages.
Let's start by creating a client and server-side decoupled WCF application:
1. Create a service side of our WCF--Select a WCF service application when creating a new project
     
                          
2, we can see that in our established solution has a default of two service.svc and Iservice.cs files, we can be
these twofiles, and then add our own WCF services, step


                     
3, in the solution we can see, when we add a user.svc, VS will automatically generate a WCF excuse IUser.cs, which is what we need
The IUser.cs defines the WCF method ShowName, which is implemented in User.svc.cs the method of the interface. The code is as follows  
Using system;using system.collections.generic;using system.linq;using system.runtime.serialization;using System.servicemodel;using system.text;namespace demoservicewcf{    //Note: Using the rename command on the Refactor menu, you can change the interface name in both the code and the configuration file " Iuser ".    [ServiceContract] public    interface Iuser    {        [OperationContract]        string showname (string name);}    }

Using system;using system.collections.generic;using system.linq;using system.runtime.serialization;using System.servicemodel;using system.text;namespace demoservicewcf{    //NOTE: You can change the class name in code, SVC, and configuration files at the same time using the Rename command on the Refactor menu "User".    //Note: In order to start the WCF test client to test this service, select User.  svc or user. Svc.cs in Solution Explorer  and start debugging. Public    class User:iuser    {Public        string ShowName (string name)        {            string wcfname = string. Format ("WCF service, display name: {0}", name);            return wcfname;}}    }


you see in the WCF interface with the usual we are different is it two more tags, the rest and our normal learning interface is the same, add this
twoa tag is a reference that relies on system.servicemodel.
[ServiceContract], to indicate that the interface is a WCF interface, if not added, it will not be called externally.

[OperationContract], to illustrate that the method is a WCF interface method, without adding the words ibid.
In fact, these two tags are simply a sign of distinguishing between common interfaces and common methods, like the tenth and 11th phases of our accelerated, and 12.
, for example, now 10 has a classmate called Zhang San, 11 also has a classmate called Zhang San, so we need to find someone to have this different
to differentiate between the period numbers.
4, the following look at the effect of the run, set User.svc as the start page after the run.
       
so our WCF server is basically ready to use, and all that is left is to publish the server and deploy it to IIS so that it can be used by clients
, and I'm not going to give you an explanation. How to publish WCF on IIS, we use a simple way to directly preview the USER.SVC,      In fact, the application scenario of WCF can be described as simple as this: we designed the scene is often used in production scenarios, the WCF program to send
stay on top of IIS. Suppose the scenario is as follows: a server and B server. We have "deployed" the WCF program we just created on Server B (this tutorial
A , b server is placed on my own machine), our goal is to access the server-a WCF program in the A-server application to implement
application communication.
Here's a look at the creation of our client
1, first I created a Web application on the client;                  
2, below we need to add a service reference, fill in the address and click the Go button, and then click OK
            
Once the reference is complete, we will find one more file in the solution:
                      3, the code in the page is as follows 
<%@ page language= "C #" autoeventwireup= "true" codebehind= "User.aspx.cs" inherits= "Democlientwcf.user"%><! DOCTYPE html>

Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.UI;using system.web.ui.webcontrols;using democlientwcf.servicereference;namespace democlientwcf{public    partial class User:System.Web.UI.Page    {        protected void Page_Load (object sender, EventArgs e)        {        }        protected void Btnclick (object sender, EventArgs e)        {            userclient user = new Userclient ();            string result = user. ShowName (this.txtName.Text);            Response.Write (Result);}}}    

4, the following to explain some of the more important code
1, using Democlientwcf.servicereference; This reference democlientwcf the namespace of our client,
ServiceReference is the name we need to remember when we add a service reference.
2 Userclient user = new Userclient (); Userclient is the client agent of the server user class that we generated when we added the reference.
class, the proxy class name for the generic client will be ***client. Where user is the name of the WCF Service (USER.SVC) that we added on our server.   Here we have completed a very simple WCF demo, and completed the application by a server small B server in the WCF-provided method of the call.
Summary
This little demo is one of the most basic basics of learning about WCF, which seems to me to be easy to accept for beginners and to be completed
A basic tutorial, with more in-depth study of WCF, I will continue to update this kind of blog to communicate with you, if this blog has what does not understand the ground
party can message exchange.
  

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

WCF Basic Tutorial--vs2013 creating a WCF Application

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.