At its core,WCF is a development platform for service-oriented applications.
As I mentioned earlier,WCF is part of the .NET Framework 3.0,which comprises a set of new assemblies that rely on the .NET Framework 2.0.
System.ServiceModel is the assembly that contains core functionality for WCF,which explains why the WCF platform is often called the service model.
Any project that exposes or consumes WCF services must reference the System.ServiceModel assembly,and possibly other supporting assemblies.

Before you can begin to do interesting things with the service model,it helps to understand the core features that make it possible to create,host,and consume services.
In this section,I will briefly summarize some of the concepts that will be elaborated on in this chapter, to help you on your way.

1.Message Serialization
All enterprise applications at some point must make remote calls across process and machine boundaries.
This is handled by sending messages between applications.
The format of the message is what determines an application’s ability to communicate with other applications.
Remote procedure calls (RPC) and XML messaging formats are two common ways for applications to communicate.
RPC calls are used to communicate with objects (components) across boundaries—for example,calls from a client application to a remote object living in another process.
RPC calls are marshaled by converting them to messages and sending them over a transport protocol such as TCP.
When the message reaches its destination,it is unmarshaled and converted into a stack frame that invokes the object.
This is all usually handled transparently through a client proxy and stub at the destination.
Both proxy and stub know how to construct and deconstruct messages.
This process is known as serialization and deserialization.

Figure 1-14 illustrates the serialization and deserialization process (often just called serialization) from a high level.

Figure 1-14. RPC serialization and deserialization

As I mentioned,the transport carries a message according to the agreed-upon format of both the client and remote application.
As far as the client is concerned,it is usually working with a proxy that looks like the remote object.
When a method is invoked at the client,the proxy invokes underlying plumbing of the technology (for example,Enterprise Services or .NET Remoting) to serialize the outgoing message.
The remote application usually listens for messages on a particular port,
and as they arrive,deserializes those messages (using the same technology) to build a stack frame and invoke the appropriate method on the remote object.
The method return is likewise serialized by the underlying plumbing and returned to the client,at which time the plumbing deserializes the message and constructs a stack frame for the response.
RPC communication like this comes in many flavors,and each flavor is generally not compatible with another—that’s why RPC is not interoperable.

To achieve interoperability,systems rely on a standard format for messages understood by both ends of the communication.
Applications still exchange messages,but they are formatted in XML according to known protocols.
The technology used to support this is traditionally associated with web services such as ASMX,WSE,or WCF.
As Figure 1-15 illustrates,the serialization process is consistent with RPC,the key difference being the underlying plumbing,the format of the message,and the target object,which is usually a web service.

Figure 1-15. Web service serialization and deserialization

The other difference is in the lifetime of the service.
While remote objects are frequently kept alive for the duration of a client session, web services are typically constructed anew for each call.

WCF can be used to achieve both RPC-style messaging and web service messaging.
In both cases,the type that ultimately processes messages is a service type,but its lifetime can be controlled to behave like traditional client-server components or like web services without the notion of a session.
The service model handles all serialization activities based on configuration settings.

2.Services

WCF applications expose functionality through services.
A service is a Common Language Runtime (CLR) type that encapsulates business functionality and exposes a set of methods that can be accessed by remote clients.
In order for a regular CLR type to be considered a service it must implement a service contract.

A service contract is defined by applying the ServiceContractAttribute to a class or interface.
When applied to a class,the class becomes a service type.
When applied to an interface,any class that implements the interface becomes a service type.
In either case,methods exposed by the class or interface must be decorated with the OperationContractAttribute to be considered part of the service contract.
Methods with this attribute are considered service operations.

A service type must be hosted before clients can invoke service operations.

3.Hosting
Service functionality is made available at runtime through a host process—any managed process will do the trick.
Many hosting options are available for WCF services,
including:

  • Self-hosting

This includes console applications,Windows Forms or WPF applications,or Windows services.     //此处把Windows服务划分为自托管

  • Internet Information Services (IIS)

Services can be hosted alongside other ASP.NET applications, for example.

  • Windows Activation Service (WAS)

This is similar to IIS hosting but is only available to IIS 7.0.

Note:Each hosting environment has its benefits and appropriate uses,which will be discussed in Chapter 4.

Although a host process is important,ultimately it is the service model that knows how to process messages targeting a service.
For this a ServiceHost instance is associated with each service type.
ServiceHost is part of the service model and is responsible for initializing communication channels that receive messages to a service.
Basically,to host any service,you construct a ServiceHost,provide it with a service type to activate for incoming messages,
provide it with one or more addresses where the service can be located along with the service contract supported by each address,and provide it with the supported communication protocols.

You can think of the ServiceHost as responsible for managing the lifetime of the communication channels for the service.

4.Endpoints
When the ServiceHost opens a communication channel for a service,it must expose at least one endpoint for the service so that clients can invoke operations.
In fact,endpoints are the key to invoking service functionality.
An endpoint describes where services can be reached,how they can be reached,and what operations can be reached.
Thus, endpoints have three key parts:

  • Address

