C # create a message Interceptor (intercept) 2

Source: Internet
Author: User

C # create a message Interceptor (intercept) 2

Now, in the previous article, we will implement an InterceptProperty class.

First, this class inherits two interfaces: IContextProperty and IContributeObjectSink.

IContextProperty: this interface indicates that this class is a context property. It has two methods: IsNewContextOK and Freeze.

1. Freeze () is used to locate the last position of the created Context. Generally, no logic is required.

2. The IsNewContextOK () method allows us to check whether we are satisfied with the current new Context. True is returned if you are satisfied. If you are not satisfied with false, an exception is returned. We can proceed with this operation.

IContributeObjectSink: this interface is a message pool. Only by inheriting this interface can we receive Object messages.

Of course, there are also IContributeEnvoySink, IContributeClientContextSink, and IContributeServerContextSink. These message pools can receive messages of different forms. Here I will not explain them more.

1. The GetObjectSink () method in IContributeObjectSink needs to be implemented. The main function is to get the object of a message pool.


Well, if you don't talk much about it, paste the Code directly:

//IContributeObjectSink,IContributeEnvoySink,IContributeClientContextSink,IContributeServerContextSink    public class InterceptProperty:IContextProperty,IContributeObjectSink    {        public InterceptProperty()        {            Console.WriteLine(" Call 'InterceptProperty' - 'Constructor'  ");        }        public string Name {             get            {                return "Intercept";            }        }        public void Freeze(Context newContext)        {        }        public bool IsNewContextOK(Context newCtx)        {            var result = newCtx.GetProperty("Intercept");            return result!=null;            //return false;        }        public IMessageSink GetObjectSink(MarshalByRefObject obj, IMessageSink nextSink)        {            InterceptSink interceptSink = new InterceptSink(nextSink);            return interceptSink;        }    }
In the IsNewContextOK () function, I mainly obtain the Intercept attribute in the new context. Normally, the system constructs an InterceptProperty object, GetProperty () the function is to get whether the Name attribute matches. If yes, return true; otherwise, an exception occurs.
The GetObjectSink method is used to get an InterceptSink object. In the next section, I will implement the InterceptSink class.

Related Article

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.