Basic Conception On WCF

1. What is WCF?

WCF is the latest technology from Microsoft for building services, including web
services.

WCF is the acronym for Windows Communication Foundation. It is Microsoft's
latest technology that enables applications in a distributed environment to
communicate with each other.

WCF is an umbrella technology that
covers ASMX web services, .NET remoting, WSE, Enterprise Service, and System.
Messaging. It is designed to offer a manageable approach to distributed computing,
broad interoperability, and direct support for service orientation.

2. WCF architecture

Contracts(契约):

The Contracts layer defines various aspects of the message system. For
example, the Data Contract describes every parameter that makes up
every message that a service can create or consume.

Service runtime:

The Service runtime layer contains the behaviors that occur only during the
actual operation of the service, that is, the runtime behaviors of the service.

Messaging:

The Messaging layer is composed of channels. A channel is a component that
processes a message in some way, for example, in authenticating a message.

WCF ABCs 

address, binding,contract

Address

The WCF Address is a specific location for a service. It is the specific place to which a
message will be sent. All WCF services are deployed at a specific address, listening at
that address for incoming requests.

eg:

http://www.myweb.com/myWCFServices/SampleService

 

Binding

Bindings are used to specify the transport, encoding, and protocol details required
for clients and services to communicate with each other.

system-provided bindings:

BasicHttpBinding, WSHttpBinding, WSDualHttpBinding,
WSFederationHttpBinding, NetTcpBinding, NetNamedPipeBinding,
NetMsmqBinding, NetPeerTcpBinding, and MsmqIntegrationBinding

Contract

A WCF contract is a set of specifications that define the interfaces of a WCF service.
A WCF service communicates with other applications according to its contracts.
There are several types of WCF contracts such as Service Contract, Operation
Contract, Data Contract, Message Contract, and Fault Contract.

 

Service contract

A service contract is the interface of the WCF service. Basically, it tells others what
the service can do. It may include service-level settings such as the name of the
service, the namespace of the service, and the corresponding callback contracts of the
service. Inside the interface, it can define a bunch of methods, or service operations,
for specific tasks. Normally, a WCF service has at least one service contract.

 

Operation contract

An operation contract is defined within a service contract. It defines the
parameters and return type of an operation. An operation can take data of a
primitive (native) data type such as an integer as a parameter, or it can take a
message, which should be defined as a message contract type.

eg:

[FaultContract(typeof(ProductFault))]
GetProductResponse GetProduct(GetProductRequest request);

 

Message contract

If an operation contract needs to pass a message as a parameter or return a message,
the type of these messages will be defined as message contracts. A message contract
defines the elements of the message as well as any message-related settings such as
the level of message security, and also whether an element should go to the header
or to the body.

 

namespace MyWCF.EasyNorthwind.MessageContracts
{
/// <summary>
/// Service Contract Class - GetProductResponse
/// </summary>
[WCF::MessageContract(IsWrapped = false)]
public partial class GetProductResponse
{
  private MyWCF.EasyNorthwind.DataContracts.Product product;
  [WCF::MessageBodyMember(Name = "Product")]
  public MyWCF.EasyNorthwind.DataContracts.Product Product
  {
    get { return product; }
    set { product = value; }
  }
}
}

 

Data contract

Data contracts are data types of the WCF service. All data types used by the
WCF service must be described in metadata to enable other applications to
interoperate with the service. A data contract can be used by an operation contract
as a parameter or return type, or it can be used by a message contract to define
elements. If a WCF service uses only primitive (native) data types, it is not necessary
to define any data contract.

 

namespace MyWCF.EasyNorthwind.DataContracts
{
/// <summary>
/// Data Contract Class - Product
/// </summary>
[WcfSerialization::DataContract(Namespace = "http://MyCompany.com/
ProductService/EasyWCF/2008/05", Name = "Product")]
public partial class Product
{
private int productID;
private string productName;
[WcfSerialization::DataMember(Name = "ProductID",
IsRequired = false, Order = 0)]
public int ProductID
{
get { return productID; }
set { productID = value; }
}
[WcfSerialization::DataMember(Name =
"ProductName", IsRequired = false, Order = 1)]
public string ProductName
{
get { return productName; }
set { productName = value; }
}
}
}

Endpoint

Messages are sent between endpoints. Endpoints are places where messages are
sent or received (or both), and they define all of the information required for the
message exchange. A service exposes one or more application endpoints (as well
as zero or more infrastructure endpoints). A service can expose this information
as the metadata that clients process to generate the appropriate WCF clients and
communication stacks. When needed, the client generates an endpoint that is
compatible with one of the service's endpoints.
A WCF service endpoint has an address, a binding, and a service contract
(WCF ABC).

Behavior

A WCF behavior is a type or settings to extend the functionality of the original
type. There are many types of behaviors in WCF such as service behavior, binding
behavior, contract behavior, security behavior, and channel behavior.

Hosting

A WCF service is a component that can be called by other applications. It must be
hosted in an environment in order to be discovered and used by others.

Self hosting

A WCF service can be self-hosted, which means that the service runs as a standalone
application and controls its own lifetime. This is the most flexible and easiest way of
hosting a WCF service, but its availability and features are limited.

Windows services hosting

A WCF service can also be hosted as a Windows service. A Windows service is
a process managed by the operating system and it is automatically started when
Windows is started (if it is configured to do so). However, it lacks some critical
features (such as versioning) for WCF services.

IIS hosting

A better way to host a WCF service is to use IIS. This is the traditional way of
hosting a web service. IIS, by its nature, has many useful features such as process
recycling, idle shutdown, process health monitoring, message-based activation, high
availability, easy manageability, versioning, and deployment scenarios. All of these
features are required for enterprise-level WCF services.

Windows Activation Services hosting

The IIS hosting method, however, comes with several limitations in the
service-orientation world, the dependency on HTTP being the main culprit. With
IIS hosting, many of WCF's flexible options can't be utilized. This is the reason why
Microsoft specifically developed a new method called Windows Process Activation
Services (WAS) to host WCF services.
WAS is the new process activation mechanism for Windows Server 2008 that is also
available on Windows Vista and Windows 7. It retains the familiar IIS 6.0 process
model application pools and message-based process activation and hosting features
(such as rapid failure protection, health monitoring, and recycling), but it removes
the dependency on HTTP from the activation architecture. IIS 7.0 uses WAS to
accomplish message-based activation over HTTP. Additional WCF components also
plug into WAS to provide message-based activation over the other protocols that
WCF supports, such as TCP, MSMQ, and named pipes. This allows applications that
use the non-HTTP communication protocols to use the IIS features such as process
recycling, rapid fail protection, and the common configuration systems that were
only previously available to HTTP-based applications.
This hosting option requires WAS to be properly configured, but it does not require
you to write any hosting code as part of the application. (Microsoft MSN, Hosting
Services, retrieved on 3/6/2008 from http://msdn2.microsoft.com/enus/
library/ms730158.aspx.)

Channels

As we have seen in the previous sections, a WCF service has to be hosted in an
application on the server side. On the client side, the client applications have to
specify the bindings to connect to the WCF services. The binding elements are
interfaces, and they have to be implemented in concrete classes.

The concrete implementation of a binding element is called a channel.

The binding represents the configuration and the channel is the implementation associated with that
configuration. Therefore, there is a channel associated with each binding element.
Channels stack on top of one another to create the concrete implementation of the
binding—the channel stack.

Metadata

The metadata of a service describes the characteristics of the service that an external
entity needs to understand in order to communicate with the service.

技术改变世界
原文地址:https://www.cnblogs.com/davidgu/p/2383148.html