I have been working on several projects lately that utilize the cloud for many different things, including service, session, database and client hosting. The ultimate goal is to have the flexibility to host solutions in the cloud in a fully available manner that is flexible to allocate more resources when needed. I have run into a case where I would like to pass more information into WCF functions that allows me to confirm caller identity, and manage some other things behind the scenes, without passing this information as parameters.

My original thinking was to somehow extend the functionality of WCF services itself to allow for adding parameters that would be set at initial session start.

An example being:

 

Code Snippet
  1. Service1Client myService = new Service1Client();
  2. myService.itemToPass = "Hello World!";

 

I didn’t really find anything that could meet my needs! It amazes me that there wasn’t more information out there regarding this type of data passing, since it seems a lot more reasonable to include such a offering, instead of passing the same value to all methods within a service. So, I asked on a couple forums and one post suggested possibly using a static variable for this, so I finally found a solution for it!

So on the service level I declared a private static variable for storing the value globally to be available for other methods within the service. I also created a setter method to enable, on setting up a connection, the availability to set this variable.

Code Snippet
  1. public class Service1 : IService1 {
  2. private static int customVar { get; set; }
  3. public void setCustomVar(int CustomVar) {
  4.     customVar = CustomVar;
  5. }
  6. // …

 

You ultimately could pass multiple things through a service, but remember to limit this to variables that MUST be provided globally. So from here, it is just a matter of (on the client) connecting to the service and setting this variable.

 

Code Snippet
  1. Service1Client myService = new Service1Client();
  2. myService.setCustomVar(12345);

 

Now that we have that variable set, it can be utilized for the lifetime of the connection to the service. This is very useful for those variables you want to have available globally, but don’t want to insert this parameter into every call!