Share constants across each WCF call between Client and Server

108 views Asked by At

A couple of integer constants are computed by WCF client before making the call to the WCF service. I would want these integer constants to be available on WCF service. Please note that I would not want them to be used as method arguments, because these constants are required irrespective of the method in the Service. Having the same argument for every method would be redundant.

To be clear, client keeps computing values for 'a' and 'b' before making a WCF service call. Once the call is made, throughout the entire call 'a' and 'b' can be accessed by the service. This is similar to using them as arguments of the method. Only difference here is that, 'a' and 'b' are needed by every method in the WCF service. So, I would prefer a better solution than adding 'a' and 'b' as arguments to every method in the WCF call.

So, let's say I have 3 methods in WCF service->

void Count1()

void Count2()

void Count3()

Client will compute different 'a' and 'b' values for each WCF method call. One solution is to keep the methods in this format.

void Count1(int a, int b)

void Count2(int a, int b)

void Count3(int a, int b)

But, I would like a better solution if possible.

Sorry about this long question.

2

There are 2 answers

0
Zohar Peled On BEST ANSWER

You could create a "Request" class for your service as a data contract, and include in this request a, b and any other value(s) you might want to get from the client. A basic request would only keep a and b, but other requests might derive from it.
This way you only send one parameter to the service for every method.

0
Ross Bush On

This is not a normal mode for WCF services, however, you can create a "sticky" service class that last for the duration of the client. Note that the InstanceContextMode is set to session meaning that this class instance will hang around as long as the client has not disconnected/abandoned or a timeout does not occurr (you will need to send a periodic pulse in order to keep it awake if your client is not chatty). Also, pay attention to the ConcurrencyMode.

[ServiceBehavior(Name = "MyStickyServicee",
    InstanceContextMode = InstanceContextMode.PerSession,
    ConcurrencyMode = ConcurrencyMode.Single)]

public class MyStickyService : IMyStickyService, IDisposable
{
}

Here is some more information on instancing in wcf.