The main content of this section: 1. Basic Steps for creating a WCF model; 2. Download the code
To give you an intuitive impression on the WCF programming model, you can create a WCF application step by step using a simple program.
1. Basic Steps
1. Create a solution that contains two projects:
Y. wcffirst. HOST: a console application. As a server, you must add the system. servicemodel assembly.
Y. wcffirst. Client: console application. as a client, the system. servicemodel assembly is also required.
2. Y. wcffirst. Host create contract
The servicecontractattribute attribute class defines an interface as a service contract and acts on a method through the operationcontractattribute attribute class so that it can be called by the client. The Code is as follows:
View code
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 using System.ServiceModel;
7 namespace y.WcfFirst.Host.Services
8 {
9 [ServiceContract]
10 public interface IHello
11 {
12 [OperationContract]
13 string Say(string name);
14 }
15 }
3. Y. wcffirst. Host implements the contract. The Code is as follows:
View code
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 using System.ServiceModel;
7 namespace y.WcfFirst.Host.Services
8 {
9 [ServiceBehavior]
10 public class HelloService:IHello
11 {
12 [OperationBehavior]
13 public string Say(string name)
14 {
15 Console.WriteLine("Receive from Client:{0}", name);
16 return string.Format("Hello {0}!", name);
17 }
18 }
19 }
3. Run the following code to host the Y. wcffirst. Host Service:
View code
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 using System.ServiceModel;
7 using y.WcfFirst.Host.Services;
8 namespace y.WcfFirst.Host
9 {
10 class Program
11 {
12 static void Main(string[] args)
13 {
14 Console.Title = "y.WcfFirst.Host";
15 ServiceHost host = new ServiceHost(typeof(HelloService));
16 host.Opened += delegate
17 {
18 Console.WriteLine("\"HelloService\" is Running......\r\nPress any key to exit");
19 };
20 host.Open();
21 Console.ReadLine();
22 }
23 }
24 }
4. Configure the endpoint in Y. wcffirst. Host app. config
Address: service address
Binding: all details of communication, such as encoding, network transmission, and whether the body is encrypted.
Contract: available operations on the client.
The detailed configuration is as follows:
View code
1 <?xml version="1.0" encoding="utf-8" ?>
2 <configuration>
3 <system.serviceModel>
4 <services>
5 <service name="y.WcfFirst.Host.Services.HelloService">
6 <endpoint address="net.tcp://localhost:6666/hello"
7 binding="netTcpBinding"
8 contract="y.WcfFirst.Host.Services.IHello"></endpoint>
9 </service>
10 </services>
11 </system.serviceModel>
12 </configuration>
5. Y. wcffirst. client proxy implementation:
View code
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 using System.ServiceModel;
7 namespace y.WcfFirst.Client.Proxys
8 {
9 [ServiceContract]
10 public interface IHello
11 {
12 [OperationContract]
13 string Say(string name);
14 }
15 }
View code
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 using System.ServiceModel;
7 using System.ServiceModel.Channels;
8 namespace y.WcfFirst.Client.Proxys
9 {
10 public class HelloProxy:ClientBase<IHello>,IHello
11 {
12 public HelloProxy()
13 : base()
14 {
15 }
16
17 public string Say(string name)
18 {
19 return base.Channel.Say(name);
20 }
21 }
22 }
6. The configuration in Y. wcffirst. Client app. config is as follows:
View code
1 <?xml version="1.0" encoding="utf-8" ?>
2 <configuration>
3 <system.serviceModel>
4 <client>
5 <endpoint name="wcfFirst"
6 address="net.tcp://localhost:6666/hello"
7 binding="netTcpBinding"
8 contract="y.WcfFirst.Client.Proxys.IHello"></endpoint>
9 </client>
10 </system.serviceModel>
11 </configuration>
7. Y. wcffirst. cliet:
View code
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 using System.ServiceModel;
7 using y.WcfFirst.Client.Proxys;
8 namespace y.WcfFirst.Client
9 {
10 class Program
11 {
12 static void Main(string[] args)
13 {
14 Console.Title = "y.WcfFirst.Client";
15 Console.WriteLine("Please input your name:");
16 string name = string.Empty;
17 while (true)
18 {
19 name = Console.ReadLine();
20 if (name.Equals("exit"))
21 {
22 Environment.Exit(0);
23 }
24 using (HelloProxy proxy = new HelloProxy())
25 {
26 Console.WriteLine("Recevie from Server:{0}", proxy.Say(name));
27 proxy.Close();
28 }
29 }
30 }
31 }
32 }
8. Run the "show" command. Run the server (host) first, then the client (client), and input "book" and "Glass" on the client. The result is as follows:
Ii. Download source code
You can download the source code from here.