Learing WCF Chapter1 Fundamental WCF Concepts
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的更多相关文章
- 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 ...
- 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 ...
- WCF服务与WCF数据服务的区别
问: Hi, I am newbie to wcf programming and a little bit confused between WCF Service and WCF Data Se ...
- [WCF编程]1.WCF入门示例
一.WCF是什么? Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,整合了原有的windows通讯的 .net Remotin ...
- 大比速:remoting、WCF(http)、WCF(tcp)、WCF(RESTful)、asp.net core(RESTful)
近来在考虑一个服务选型,dotnet提供了众多的远程服务形式.在只考虑dotnet到dotnet的情形下,我们可以选择remoting.WCF(http).WCF(tcp).WCF(RESTful). ...
- 大比速: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提供了众多的远程服务形式.在只 ...
- Prism for WPF 搭建一个简单的模块化开发框架(四)异步调用WCF服务、WCF消息头添加安全验证Token
原文:Prism for WPF 搭建一个简单的模块化开发框架(四)异步调用WCF服务.WCF消息头添加安全验证Token 为什么选择wcf? 因为好像wcf和wpf就是哥俩,,, 为什么选择异步 ...
- WCF入门三[WCF宿主]
一.概述 WCF程序必须在宿主上运行,也就是WCF服务必须寄宿在某一个windows的进程中,可以是IIS.控制台程序.窗体程序.WAS以及所有.net程序等程序进程中.在我用VS2013创建WCF服 ...
- WCF入门一[WCF概述]
一.什么是WCF WCF是使用托管代码建立和运行面向服务(Service Oriented)应用程序的统一框架.它使得开发者能够建立一个跨平台的.安全.可信赖.事务性的解决方案,且能与已有系统兼容协作 ...
随机推荐
- 首页的sitecontent地址
当无法出现工具栏时,利用url地址,可以方便直接进入sitecontent http://sp2013/sites/bentest/_layouts/15/viewlsts.aspx
- MVC Filter自定义验证(拦截)
namespace QS.Web.Extensions { /// <summary> /// 验证session.权限 状态 /// </summary> [Attribut ...
- 一个fibonacci数列简单求和的问题
前段时间老师在讲函数调用的时候,用Fibonacci数列来演示了一下,因为以前没怎么接触过Fibonacci,所以当时很懵. 当时让求的是Fibonacci数列中,第N位值为多少,当时老师写的是: 之 ...
- 关于使用navigationController,前后2个视图控制器navigationBar隐藏属性不同,导致右滑手势失效问题的解决办法
###1.问题描述:如A是navigationController的rootViewController,在这个页面navigationBar是显示的(隐藏属性为NO),它push圧栈过来B视图控制器 ...
- 关于添加非系统framework后,import导入头文件时没有提示的解决办法
##1.选择target(就是左边你的工程target)—— BuildSettings —— search Paths 下的 User Header Search Paths(如图所示: ##2.双 ...
- uitableview的重用重叠问题
以前也遇到过.但都不知道怎么就解决了. 今天费了一番功夫找到了最佳解决方案. 对于一些复杂的cell 从来都是用自定义的方法,但是如果复杂的cell里面内容多了.特别是图片加载,那难免会出现重叠重用 ...
- c编程:提示用户输入一个0—9的数字进行猜测电脑产生的随机数。一共有三次机会。
// // main.c // 使用c语言进行编程: 题目:由电脑生成一个由0-9之间的随机数,提示用户也输入一个数字进行猜测.当猜测三次仍不中的时候结束程序. 编译环境:Xcode6.3 特别介 ...
- Java实战之01Struts2-03属性封装、类型转换、数据验证
九.封装请求正文到对象中 1.静态参数封装 在struts.xml配置文件中,给动作类注入值.调用的是setter方法. 原因:是由一个staticParams的拦截器完成注入的. 2.动态参数封装: ...
- shell脚本操作mysql数据库—创建数据库,在该数据库中创建表(插入,查询,更新,删除操作也可以做)
#!/bin/bash HOSTNAME="192.168.1.224" #数据库Server信 ...
- SQL Server 扩展事件(Extented Events)从入门到进阶(1)——从SQL Trace到Extented Events
由于工作需要,决定深入研究SQL Server的扩展事件(Extended Events/xEvents),经过资料搜索,发现国外大牛的系列文章,作为“学习”阶段,我先翻译这系列文章,后续在工作中的心 ...