That day, a child taught me how to use WCF [1] [1/3].

Source: Internet
Author: User

Since it is a child series, of course, there must be a little foundation to quickly master, summarize, and summarize the series. Haha

Preface:

I will not elaborate on the first article. I will step by step teach you how to create a simple SOA case and have a basic understanding of WCF.

 

 

1.1 Introduction to SOA

SOA (Service-Oriented Architecture) is both a programming model and an architectural method for software development.

Based on this architecture methodProgramIt is composed of "functions with certain behaviors (services. <If you have written WebService, you will understand it better. If you used to write in the form of three layers, then the original data access layer can only be called by programs in your program, if you use this architecture, for example, to query the price of the corresponding book based on an ISBN, it is a method. If you use this architecture, this method (behavior) the service is bound to a URL address. Then, you can add a special parameter (such as ISBN) based on the URL to return a JSON or XML file, then you can call it. For example, if your andriod tablet or ipad can get the data you want through URL, the cross-platform effect can be achieved, which is similar to WebService, however, WCF is not that simple>

 

 

1.2 Basic start

 

Preface:

We are preparing to complete a simple news publishing system.

There are more functions for internal editing and publishing of news, but I only provide the viewing function for external users to add new news, but the news type is provided by friends.

Probably because our news system is better, we provide some interfaces for others to make their own clients, and we can also publish news, but ourSource codeIt is impossible for others, so we can use WCF to do it. For example, if we want to be an iPhone app, others can download our app and view our published news, it is impossible for us to access the database on Apple's mobile phone. We can update data through the service, and use WCF to implement cross-platform. Now let's get started.

 

contract is the meaning of a contract. It is the most common and must be recited.

1. Open Visual Studio 2010 and create a blank solution.

name newssystem and click OK

right-click solution and add four Solution Folders.

clients, hosts, services, interfaces

2. Right-click interfaces and add two class libraries.

newsinterface

newsexternalinterface

Add two most important assembly references in WCF

perform the same operation on the newsexternalinterface class library.

3. Delete the two default class1.cs files. Add the inewsinterface and inewsexternalinterface to the two class libraries respectively.

4. Add the following namespace reference to both interfaces

  using  system. runtime. serialization; 
  using  system. servicemodel; 

 

5. Add the public keyword before the interface

For example:

 

 

6. entity classes, we will not write them separately. Write them directly in the corresponding interface. Here we only demonstrate the usage of WCF, database operations, and direct use of LINQ to SQL, most enterprises use EF code first. I will discuss this later. To avoid conflicts with the database, add a DTO

This section describes the use of operation contracts, data contracts, and enumeration in advance.

First, we operate the newsinterface class library.

 

 

6.1 create an enumeration, newstypeenum, to demonstrate the basic expression of enumeration

 
UsingSystem;
 
UsingSystem. Collections. Generic;
 
UsingSystem. LINQ;
 
UsingSystem. text;
 
UsingSystem. runtime. serialization;
 
 
 
NamespaceNewsinterface
 
{
 
[Datacontract]
 
Public EnumNewstypeenum
 
{
 
[Enummember]
 
Sports,
[Enummember]
 
It,
 
[Enummember]
 
Country,
 
[Enummember]
 
Funny
 
}
 
}

Newstypeenum is a public Enumeration type. Note that the datacontract attribute must be added before enum. Each constant of the enumeration type must be represented by the enummember attribute.

 

 

6.2 Add a newsdto object class to demonstrate the basic object Writing Method

 
UsingSystem;
 
UsingSystem. Collections. Generic;
 
UsingSystem. LINQ;
 
UsingSystem. text;
 
UsingSystem. runtime. serialization;
UsingSystem. servicemodel;
 
 
 
NamespaceNewsinterface
 
{
 
[Datacontract]
 
Public ClassNewsdto
 
{
 
/// <Summary>
 
/// News title
 
/// </Summary>
 
[Datamember]
 
Public StringNewstitle {Get; set ;}
 
 
 
/// <Summary>
/// News content
 
/// </Summary>
 
[Datamember]
 
Public StringContent {Get; set ;}
 
 
 
/// <Summary>
 
/// News type, which can be enumerated here by the way
 
/// </Summary>
 
[Datamember]
 
PublicNewstypeenum newstype {Get; set ;}
 
 
 
/// <Summary>
 
/// Release time
/// </Summary>
 
[Datamember]
 
PublicDatetime publishtime {Get; set ;}
 
 
 
/// <Summary>
 
/// Last Update Time
 
/// </Summary>
 
[Datamember]
 
PublicDatetime lastupdatetime {Get; set ;}
 
 
 
/// <Summary>
 
/// Author
 
/// </Summary>
[Datamember]
 
Public StringAuthor {Get; set ;}
 
 
 
/// <Summary>
 
/// Last modified
 
/// </Summary>
 
[Datamember]
 
Public StringLastauthor {Get; set ;}
 
 
 
/// <Summary>
 
/// Read volume
 
/// </Summary>
 
[Datamember]
Public IntReadcount {Get; set ;}
 
}
 
}

 

 

 

