首页 > 应用开发 > IT综合 > WCF Essentials (5)
11. WCF Architecture
WCF offers immensely valuable support for reliability, transactions, concurrency management, security, and instance activation, all of which rely on the WCF interception-based architecture. Having the client interact with a proxy means that WCF is always present between the service and the client, intercepting the call and performing pre- and post-call processing. The interception starts when the proxy serializes the call stack frame to a message and sends the message down a chain of channels. The channel is merely an interceptor, whose purpose is to perform a specific task. Each client-side channel does pre-call processing of the message. The exact structure and composition of the chain depends mostly on the binding. For example, one of the channels may be responsible for encoding the message (binary, text, or MTOM), another for passing security call context, another for propagating the client transaction, another for managing the reliable session, another for encrypting the message body (if so configured), and so on. The last channel on the client side is the transport channel, which sends the message over the configured transport to the host.
On the host side, the message goes through a chain of channels as well, which perform host-side pre-call processing of the message. The first channel on the host side is the transport channel, which receives the message from the transport. Subsequent channels perform various tasks, such as decryption of the message body, decoding of the message, joining the propagated transaction, setting the security principal, managing the session, and activating the service instance. The last channel on the host side passes the message to the dispatcher. The dispatcher converts the message to a stack frame and calls the service instance.

Most noteworthy is that almost all the points in the architecture provide hooks for extensibilityyou can provide custom channels for proprietary interaction, custom behaviors for instance management, custom security behavior, and so on. In fact, the standard facilities that WCF offers are all implemented using the same extensibility model.
Host Architecture
It is also interesting to explore how the transition is made from a technology-neutral, service-oriented interaction to CLR interfaces and classes. The bridging is done via the host. Each .NET host process can have many app domains. Each app domain can have zero or more service host instances. However, each service host instance is dedicated to a particular service type. When you create a host instance, you are in effect registering that service host instance with all the endpoints for that type on the host machine that correspond to its base addresses. Each service host instance has zero or more contexts. The context is the innermost execution scope of the service instance. A context is associated with at most one service instance, meaning it could also be empty, without any service instance. This architecture is shown as below.

12. Working with Channels
You can use channels directly to invoke operations on the service without ever resorting to using a proxy class. The ChannelFactory TheChannelFactory
You need to provide the constructor of ChannelFactory
|
Binding binding = new NetTcpBinding( );
EndpointAddress address = new EndpointAddress("net.tcp://localhost:8000");
IMyContract proxy = ChannelFactory.CreateChannel(binding,address);
using(proxy as IDisposable)
{
proxy1.MyMethod( );
}
13. Reliability
WCF and other service-oriented technologies make a distinction between transport reliability and message reliability. Transport reliability (such as the one offered by TCP) offers point-to-point guaranteed delivery at the network packet level, as well as guarantees the order of the packets. Transport reliability is not resilient to dropping network connections and a variety of other communication problems.
Message reliability, as the name implies, deals with reliability at the message level independent of how many packets are required to deliver the message. Message reliability provides for end-to-end guaranteed delivery and order of messages, regardless of how many intermediaries are involved, and how many network hops are required to deliver the message from the client to the service. Message reliability is based on an industry standard for reliable message-based communication that maintains a session at the transport level. It offers retries in case of transport failures such as dropping a wireless connection; it automatically deals with congestion, message buffering, and flow control; and it can adjust the number of messages accordingly. Message reliability also deals with managing the connection itself via connection verification and cleanup when no longer needed.
Binding and Reliability
In WCF, reliability is controlled and configured in the binding. A particular binding can support or not support reliable messaging, and if supported, it can be enabled or disabled. Which binding supports which reliability value is driven by the target scenario for that particular binding. The following table summarizes the relationship between binding, reliability, and ordered delivery and their respective default values.
Name | Supports reliability | Default reliability | Supports ordered | Default ordered |
|---|---|---|---|---|
BasicHttpBinding | No | N/A | No | N/A |
NetTcpBinding | Yes | Off | Yes | On |
NetPeerTcpBinding | No | N/A | No | N/A |
NetNamedPipeBinding | No | N/A (On) | Yes | N/A (On) |
WSHttpBinding | Yes | Off | Yes | On |
WSFederationHttpBinding | Yes | Off | Yes | On |
WSDualHttpBinding | Yes | On | Yes | On |
NetMsmqBinding | No | N/A | No | N/A |
MsmqIntegrationBinding | No | N/A | No | N/A |
Reliability is not supported by the BasicHttpBinding, NetPeerTcpBinding, and the two MSMQ bindings, NetMsmqBinding and MsmqIntegrationBinding. The reason is that the BasicHttpBinding is oriented toward the legacy ASMX web services world, which does not have reliability. NetPeerTcpBinding is designed for broadcast scenarios. The MSMQ bindings are for disconnected calls, where no transport session is possible anyway.
Configuring Reliability
You can configure reliability (and ordered delivery) both programmatically and administratively. When you enable reliability, you must do so on both the client and the service host sides, otherwise the client will not be able communicate with the service. You can only configure reliability for the bindings that support it. Example below shows the service-side config file that uses a binding configuration section to enable reliability when using the TCP binding.
|
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/13651903/viewspace-1033998/ ,如需转载,请注明出处,否则将追究法律责任。