Step by step to learn about the WCF series-create and call a HelloWorld instance for the WCF Service, and use the encoding method (4)

Source: Internet
Author: User

  Reading directory

  I. Preface

Ii. Create a WCF Service

Iii. Hosting the WCF Service

Iv. Call of the WCF Service

V. Summary

  I. Preface

Figure 1 shows three projects, which correspond to different parts in Figure 2. The Client project corresponds to the Client in Figure 2, and the HelloWorldService project corresponds to the Service in Figure 2, that is to say, the specific service is provided by HelloWorldService. The Host project corresponds to the ServiceHost in figure 2 and provides a running environment for the HelloWorldService.

  

Figure 1

  

Figure 2

  Ii. Create a WCF Service

Let's take a look at the code in the specific project. The first project: HelloWorldService, that is, the project created by the service. To be able to use WCF, you need to add System in the reference. serviceModel and System. identityModel

This interface actually defines what we need to accomplish for this service. In this interface, it needs to complete the GetHelloWorld operation and have a string type return value, we can see that there are some additional attributes ServiceContract and OperationContract on the interface. These attributes are some special tags in WCF. These tags are called contracts, this is actually a very notable feature of WCF. In WCF, communication between the client and the server must follow a certain contract, and the client and the server must ensure that these contracts are consistent.

1: IHelloWorldService. cs

Using System;
Using System. ServiceModel;

Namespace HelloWorldService
{

/* Interface with contract added */
[ServiceContract (Namespace = "http://www.monkeyfu.net/")]
Public interface IHelloWorldService
{
[OperationContract]
String GetHelloWorld ();
}

}

2: HelloWorldService. cs

Namespace HelloWorldService
{
/* To implement a specific class of this interface, and implement the interface with the contract added */
Class HelloWorldService: IHelloWorldService
{
Public string GetHelloWorld ()
{
Return string. Format ("the message was received at {0 }:{ 1}", DateTime. Now, "The world is so beautiful, but I am so violent, not good, not good! ");
}
}
}

  Iii. Hosting the WCF Service

The second project: Host. In order to provide services to customers, this Service must be executed and put into the Host when it is executed, that is, it must be placed in the service host, that is, ServiceHost for execution.

1: Create a host