Refers to the URI where messages can be sent to the service.

  • Binding

Bindings indicate the protocols supported when messages are sent to a particular address.

  • Contract

Each address supports a specific set of operations,as described by a service contract.

The ServiceHost is provided with a list of endpoints before the communication channel is opened. These endpoints each receive messages for their associated operations over the specified protocols.

5.Addresses
Each endpoint is associated with an address,identified by a URI.
An address has a scheme,domain,port,and path in the following format:
scheme://domain[:port]/[path].

The scheme indicates the transport protocol being used,such as TCP,named pipes,HTTP,or MSMQ.
Respectively,the schemes for these protocols are net.tcp,net.pipe,http,and net.msmq.
The domain refers to either a machine name or web domain.
Sometimes localhost is used for communications on the same machine.
The port can be specified to use a specific communication port other than the default for the protocol identified by the scheme.
For example,HTTP defaults to port 80.
Here are some examples of valid base addresses before specifying a path:
net.tcp://localhost:9000
net.pipe://mymachinename
http://localhost:8000
http://www.anydomain.com
net.msmq://localhost

A path is usually provided as part of the address to disambiguate service endpoints. //disambiguate 消除(字句等的)含糊意义
The path does not usually include a filename for self-hosting,but with IIS (as you will see later in this chapter) a physical file is implicitly included in the address.
These are valid self-hosting addresses that include paths:
net.tcp://localhost:9000/ServiceA
net.pipe://mymachinename/ServiceB
http://localhost:8000/Services/ServiceA
http://www.mydomain.com/ServiceA
net.msmq://localhost/QueuedServices/ServiceA

When you add endpoints to a ServiceHost instance,you must specify a unique address for each endpoint.
That means that you must vary at least one of the scheme,domain, port, or path specified.

6.Bindings
A binding describes the protocols supported by a particular endpoint,specifically,the following:
• The transport protocol, which can be TCP, named pipes, HTTP, or MSMQ
• The message encoding format,which determines whether messages are serialized as binary or XML, for example
• Other messaging protocols related to security and reliability protocols,plus any other custom protocols that affect the contents of the serialized message

There are a number of predefined bindings (called standard bindings) provided by the service model.
These standard bindings represent a set of typical protocols representative of common communication scenarios.
Bindings are discussed in detail in Chapter 3.

7.Metadata
Once the ServiceHost is configured for one or more endpoints,and communication channels are open,service operations can be invoked at each endpoint.
This is according to the protocols supported by each endpoint.
Clients invoke service operations at a particular endpoint.
To do so,they need information about the endpoint,including the address,the binding,and the service contract.
Information about service endpoints is part of the metadata for a particular service.
Clients rely on this metadata to generate proxies to invoke the service.

Metadata can be accessed in two ways.
The ServiceHost can expose a metadata exchange endpoint to access metadata at runtime,
or it can be used to generate a WSDL document representing the endpoints and protocols supported by the service.
In either case, clients use tools to generate proxies to invoke the service.

Note:You’ll explore different ways to work with service metadata throughout this chapter, and Chapter 2 discusses metadata in further detail.

8.Proxies
Clients communicate with services using proxies.
A proxy is a type that exposes operations representative of a service contract that hides the serialization details from the client application when invoking service operations.
For WCF applications,proxies are based on the service contract,so if you have access to the service contract definition,you can create a proxy instance to invoke the service.
Before the proxy instance can be used to call service operations,it must be provided with information about one of the endpoints exposed for that service contract—there is a oneto-one relationship between proxy and endpoint.
Tools also exist to generate proxies and endpoint configurations from metadata.
In this chapter,you will learn how to create a proxy manually,without generating metadata,and how to use proxy generation tools.
In either case,the client must open a communication channel with the service to invoke operations.
This channel must be compatible with the channel exposed by the ServiceHost for communications to work.

9.Channels
Channels facilitate communication between clients and services in WCF.
The ServiceHost creates a channel listener for each endpoint,which generates a communication channel.
The proxy creates a channel factory, which generates a communication channel for the client.
Both communication channels must be compatible for messages between them to be processed effectively.
In fact,the communication channel is comprised of a layered channel stack—each channel in the stack is responsible for performing a particular activity while processing a message.
The channel stack includes a transport channel,a message-encoding channel,and any number of message processing channels for security,reliability,and other features.
Without getting into specifics,the binding controls which channels participate in the channel stack,as shown in Figure 1-16. (The details of channels and bindings are explored in Chapter 3.)

Figure 1-16. Messages are processed by equivalent channels at the client and service

10.Behaviors
Behaviors also influence how messages are processed by the service model.
While services and endpoints determine the core communication requirements and metadata shared with clients,
a behavior modifies the way messages are processed as they flow through the channel stack.
Behaviors are local to the client or service—thus,they are not included in metadata.
There are behaviors to control many service model features such as exposing metadata,authentication and authorization,transactions,message throttling,and more.
Behaviors are enabled either in configuration or by applying behavior attributes to client proxies and services.

