This post is part of a series about WCF extensibility points. For a list of all previous posts and planned future ones, go to the index page.
And we’re now back in the middle of the WCF runtime, and at the last few posts for this series. This time we’ll look at the instance context provider. The instance context represents the service which is running, but often (as most inner components in WCF) you don’t need to know anything about them, until you do. Unfortunately the MSDN documentation for the instance context isn’t very clear (it lists what it has, but doesn’t show many examples on where it can be used), but I’ve found that the post on instance context from Dan Rigsby to be a good introduction on the topic, where he lists some usages and properties of that class, so I won’t repeat that here.
The instance context provider, represented by the aptly named interface IInstanceContextProvider, is a way for a service to change how those contexts can be created. Similar to the IInstanceProvider (which defines how instances of the service class are created), it allows for tweaking the context creation, sharing of contexts between multiple calls, among other things, but the canonical usage of the instance context provider is to allow the sharing of service sessions between different clients.
As with most runtime extensibility points, there are no public implementations in WCF. There are three internal ones, however, which are chosen depending on the value of the InstanceContextMode property of a ServiceBehaviorAttribute which can be applied to the service class – you can use a tool such as ILSpy or Reflector to get more information on the implementation of those classes.
Note that when a custom IInstanceContextProvider is set in the dispatch runtime, the instance context mode set in the service behavior attribute is ignored.
The first method in the instance context provider to be called is GetExistingInstanceContext, whenever a new message arrives to the service. For that message the provider can either return an existing instance context which will be reused, or return null (or Nothing) – in that case, a new instance context is created by the WCF runtime and passed to the InitializeInstanceContext method, so that it can be properly initialized. Notice that each new instance context created by the runtime counts towards the ServiceThrottle.MaxConcurrentInstances quota.
When the WCF runtime is done with the instance context, it will call IsIdle to check whether the instance context can be disposed. If the method return true, then the context will be closed and recycled, and the number of concurrent instances (bound by ServiceThrottle.MaxConcurrentInstances) will be decremented. If the method returns false, then NotifyIdle will be called. In this case, the runtime will pass a callback that the instance context provider must call whenever the instance context is done and ready to be recycled. When the callback is invoked, the runtime will again invoke IsIdle to check whether it can dispose and recycle the instance context.
Instance context providers apply only at the server side, and they’re bound to the DispatchRuntime object. They’re typically added in endpoint or service behaviors, as shown in the example below.
The canonical usage of the instance context provider (and the one in the WCF samples) is for two different client to be able to share a session object on the server. I decided to improve on that example, to, beside enable sharing session, also enable it to happen in bindings which don’t support sessions (such as BasicHttpBinding). In this example I’m using a custom SOAP header to identify the session, but another scenario where this could definitely be useful would be where a cookie was used for identification.
Anyway, let’s revive once again the calculator. But this time, to make things more interesting for sessions, let’s make it a calculator which works with postfix operators, using the Reverse Polish notation (RPN). So instead of having the usual operations taking two parameters, our contract is shown below. The operation Enter adds a parameter to a stack, and the operations take the top two parameters from the stack, process them, and store the result back in the stack.
And example helps to understand this concept. If we want to calculate ((4*5) / (3+2)), we’d call the following operations:
Now, this new calculator has a concept of a stack. If we use a normal session-less binding (such as BasicHttpBinding or WebHttpBinding) to implement this contract, the operation calls would fail, because every call goes to a new instance (thus the stack would be empty). We cannot use the InstanceContextMode.PerSession property of the ServiceBehaviorAttribute, because for session-less bindings this is essentially the same as InstanceContextMode.Single. We could also have a static stack, but in this case the operations by two different clients would clobber each other. What we really need is to simulate a session over those bindings which don’t support them (notice that another way to do that is to create a protocol channel that changes the shape of the underlying channel from session-less to session-full; but as I mentioned before, if you can avoid writing channels, you should do always go for the alternative).
The approach I’m using in this sample is to keep a session alive for some time after the last message for that session arrived. This way, the client can send a series of messages which share the same header, and if they arrive close enough to each other, they will get the same instance context in the server (and consequently the same service instance). Each time a new message for that session is processed, the “expiration” of the session is extended. A background thread keeps checking the sessions to see which ones are expired, and marks those which are for removal.
Before we move on, the usual disclaimer: this is a sample for illustrating the topic of this post, this is not production-ready code. I tested it for a few contracts and it worked, but I cannot guarantee that it will work for all scenarios – please let me know if you find a bug or something missing. There are some security implications in keeping multiple instance contexts alive for a certain time period which are not addressed (if many different clients connect to the server, it would quickly exceed its MaxConcurrentInstances quota). Also, as usual, error checking has been kept to a minimum.
First, let’s look at the service code – it’s not as trivial as the “normal” calculator, but it’s quite simple nonetheless. Since the service class doesn’t need to deal with sessions itself (that’s why we’re using a custom instance context provider), it just implements the stack calculator as it normally would.
The test program will just use the channel factory and channel as it would in a session-full scenario, but this time it will use a session-less binding, BasicHttpBinding – but we’ll add an endpoint behavior which will do the trick with the instance contexts. The client code is also similar, but we’ll use an operation context scope to add a header to all requests from the client, so that all the requests can go to the same server session.
The endpoint behavior will add our custom instance context provider to the runtime. And like the “official” sample, it will also add a message inspector which will be used to notify our session that the call has completed.
Now, for the instance context provider class. The class stores a dictionary of instance contexts keyed by the identifier send by the client. When a new message arrives, we first try to fetch the header to find if the client sent any session identifier. If it did, we check if the session is active. If it is, we’ll retrieve a SharedInstanceContextInfo from the instance context extension list (see more about extensions in the previous post) and increment the number of instances which are using it. The extension object is a perfect place for this scenario, since it will live alongside the instance context object. If we didn’t find a session for the client (either because it didn’t send any identifier, or because there’s no instance context for the given identifier), then the method returns null, which signals WCF that it’s time to create a new instance context for that request.
If the previous method (GetExistingInstanceContext) returns null, then WCF will create a new instance context, and pass it to the instance context provider so that it can be initialized. In this method, we first try to fetch the instance id send by the client; if there is none, we just create a new random one, then create a SharedInstanceContextInfo which will live alongside with the instance context as one of its extensions – this is similar to the code from the official sample. But to use the timed expiration, we’ll increment the “busy count” of the context info twice – one for the request which caused the new context to be created, and one so that the context will always be “alive”, until the expiration thread finally marks it down. We then add the new instance context to the provider’s dictionary, and start the expiration thread, if it hasn’t yet. Finally, the code will add a handler to the Closing event of the context: when WCF decides that it’s time to recycle it, we’ll be notified and we can remove it from the provider’s dictionary.
The expiration thread method is a simple loop which every second looks at the instance contexts in the provider’s dictionary. If the context info associated with it is expired, then the context is marked to be removed, which is done by decrementing the “busy count” one last time so that it reaches zero.
The last two methods in the IInstanceContextProvider interface simply delegate the processing to the context info associated with the extension.
You recall that the instance context provider also implements IDispatchMessageInspector – and we use its methods to decrement the “busy count” for the instance context information (which was incremented on either GetExistingInstanceContext or InitializeInstanceContext).
That’s the instance context provider. A lot of the processing, though, happens on the extension class, SharedInstanceContextInfo, which is shown below. It stores the instance context associated with it, its expiration time and the number of active calls (plus the expiration one) in the “busy count”.
The busy / idle operations are interesting in the extension. Whenever a new message arrives, the instance context provider will increment the busy count in the extension. When the message processing finishes (on the dispatch message inspector), or with the expiration thread finds the instance context to be expired, they will decrement the busy count. Once it reaches zero, if any callback had been set by the WCF runtime, it will be called, which will cause WCF to call IsIdle again and, unless new messages arrived for the same instance identifier, the instance context will be marked for recycling.
And that’s it. On the code gallery I’ve augmented the test program to perform more operations: have multiple instance contexts at the same time, verify that the sessions actually expire after a certain time, and other examples.
Initializer interfaces in the WCF runtime
[Code in this post]
[Back to the index]