My WCF journey (2): Endpoint Overview

Source: Internet
Author: User

In fact, WCF builds a framework that enables communication between applications in an interconnected system. So that Developers and effecect do not need to consider how to implement communication problems when building a distributed system, but pay more attention to the business logic of the system. In WCF Infrastructure, the communication between applications is implemented by the Endpoint.

Endpoint Structure

An Endpoint contains the following four objects:

Address: Address uniquely identifies an Endpoint through a URI and tells the potential WCF service caller how to locate this Endpoint. So Address solves Where to locate the WCF Service?
Binding: Binding implements all the underlying details of communication between the Client and the Service. For example, how is the Message transmitted between the Client and the Service encoded -- text/XML, binary, MTOM; what kind of Transport -- TCP, Http, Named Pipe, MSMQ is used for Message transmission; and the mechanism used to solve the Secure Messaging problem-SSL and Message Level Security. So what Binding solves is How to communicate with service?
Contract: the main function of Contract is to expose all valid Functionality provided by a WCF Service. In terms of Message Exchange, Contract converts each Operation to the corresponding Message Exchange Pattern -- MEP (Request/Response; One-way; Duplex ). So Contract solves What functionalities do the Service provide?
Behavior: Behavior is mainly used to customize the necessary Behavior of an Endpoint during runtime. For example, the Timeout of the Service callback Client, the Credential type used by the Client, and whether Transaction is supported.
When we Host a WCF Service, we must define one or more endpoints for it, and then the service listens to requests from the Client through this defined Endpoint. When our Application needs to call this Service, because the Client and Service communicate through the Endpoint, we must define the Client Endpoint for our Application. Only when the Client Endpoint and a Service end Endpoint match each other (the Service end can define multiple endpoints for a Service) Can the Client-side requests be monitored by the Service end. That is to say, we can call this Service only when the Client has an Endpoint that exactly matches the Service end. This type of matching is strict. For example, in terms of Address matching, the Endpoint Address of the Client and Service must not only completely match the Service on the URI, but also their Headers must match each other. For Binding, generally, the Client needs a Binding identical to the Service end to communicate with each other.

Sample

First, give a Sample to give us a perceptual knowledge of how to define an Endpoint in the WCF Service Aplication. For details about the entire Solution structure, refer to my previous Blog ([original] My WCF journey (1): create a simple WCF program. You can also download the corresponding Source Code (http://www.cnblogs.com/files/artech/Artech.WCFService.zip) through the Link below)


1. Service Contract: Artech... WCfService. Contract/ServiceContract/IGeneralCalculator. cs

 

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

Namespace Artech. WCFService. Contract
{
[ServiceContract]
Public interface IGeneralCalculator
{
[OperationContract]
Double Add (double x, double y );
}
}

2. Service: Artech. WCFSerice. Service/GeneralCalculatorService. cs

Using System;
Using System. Collections. Generic;
Using System. Text;

Using Artech. WCFService. Contract;

Namespace Artech. WCFService. Service
{
Public class GeneralCalculatorService: IGeneralCalculator
{
IGeneralCalculator Members # region IGeneralCalculator Members

Public double Add (double x, double y)
{
Return x + y;
}

# Endregion
}
}


3. Hosting: Artech. WCFService. Hosting/Program. cs


Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. ServiceModel;
Using Artech. WCFService. Contract;
Using Artech. WCFService. Service;
Using System. ServiceModel. Description;

Namespace Artech. WCFService. Hosting
{
Class Program
{
Static void Main (string [] args)
{
// HostCalculatorServiceViaCode ();
HostCalculatorSerivceViaConfiguration ();
}

/** // <Summary>
/// Hosting a service using managed code without any other aiton information.
/// Please note that the related configuration data shocould be removed before calling the method.
/// </Summary>
Static void HostCalculatorServiceViaCode ()
{
Uri httpBaseAddress = new Uri ("http: // localhost: 8888/generalCalculator ");
Uri tcpBaseAddress = new Uri ("net. tcp: // localhost: 9999/generalCalculator ");

Using (ServiceHost calculatorSerivceHost = new ServiceHost (typeof (GeneralCalculatorService), httpBaseAddress, tcpBaseAddress ))
{
BasicHttpBinding httpBinding = new BasicHttpBinding ();
NetTcpBinding tcpBinding = new NetTcpBinding ();

CalculatorSerivceHost. AddServiceEndpoint (typeof (IGeneralCalculator), httpBinding, string. Empty );
CalculatorSerivceHost. AddServiceEndpoint (typeof (IGeneralCalculator), tcpBinding, string. Empty );

ServiceMetadataBehavior behavior = calculatorSerivceHost. Description. Behaviors. Find <ServiceMetadataBehavior> ();
{
If (behavior = null)
{
Behavior = new ServiceMetadataBehavior ();
Behavior. HttpGetEnabled = true;
CalculatorSerivceHost. Description. Behaviors. Add (behavior );
}
& N

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.