WCF Learning (1): preview)

Source: Internet
Author: User
WCF Learning (1): preview

Windows commnication Foundation. WCF is Microsoft's implementation of a series of industrial standards and a good way to implement services. The traditional three-layer structure mode is generally data access layer, business logic layer, and customer layer. However, in this mode, the customer layer is closely coupled with the business logic layer, service-oriented means that the customer layer is loosely coupled with the business logic.
Address
Each service of WCF must have one address and only one address. WCF supports multiple types of transmission:
1. HTTP
2. TCP
3. Peer Network
4. Internal Process Communication
5. MSMQ
The common address includes two aspects: 1. Service Location and 2. transmission protocol. For example, if the TCP-based address is net. TCP: // localhost: 8800/henllyee. Service, net. TCP is the transmission protocol, and the following is the service location.

Contract
All WCF services are made public as a contract. When you use this service, it follows a certain contract. The representation of the contract is like the representation in WebService, which is also identified by the attribute tag. WCF defines four types of contracts:
1. Service Contract: defines the service operations that can be performed by the client.
2. Data contract: the data type of service interaction.
3. Fault contract: defines the thrown error.
4. Message contract: defines a message that interacts directly with the service.
A service contract is widely used. It is very easy to define a service contract, but it can be identified on some service interfaces.

Namespace Henllyeewcf. Service
{
[Servicecontract (namespace = " Http://henllyee.cnblogs.com " )]
Public   Interface Ihello
{
[Operationcontract]
StringSayhello (StringStrname );
}
Public   Class Hello: ihello
{
Public   String Sayhello ( String Strname)
{
ReturnString. Format ("{0} say hello at {1}", Strname, datetime. Now. tostring ());
}
}
}

First, we need to identify the interface or standard class as "servicecontract". There are two optional parameters: "namespace" and "name", that is, the name of the namespace and the class or standard interface. Even if servicecontract is applied, it is neither a type member nor a contract member. operationcontract must be displayed to expose the operation. To facilitate overloading, operationcontract provides optional parameters: name to specify another name for the exposure operation.
Note: the service contract can only be defined on interfaces or classes, and operationcontract can only be applied to methods. We recommend that you define the service contract on the interface.

Managed
Each WCF Service must be hosted in the process. Each hosting process can host multiple services, and each service can be hosted by multiple processes. There are several types of hosting:
1. IIS hosting. IIS hosting can start a request on the client without the need to start it all the time, but it can only use the HTTP protocol.
2. Self-managed. Must be passed manuallyProgramTo control the hosting startup and shutdown.
3. Was. Only use Vista, which is the same as that hosted by IIS, but not limited to HTTP.
Write a self-managed console program as follows:

Namespace Henllyeewcf. Host
{
Class Program
{
Static   Void Main ( String [] ARGs)
{
Using (Servicehost host =   New Servicehost ( Typeof (Henllyeewcf. Service. Hello )))
{< br> host. addserviceendpoint ( typeof (henllyeewcf. service. ihello), New nettcpbinding (), " net. TCP: // localhost: 8000/Hello " );
host. open ();
console. read ();
}
}
}
}

Generally, the interaction between a WCF client and a server uses an endpoint. Each endpoint consists of address, binding, and contract. The server side interacts with each other by exposing the endpoint. In the program, we first define a servicehost and specify the service as our defined henllyeewcf. service. hello, at the same time add a final node with the host, then open the host, waiting for the user to cancel the process.

Client
First, after starting the host, you can call the service by adding a service application to the client. You can also implement it in your own programming method.

Namespace Henllyeewcf. Client
{
[Servicecontract (namespace =   " Http://henllyee.cnblogs.com " )]
Public   Interface Ihello
{
[Operationcontract]
StringSayhello (StringStrname );
}

Class Program
{
Static   Void Main ( String [] ARGs)
{
Ihello proxy = Channelfactory < Ihello > . Createchannel ( New Nettcpbinding (),
New Endpointaddress ( " Net. TCP: // localhost: 8000/Hello " ));

Console. writeline (proxy. sayhello ( " Henllyee Cui " ));
Console. Read ();
}
}
}

First, I defined completely the same interface for the service, and then implemented the proxy by using channel channelfactory, specifying the address bound to the endpoint.
Architecture of the entire solution

Then we first run the host, and the following occurs on the running client:

Program: http://files.cnblogs.com/henllyee/henllyeewcf.rar

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.