WCF Technical Profiling 30: A very useful WCF invoke programming technique [next article]

Source: Internet
Author: User
Tags aop exception handling static class

In the previous article, I resolved the exception handling in the service invocation process and the shutdown of the service proxy by using delegate method. Readers of WCF Technical Profiling (Volume 1) should know that in chapter 7th I solved similar problems through an AOP approach, and now we're going to discuss the solution.

What will happen if the service agent cannot be closed in time? , we know the importance of closing the service agent in time, and give the correct programming way. If you strictly follow the above programming approach, it means that for each service invocation, you will use the same code for exception handling and shutdown or interruption of service proxy objects. In my personal opinion, it is a bad design to have every corner of an application flooded with the same code fragment. The purpose of the design is to implement code reuse (reuse), not code duplication (Duplicate).

So now our goal is to maintain the reusable code separately and reuse it where it is used. The idea is this: hijacking a method call to a client for service access through an object, implementing a real method call within the object, a service proxy shutdown or interruption, and exception handling. This is actually an AOP based solution, where you can implement AOP for service invocation by customizing the real Agent (RealProxy), which is one of the important reasons why you will spend so much time at the beginning of this chapter on real agents and transparent proxies.

The sequence diagram shown in the following illustration (Sequence Diagram) reveals the principle of implementation: Service invocation, exception handling, and channel shutdown or interruption are implemented in the defined RealProxy (servicerealproxy). The client code makes the service call entirely through the custom real agent Servicerealproxy transparent proxy, so all method calls are distributed directly to the Servicerealproxy object. Servicerealproxy constructs a Channelfactory<t> object based on the context of the current method invocation (such as parameters, MethodBase, etc.) and creates a true service proxy object. Then servicerealproxy a real service invocation with the created service proxy, and if the service call completes gracefully, call the Close method to turn off the service proxy. If both Communicationexception and TimeoutException are thrown during the call, the Abort method is invoked to forcibly interrupt the service proxy. Finally, the result of the service invocation or the thrown exception is returned to the client code via transparentproxy .

This example is simply a way of thinking about WCF service invocation through AOP, not a complete solution (for example, without considering security authentication and client credentials settings; Not taking into account two-way communications and callbacks), and interested readers can further improve on this inheritance. Now, let's do a step-by-step demo.

Step one: Create the channalfactory<t> static factory: Channelfactorycreator

Because the service invocation is done through the Service Broker, and Channelfactory<t> is the creator of the service proxy, here you define a channelfactorycreator static factory class that creates or acquires ChannelFactory <T> method. Because the creation of channelfactory<t> is a time-consuming task, in order to provide better performance, the same as clientbase<t> with the channelfactory<t> caching mechanism ( Caching mechanism for channelfactory<t> in clientbase<t>). However, the caching mechanism here is much simpler than the clientbase<t> implementation,clientbase<t> caching by the endpoint configuration name, endpoint address, and callback object. This is simply a cache of channelfactory<t> through the endpoint configuration name, as we assume that the client is using the configured endpoint exclusively for service invocation (this is also the way we recommend it). The following is the definition of the static factory class for the entire channelfactory<t>:

1:using System;
2:using System.Collections;
3:using System.ServiceModel;
4:namespace artech.serviceproxyfactory
5: {
6:internal Static Class Channelfactorycreator
7: {
8:private static Hashtable channelfactories = new Hashtable ();
9:
10:public static channelfactory<t> create<t> (String endpointname)
11: {
12:if (String. IsNullOrEmpty (Endpointname))
13: {
14:throw new ArgumentNullException ("Endpointname");
15:}
16:
17:channelfactory<t> ChannelFactory = null;
18:
19:if (Channelfactories.containskey (endpointname))
20: {
21:channelfactory = Channelfactories[endpointname] as channelfactory<t>;
22:}
23:
24:if (ChannelFactory = = null)
25: {
26:channelfactory = new channelfactory<t> (endpointname);
27:lock (Channelfactories.syncroot)
28: {
29:channelfactories[endpointname] = ChannelFactory;
30:}
31:}
32:
33:return ChannelFactory;
34:}
35:}
36:}

In Channelfactorycreator, all created channelfactory<t> collections are saved by a Hashtable type of static variable, and the Hashtable key is a string representing the endpoint configuration name. In the Create<t> method, first view the cache for a created Channelfactory<t> object by the incoming endpoint configuration name, or return directly if it exists, or create a new channelfactory<t > object and adds it to the cache before returning it.

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.