WCF learning journey-Example 3 () and Example 3

Source: Internet
Author: User

WCF learning journey-Example 3 () and Example 3
I. Preface

Based on the previous 20 chapters, we know what it is: WCF; A, B, and C in WCF; the transfer mode of WCF; the boarding mode of WCF; and Exception Handling of WCF. This article combines the above knowledge points and writes a small WCF application-BookMgr ).

This example is a very simple book management system with functions such as query, modification, addition and deletion (excluding security and optimization issues) and exception handling. Addition, deletion, modification, and query of WCF are almost the same as that of WinForm. WCF only writes the specific "Implementation" in "server", while "call" is placed in "client ".

Ii. BookMgr description

1) The "server" of the Demo takes the local console application as the host, and the "client" takes the WinForm project as an example.

2) Demo's "server" extracts data using a layered structure that is more acceptable to beginners, which can be divided into the service layer, entity layer, and data layer.

Shows the reference relationship:

3) the Demo database is SqlServer and the table is Books (the SQL statement is "initialization script. SQL" in the downloaded Package). The table structure is as follows:

Field name

Column name

Data Type

Constraints

Generation Method

Book No.

BookID

Int

Primary Key, required

Auto-Increment

Title

Name

Nvarchar (200)

Required

Manual Input

Category

Category

Nvarchar (50)

Required

Manual Input

Number of publications

Numberofcopies

Int

Required

Manual Input

Author ID

AuthorID

Int

Required

Manual Input

Price

Price

Decimal (18,2)

Required

Manual Input

Publication date

PublishDate

Datetime

Required

Manual Input

Rating

Rating

Nvarchar (5)

Not Required

Manual Input

3. Create a WCF server project structure
  • BookMgr. Contracts:A class library project that defines Service Contract and references the System. ServiceMode assembly. See.

 BookMgr. Services:A class library project that provides the implementation of the WCF Service. Define all the WCF Services in this project to implement the corresponding service agreements defined in Contracts, so Services have references to the Contracts project. See Figure 1, Figure 2.

 

Figure 1

Figure 2

  • BookMgr. Hosting:A Console application is used to host Services defined in the Services Project. This project must reference both the Contracts and Services projects and the System. ServiceMode assembly. See Figure 1, Figure 2.

Figure 1

Figure 2

  • BookMgr. Common: Public classes in the solution. See.

 BookMgr. Model:An object project is used to construct an object for the table structure in the database. It is implemented through EF 6. See.

  • The final project structure is as follows.

4. Create a BookMgr. Contracts project code

1Create an IBookService interface file and a SQLError file in the project. See.

2) create a class SQLError. cs that indicates a Fault exception in BookMgr. Contracts. The Code is as follows.

using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Serialization;using System.Text;using System.Threading.Tasks; namespace BookMgr.Contracts{    [DataContract]    public class SQLError    {        private string _operation;        private string _errorMessage;        public SQLError(string operation, string errorMessage)        {            this._operation = operation;            this._errorMessage = errorMessage;        }        [DataMember]        public string Operation        {            get { return _operation; }            set { _operation = value; }        }         [DataMember]        public string ErrorMessage        {            get { return _errorMessage; }            set { _errorMessage = value; }        }    }}

 

3) IBookService. cs interface file in BookMgr. Contracts. Create Add, Delete, modify, query, Add, Edit, Delete, Get, and Search methods respectively, the five functions correspond to the service application WCF Service Application respectively. I added a feature for returning custom exceptions in the "Edit" method. The Code is as follows.

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace BookMgr.Contracts{    public interface IBookService{  [OperationContract]         string Add(string bookInfo);  [OperationContract]        [FaultContract(typeof(SQLError))]        string Edit(string bookInfo);  [OperationContract]        string Get(string bookId);  [OperationContract]         string Delete(string bookInfo);  [OperationContract]        string Search(string Category, string searchString);     }}

 

Related Article

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.