Note:In this chapter,you’ll learn how to apply the metadata behavior to a service,but other behaviors will be explored throughout this book as they relate to each feature.

Learing WCF Chapter1 Fundamental WCF Concepts的更多相关文章

  1. Learning WCF Chapter1 Hosting a Service in IIS

    How messages reach a service endpoint is a matter of protocols and hosting. IIS can host services ov ...

  2. Learning WCF Chapter1 Generating a Service and Client Proxy

    In the previous lab,you created a service and client from scratch without leveraging the tools avail ...

  3. WCF服务与WCF数据服务的区别

    问: Hi, I am newbie to wcf programming and a little bit confused between WCF Service and WCF Data  Se ...

  4. [WCF编程]1.WCF入门示例

    一.WCF是什么? Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,整合了原有的windows通讯的 .net Remotin ...

  5. 大比速:remoting、WCF(http)、WCF(tcp)、WCF(RESTful)、asp.net core(RESTful)

    近来在考虑一个服务选型,dotnet提供了众多的远程服务形式.在只考虑dotnet到dotnet的情形下,我们可以选择remoting.WCF(http).WCF(tcp).WCF(RESTful). ...

  6. 大比速:remoting、WCF(http)、WCF(tcp)、WCF(RESTful)、asp.net core(RESTful) .net core 控制台程序使用依赖注入(Autofac)

    大比速:remoting.WCF(http).WCF(tcp).WCF(RESTful).asp.net core(RESTful) 近来在考虑一个服务选型,dotnet提供了众多的远程服务形式.在只 ...

  7. Prism for WPF 搭建一个简单的模块化开发框架(四)异步调用WCF服务、WCF消息头添加安全验证Token

    原文:Prism for WPF 搭建一个简单的模块化开发框架(四)异步调用WCF服务.WCF消息头添加安全验证Token 为什么选择wcf?   因为好像wcf和wpf就是哥俩,,, 为什么选择异步 ...

  8. WCF入门三[WCF宿主]

    一.概述 WCF程序必须在宿主上运行,也就是WCF服务必须寄宿在某一个windows的进程中,可以是IIS.控制台程序.窗体程序.WAS以及所有.net程序等程序进程中.在我用VS2013创建WCF服 ...

  9. WCF入门一[WCF概述]

    一.什么是WCF WCF是使用托管代码建立和运行面向服务(Service Oriented)应用程序的统一框架.它使得开发者能够建立一个跨平台的.安全.可信赖.事务性的解决方案,且能与已有系统兼容协作 ...

随机推荐

  1. string应用

    今天在网上搜了一些资料. C# string类应用 判断是否包含子串 想要判断一个字符串中是否包含某个子串,可以用Contains方法来实现: ? public bool Contains (stri ...

  2. Git 常用配置和使用

    Git:是一个分布式的源代码管理工具,Linux内核的代码就是用Git管理的所以它很强,也很快, 和 Vss/SVN比起来 本地Git初始化配置及其使用: 1. 初始化本地Git库:打开Git Bas ...

  3. 怎么用js代码改变单选框的选中状态

    今天突然有一个需求要用到,使用js代码改变单选框的选中状态.当时想也不想直接 function doGender(gender) { if (gender == "男") { ge ...

  4. hdoj1874 (优先队列+Dijkstra)

    hdoj1874 分析: 一看题目, 就是求最短路, 这道题用的是Dijkstra+优先队列.先说一下Dijkstra算法:每次扩展一个距离最短的节点, 更新与其相邻点的距离. 当所有边权都为正时, ...

  5. Android开发之Adapter

    学习android时,对于我这种初学者来说,刚开始接触控件,发现有的控件需要adapter有些不需要,对此我感到不解.所以决定一探究竟. 其实android是一个完全遵从MVC模式的框架,activi ...

  6. 获取汉字拼音 Java

    两种方法:一个是使用btye数组,一个是引入jar包进行操作. 1. public class CharacterParser { private static int[] pyvalue = new ...

  7. 小技巧之指定refer

    在当前页面A的控制台输入window.location.href='要跳去的页面B',B页面的refer即为A页面.

  8. Ext.Net学习笔记03:Ext.Net MessageBus用法

    发布和订阅消息 Ext.Net MessageBus 的本质是一个消息订阅机制,ExtJS中没有这种机制,所以MessageBus的Ext.Net实现的,但并不是原创,这种功能在dojo中早就实现了, ...

  9. O-C相关-09-id 类型与应用

    09-id 类型与应用 1, 使用 NSObject 访问子类对象方法 代码在编辑的时候, Xcode 会实时检查语法情况. 如果调用某个对象的方法, 在声明中没有该方法的声明, 那么就会报错. 但是 ...

  10. 安装aptana插件报Error opening the editor. java.lang.NullPointerException

    Aptana的官方网站下载eclipse的插件:  http://update.aptana.com/update/studio/3.2/ ,可以在线安装也可以下载插件后再安装,我是以在线的形式安装的 ...