In the Main function, we create a ServiceHost host = new ServiceHost (), that is, to create a host, and the service instance corresponding to this host is HelloWorldService. helloWorldService is the implementation class in the first project. ServiceHost host = new ServiceHost (typeof (HelloWorldService. helloWorldService) This code means that we have started a new host, which now runs a service, which is composed of HelloWorldService. helloWorldService class for implementation

2: Add a new Endpoint

Then, after creating a host, we need to add a new Endpoint. Before that, we said that an Endpoint is also called an Endpoint, its main function is to provide an interface for External Service Release to implement communication between the client and the server. When we add a new Endpoint, We need to specify some parameters, the first parameter is the interface to which the Endpoint is bound. Here it is bound to the IHelloWorldService interface. That is to say, all messages passing through this Endpoint follow this interface, this means that all the messages that have been added through this Endpoint must meet the corresponding contract requirements in the IHelloWorldService interface, the second parameter also specifies the Protocol to which the message is bound when the Endpoint transmits the message. Here, the new NetTcpBinding () is used to transmit the message using the TCP protocol, after binding the specified protocol, the third parameter is to specify the specific address of the communication during message transmission. net is used here. tcp: // localhost: 9000/HelloWorldService

3: Open the host

Then host. open () is to Open the host. When the host is opened, it can provide services. The client can also connect to the host to use the HelloWorldService provided by the host.

1: Program. cs

Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. ServiceModel;
Using HelloWorldService;

Namespace Host
{
Class Program
{
Static void Main (string [] args)
{

// Step 1 starts a new host, which now runs a service
Using (ServiceHost host = new ServiceHost (typeof (HelloWorldService. HelloWorldService )))
{
// Step 2 is used to provide an interface for external Service publishing to implement communication between the client and the server.
Host. AddServiceEndpoint (typeof (IHelloWorldService), new NetTcpBinding (), "net. tcp: // localhost: 9000/HelloWorldService ");
// Step 3 open the host
Host. Open ();
Console. ReadLine ();
}
}
}
}

  Iv. Call of the WCF Service

Project 3: Client

1: Create a Proxy

As mentioned above, the Client requires a Proxy to interact with the Service. Therefore, we need to create a Proxy first. We use the ChannelFactory channel factory class to create a new channel, channelFactory is a generic type. In this generic type, it indicates which contract the Proxy follows. Here we use the IHelloWorldService contract, the description of this contract is defined on the class Program. You can see that this description is the same as the IHelloWorldService description we define. If the client wants to use this service, you must use a contract that matches the service for access. This is a requirement that must be followed. CreateChannel (new NetTcpBinding (), new EndpointAddress ("net. tcp: // localhost: 9000/HelloWorldService ") in the CreateChannel method, specify communication-related parameters. The first parameter is the communication method bound to Tcp, the second parameter is to create a new EndpointAddress and its address is the address pointing to ServiceHost. You will see that after we have created the Proxy, in fact, we set up an equivalent connection between the Client and the Host. This connection means that all of us use the Tcp protocol to access the address net provided by the Host. tcp: // localhost: 9000/HelloWorldService and use the same contract

2: Access Services through proxy

After the Proxy is created, we can call the GetHelloWorld method in the Proxy. The GetHelloWorld method is provided by the IHelloWorldService interface. When you execute the GetHelloWorld method, WCF accesses net. tcp: // localhost: 9000/HelloWorldService. After receiving the message, the Host Endpoint will find the corresponding business logic in the HelloWorldService class according to the contract IHelloWorldService, the actual code in the execution logic does not need to be concerned about the underlying layer of the client. We only need to call the GetHelloWorld method, which is no different from our local call, in fact, a distributed call is implemented through a proxy class similar to a local call.

1: Program. cs

Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. ServiceModel;
Namespace Client
{

// Step 1 create a copy of the service contract
[ServiceContract (Namespace = "http://www.monkeyfu.net/")]
Public interface IHelloWorldService
{
[OperationContract]
String GetHelloWorld ();
}

Class Program
{
Static void Main (string [] args)
{
// Step 2: Create a Proxy
IHelloWorldService proxy = ChannelFactory <IHelloWorldService>. CreateChannel (new NetTcpBinding (), new EndpointAddress ("net. tcp: // localhost: 9000/HelloWorldService "));
// Step 3 access the service through proxy
String Result = proxy. GetHelloWorld ();
Console. WriteLine (Result );
Console. ReadLine ();
}
}
}

Running timeliness result

The Client accesses the GetHelloWorld method in the HelloWorldService in the Host through the Tcp protocol, so that we can complete a simple distributed call based on the Tcp protocol.

1: first start the Host

2: Start the Client.

  

  V. Summary

Basic Requirements for clients and servers

.Server

1: define and implement a service contract

That is to say, at this end of the Service, it not only needs to complete the business logic of the specific service, but also the corresponding contract of the service.

2: Create a ServiceHost instance for the service type and expose the Endpoint

To create a ServiceHost instance, we can understand that we have created a server and put this service on the server for processing. At the same time, we need to expose the Endpoint, allows the client to access the corresponding Service in ServiceHost.

3: Open the communication channel

Allows the client to access these services on the server.

.Client

1: a copy of the service contract and Endpoints endpoint information are required.

A copy of a service contract can be understood as an interface containing the contract in the service. Although the client does not need the specific service logic of the service, however, the client needs an interface containing a contract to tell the client how to communicate with the server what rules should be followed, the Endpoint information mainly includes the specific address of the server service and the protocol used for specific communication.

2: Build a communication channel for a specific Endpoints and perform the call operation to complete our specific service

Figure 3 below better illustrates the role of the client and server's specific Endpoint.

We will describe it with ABC. To send a message or receive it, there must be three types of ABC, which are indispensable.

-A indicates the Address, where is the host, as mentioned above "net. tcp: // localhost: 9000/HelloIndigo ". This is what we call the address. It tells the client where to access the services in the host.

-B indicates Binding. Binding indicates how to communicate, as mentioned above, we use the Tcp protocol for communication.

-C indicates the Contract. Contract tells us what to upload, what to do, and what operations to do, the name and parameters of this task are described in the Contract.

  

Figure 3

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.