6.3 open inewsinterface and add a public method to demonstrate common interfaces. Here, the professional name is data contract in WCF.

We will only demonstrate the basic simple and simplified query, as well as five ways to display news associated images. I will declare again that I will not talk about the news logic of news publishing. Here we will only demonstrate the usage of WCF.

 

 
UsingSystem;
 
UsingSystem. Collections. Generic;
 
UsingSystem. LINQ;
 
UsingSystem. text;
 
UsingSystem. runtime. serialization;
 
UsingSystem. servicemodel;
 
 
NamespaceNewsinterface
 
{
 
[Servicecontract]
 
Public InterfaceInewsinterface
 
{
 
/// <Summary>
 
/// Add a News Record
 
/// </Summary>
 
/// <Param name = "DTO"> news entity </param>
 
/// <Returns> the id after successful addition is returned </returns>
 
[Operationcontract]
 
IntNewsadd (newsdto DTO );
 
 
/// <Summary>
 
/// Delete a news item
 
/// </Summary>
 
/// <Param name = "DTO"> news entity </param>
 
/// <Returns> whether the deletion is successful </returns>
 
[Operationcontract]
 
BoolNewsdelete (newsdto DTO );
 
 
 
/// <Summary>
 
/// Update a News Record
 
/// </Summary>
 
/// <Param name = "DTO"> news entity </param>
/// <Returns> whether the update is successful </returns>
 
[Operationcontract]
 
BoolNewsupdate (newsdto DTO );
 
 
 
/// <Summary>
 
/// Obtain all news
 
/// </Summary>
 
/// <Returns> return to the news list </returns>
 
[Operationcontract]
 
List <newsdto> newslist ();
 
 
 
/// <Summary>
 
/// Obtain the news image by ID
/// </Summary>
 
/// <Param name = "ID"> News No. </param>
 
/// <Returns> picture associated with news </returns>
 
[Operationcontract]
 
Byte[] Getnewsimage (StringID );
 
 
 
}
 
}

 

 

7. Create a service

Right-click the services solution folder and add a class library named newsservices.

Change the class1.cs file name

 

 

7.1 first compile the solution and then add reference

 

 

7.2 Implementation Interface

 
UsingSystem;
 
UsingSystem. Collections. Generic;
 
UsingSystem. LINQ;
 
UsingSystem. text;
 
UsingSystem. runtime. serialization;
 
UsingSystem. servicemodel;
 
UsingNewsinterface;
 
 
 
NamespaceNewsservices
 
{
 
Public ClassNewsimpl: inewsinterface
 
{
 
 
Public IntNewsadd (newsdto DTO)
 
{
 
Throw NewNotimplementedexception ();
 
}
 
 
 
Public BoolNewsdelete (newsdto DTO)
 
{
 
Throw NewNotimplementedexception ();
 
}
 
 
 
Public BoolNewsupdate (newsdto DTO)
 
{
Throw NewNotimplementedexception ();
 
}
 
 
 
PublicList <newsdto> newslist ()
 
{
 
Throw NewNotimplementedexception ();
 
}
 
 
 
Public Byte[] Getnewsimage (StringID)
 
{
 
Throw NewNotimplementedexception ();
 
}
 
}
 
}

 

 

8. Add the host hosts folder. We will demonstrate it in the console. The host can be in many forms.

Modify attributes with. Net framework4 at the same time

Then add reference

Open program. CS and enter basic information.Code

UsingSystem;
 
UsingSystem. Collections. Generic;
 
UsingSystem. LINQ;
 
UsingSystem. text;
 
UsingSystem. runtime. serialization;
 
UsingSystem. servicemodel;
 
UsingNewsservices;
 
 
 
NamespaceNewshosts
 
{
 
ClassProgram
 
{
 
Static VoidMain (String[] ARGs)
{
 
Console. writeline ("The service host is opening ...");
 
Try
 
{
 
// Todo waits for the service code to be enabled
 
 
 
}
 
Catch(Exception ex)
 
{
 
Console. writeline (ex. Message );
 
}
 
Console. writeline ("Opened successfully! ");
 
Console. readkey ();
 
}
 
}
 
}

Add a program class using the servicehost object

try... Add the following code to the catch code block:

Add the host service configuration. You can also manually write the configuration using the WCF tool.

right-click the default generated app. config and choose

WCF Service configuration editor in the menu bar of vs2010

before use, make sure that your solution is regenerated

let me record a screen and talk about the configuration.

remember the endpoint address here. Through this address, other clients can obtain the data we want. We can directly send the data to this address, JSON, XML, or.. Net object, and you can continue to process it. If JSON or XML is used, the Web can use $. ajax and other jquery methods are used to obtain data, which is displayed on the webpage

set the startup Item and run

start the service successfully!

download code: Click here to download

 

 

Tomorrow, September 11, June 28, 2013

 

9. Create a database

10. Create a service

11. The client uses the service to delete, modify, and query images.

 

 

 

 

 

It's really late. I'm going to bed. Write at 1.1

 

Good night, everybody!

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.