Enterprise Library Policy Injection Application Block II

Source: Internet
Author: User

In the previous article, I made a brief introduction to the PIAB (Policy Injection Application Block) in the Enterprise Library. In this article I would like to talk about my personal understanding of the principles of PIAB design and implementation. In the introduction process, I try to use the way out, while combining examples, Source Code. Hope that through this article to let everyone have a comprehensive and profound understanding of PIAB.

I. MBR, OBJREF, RealProxy, TransparentProxy

Before we actually get into the PIAB, we're going to talk about some of the necessary background information that is relevant. MBR, ObjRef, RealProxy and transparentproxy, for these nouns, I would like to be familiar with or contact with the. NET remoting people are certainly not unfamiliar. Because the implementation mechanism of PIAB relies on this kind of marshaling of remoting, if the reader who does not understand this is difficult to understand later, it is necessary to do the following simple introduction.

We know that the CLR implements isolation between different application through AppDomain, and in general, different AppDomain have different shared memory. An object created in a AppDomain cannot be used directly by a program that is running in another AppDomain. Sharing across AppDomain objects is dependent on an important process: marshaling. We have two different marshaling ways: marshaling by value and marshaling by Reference. The former adopts the serialization/deserialization way, while the latter is implemented by means of passing reference, which is realized as follows:

Remoting infrastructure into an object of ObjRef Instance, ObjRef (SYSTEM.RUNTIME.REMOTING.OBJREF) represents a reference of a remote object that stores all the information needed to make a remote call across AppDomain, such as a URI, a hierarchical structure of a type, And the implementation of the interface and so on. ObjRef can be serialized, that is to say, it can be marshaling by the way of value. So you can say that marshaling by reference relies on the marshaling by Value of ObjRef.

When the ObjRef origin is implemented to another AppDomain, two proxy objects are generated based on the OBJREF data: RealProxy and TransparentProxy. RealProxy is created based on ObjRef, creating TransparentProxy through RealProxy. When making a remote call, the client deals directly with TransparentProxy, and the call to TransparentProxy will forward to realproxy,realproxy through remoting Infrastructure's communicate and activation mechanisms pass invocate to the activated remote Object.

MBR typically implements remote calls in a AppDomain environment, but this mechanism is still valid with a Appdomian and eliminates performance losses across AppDomain. PIAB is the full use of this in the same AppDomain under the MBR.

Two, method interception & Custom RealProxy

In the first part we know that the implementation of PIAB is by applying the policy to the corresponding method, and before actually executing the specific methods of the target object, Piab intercept the call of the whole method. Then call each of the Callhandler that should be included on the method (in the previous chapter, we said policy = Matching rule + call Handler), and then invoke the policy of the true target object. We turn this mechanism into a method injection.

How can you achieve such a method injection? This will require the use of the MBR that we described in the previous section. From the above description, we know that the process of calling an MBR object is this:

Client code==〉transparent proxy==〉real proxy==〉target Object

In the invocate chain above, because the true target object's method is called in the last part, we have the right to call "hijack" in the middle, so that we can call our Callhandler first. And the breakthrough of this inject is realproxy. In the FCL (Framework Class Library) RealProxy (System.Runtime.Remoting.Proxies.RealProxy) is a special abstract class, You can inherit realproxy define your own custom realproxy and write the actions you need to inject into the Invoke method. Piab is using such a solution.

We're not busy first. Introduce the concrete implementation principle of PIAB, because relatively complex. To make it easier for readers to understand the implementation of PIAB, I wrote a simple example. We can generally embody the PIAB implementation mechanism.

This is a simple console application, I first defined a custom RealProxy:

public class Myrealproxy<t>:realproxy
{
Private T _target;
Public Myrealproxy (T target)
: Base (typeof (T))
{
This._target = target;
}
public override IMessage Invoke (IMessage msg)
{
Invoke injected pre-operation.
Console.WriteLine ("The injected pre-operation is invoked");
Invoke the real target instance.
IMethodCallMessage callmessage = (imethodcallmessage) msg;
Object returnvalue = CallMessage.MethodBase.Invoke (This._target, Callmessage.args);
Invoke the injected post-operation.
Console.WriteLine ("The injected post-peration is executed");
Return
return new ReturnMessage (returnvalue, new object[0], 0, NULL, callmessage);
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.