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 ();
}
}
}
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