Thursday, 20 February 2014

Customizing WCF with Behaviors

How to Configure WCF Behaviors

In this demonstration, you will see how to apply the instancing attributes to your behaviors for your Windows Communication Foundation service. For this demonstration we have an existing application that consists of a service and a client.

The service is hosted on Internet Information Server as a web service.

We have two contracts, or two interfaces in our service, one is a calculator, which has the four standard mathematical methods, add, subtract, multiply, and divide.

We also have an ICalculator instance which implements the ICalculator and this will be responsible for providing us with our ContextMode InstanceId and operationCount values.

Notice that each one of these two contracts of the interfaces in our service have the SessionMode equals SessionMode.Required.

We specify this so that when a client accesses it, we indicate the necessary type of session mode that we wish in our service, and the client will then see that behavior from our service as it is executed.

So very quickly we will take a look at our CalculatorService class which implements our ICalculator instance interface, and we see that we have a static instanceCount variable, and we will see how that plays into our client application as we execute it and we will see those values increment accordingly.

The CalculatorService in the constructor will simply implement our instanceCount and sets the InstanceId equal to the instanceCount and then we have our four methods add, subtract, multiply, and divide, and then for our ICalculator instance, we implement the GetInstanceContextMode method here, our GetInstanceId method, and our GetOperationCount method in here.

So, one of the first things that we need to do is to make sure that we have the appropriate attribute uncommented in our code.

The first one we will make use of is the PerSession attribute, where the PerSession will create an instance per channel session.

So, if we take a look at our client application, it is a console based application, we create an instance of our calculator instance client.

We then get the instance mode from the GetInstanceContextMode, write that value out to our screen, and then start calling the methods and new calculations.

Notice in the first one, that we are using a client called client, so this is our first instance, and for the second instance we will have a client called client2.

We will call the new calculations with client1 and then client2, closing outer clients here, and writing out the necessary information to terminate the client information.

And the new calculations simply call each one of our operations, add service, subtract service, multiply service, we pass in the necessary values, we call the add method, or we call the subtract method as appropriate.

In our output we will write out values that we are going to be adding, subtracting, multiplying or dividing, the result. We will grab the client.InstanceId and GetOperationCount.

So, let’s go ahead and run our client application, and we will see what our first generated output results in.
So here we have used PerSession, as you can see, InstanceContextMode indicates PerSession, we see that we have called the add method, passed in the two values, got a result back, notice our InstanceId is one, and our operationCount is one.

So we have called add, subtract, multiply, and divide, all from instance one and we can see that with the instance IDs.

For each one of these same instances that we have called, we see that our operationCount increments.
Then we went ahead and used client2 to call the same methods again, notice that our instance ID has now changed to two, and that each of our operationCounts has been incremented again within that instance.
So you can see that we have really established two sessions here, and each one of the sessions maintained its own operationCount.

Let’s go ahead and terminate that client, we will come back to our service, and we will comment out the PerSession call and we will now change our attribute to a PerCall InstanceContextMode.
Note that when we change these because we have made changes in the actual attribute that is applied to the service, we need to rebuild that service one more time, and now we can go ahead and run our client application on a PerCall basis.

Notice that our InstanceContextMode indicates that we have executed a PerCall, and you will also note as we take a look at the InstanceId for each method call, regardless of the client that called it, the InstanceId has changed.

So each time we have made a call with a client, regardless of which client it was, we have actually generated a new InstanceId for each and every call.

Also take note, that because the instance changes for every call, the operationCount never gets incremented and always remains at zero. So, once again this is the client using the PerCall method.

Let’s go ahead and terminate that client. And for the final part, comment out our PerCall attribute, uncomment the Single, rebuild our service one more time, and we will start the client application again.
So our InstanceContextMode now, has been changed to Single, again we are still calling the same operations, but you will notice that regardless of the client that is making the call, and regardless of the method that is being called, our InstanceID remains at one, so this is a single instance that is servicing all clients, and you will also notice that the operationCount increments from beginning to end regardless of the number of clients that are actually calling it.

So if we were to add client3, and have client3 call one of these methods again, our InstanceID will still remain as one, but we would also notice the values of nine, ten, eleven, and twelve in our operationsCounts incremented.


So in this demonstration, you have seen how to apply the three different attributes to a service behaviour for your Windows Communication Foundation service.

No comments:

Post a Comment