Basic Conception On WCF

简介: 1. What is WCF? WCF is the latest technology from Microsoft for building services, including webservices.

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.

 

 

目录
相关文章
|
4天前
|
搜索推荐 编译器 Linux
一个可用于企业开发及通用跨平台的Makefile文件
一款适用于企业级开发的通用跨平台Makefile,支持C/C++混合编译、多目标输出(可执行文件、静态/动态库)、Release/Debug版本管理。配置简洁,仅需修改带`MF_CONFIGURE_`前缀的变量,支持脚本化配置与子Makefile管理,具备完善日志、错误提示和跨平台兼容性,附详细文档与示例,便于学习与集成。
293 116
|
19天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
7天前
|
数据采集 人工智能 自然语言处理
Meta SAM3开源:让图像分割,听懂你的话
Meta发布并开源SAM 3,首个支持文本或视觉提示的统一图像视频分割模型,可精准分割“红色条纹伞”等开放词汇概念,覆盖400万独特概念,性能达人类水平75%–80%,推动视觉分割新突破。
451 44
Meta SAM3开源:让图像分割,听懂你的话
|
13天前
|
安全 Java Android开发
深度解析 Android 崩溃捕获原理及从崩溃到归因的闭环实践
崩溃堆栈全是 a.b.c?Native 错误查不到行号?本文详解 Android 崩溃采集全链路原理,教你如何把“天书”变“说明书”。RUM SDK 已支持一键接入。
683 221
|
1天前
|
Windows
dll错误修复 ,可指定下载dll,regsvr32等
dll错误修复 ,可指定下载dll,regsvr32等
133 95
|
11天前
|
人工智能 移动开发 自然语言处理
2025最新HTML静态网页制作工具推荐:10款免费在线生成器小白也能5分钟上手
晓猛团队精选2025年10款真正免费、无需编程的在线HTML建站工具,涵盖AI生成、拖拽编辑、设计稿转代码等多种类型,均支持浏览器直接使用、快速出图与文件导出,特别适合零基础用户快速搭建个人网站、落地页或企业官网。
1677 158
|
存储 人工智能 监控
从代码生成到自主决策:打造一个Coding驱动的“自我编程”Agent
本文介绍了一种基于LLM的“自我编程”Agent系统,通过代码驱动实现复杂逻辑。该Agent以Python为执行引擎,结合Py4j实现Java与Python交互,支持多工具调用、记忆分层与上下文工程,具备感知、认知、表达、自我评估等能力模块,目标是打造可进化的“1.5线”智能助手。
925 61