A serious problem caused by a callcontextinitializer-based WCF extension

Source: Internet
Author: User

WCF is a distributed communication framework with extremely high scalability, whether in the channel layer (Channel Layer) or service model layer, we can customize the related component to inject into the WCF running environment through the corresponding extension. In the many scalable points of WCF, Icallcontextinitializer can help us complete some additional functionality before and after the service operation, which is actually a way of implementing AOP. For example, in the implementation of localization through WCF extension, I made sure that the service operation had the same language culture as the client through Icallcontextinitializer; in the through WCF Extension implementation of context information delivery, I through the icallcontextinitializer implementation contexts in the client to the server automatic delivery. The definition of Icallcontextinitializer is as follows:

1: public interface ICallContextInitializer
2: {
3:   // Methods
4:   void AfterInvoke(object correlationState);
5:   object BeforeInvoke(InstanceContext instanceContext, IClientChannel channel, Message message);
6: }

Yesterday, Li Yongjing students asked me a related question. The problem is probably this, he uses Icallcontextinitializer to implement WCF and NHibernate integration. Specifically, through the Icallcontextinitializer implementation of the transaction, that is, through the Beforeinvoke method initialization NHibernate session, through the Afterinvoke to commit the transaction. However, there is a very serious problem with this: when executing Afterinvoke commit a transaction, it is possible to throw an exception. Once the exception is thrown from the Afterinvoke, the entire service side crashes. Let's talk about this now and the root cause of the problem.

I. Recurring problems

To reproduce the problem, I've written a very simple example where you can download this example. First I define the following custom callcontextinitializer:mycallcontextinitializer that implements the Icallcontextinitializer interface. In the Afterinvoke method, I throw an exception directly.

1: public class MyCallContextInitializer : ICallContextInitializer
2: {
3:   public void AfterInvoke(object correlationState)
4:   {
5:     throw new Exception("调用MyCallContextInitializer.AfterInvoke()出错!");
6:   }
7:
8:   public object BeforeInvoke(InstanceContext instanceContext, IClientChannel channel, Message message)
9:   {
10:     return null;
11:   }
12: }

Then, we apply the Mycallcontextinitializer defined above by ServiceBehavior way. To this end, we define the following service behavior that implements the IServiceBehavior interface: Myservicebehaviorattribute. In the ApplyDispatchBehavior method, add our custom Mycallcontextinitializer object to the Callcontextinitializer list of distribution runtime operations for all endpoints.

1:public class Myservicebehaviorattribute:attribute, IServiceBehavior
2: {
3:public void addbinding Parameters (ServiceDescription servicedescription, ServiceHostBase servicehostbase, Collection<serviceendpoint > Endpoints, BindingParameterCollection bindingparameters) {}
4:
5:public void ApplyDispatchBehavior (Servic Edescription servicedescription, ServiceHostBase servicehostbase)
6: {
7:foreach (ChannelDispatcher DISPATC Her in servicehostbase.channeldispatchers)
8: {
9:foreach (EndpointDispatcher endpoint in dispatcher. Endpoints)
: {
11:foreach (DispatchOperation operation in endpoint. Dispatchruntime.operations)
A: {
13:operation. Callcontextinitializers.add (New Mycallcontextinitializer ());
14:}
15:}
16:}
17:}
:
19:public void Validate (ServiceDescription servicedescription, ServiceHostBase servicehostbase) {}
:

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.