Tuesday, July 7, 2015

WCF Sessions (and its dependency on bindings) demo


To find the instance id of the WCF instance (for monitoring purpose) use the following code –snippet

public class Service1 : IService1
    {
        private string instanceId = "";

        public Service1()
        {
            instanceId = Guid.NewGuid().ToString();
        }
        public string GetData(int value)
        {
Trace.WriteLine(String.Concat("Current WCF Instance Id is : ", instanceId));
File.AppendAllText(@"C:\Temp\logs.txt", "Current WCF Instance Id is : " + instanceId);
            return string.Format("You entered: {0}", value);
        }

You will get a different GUID printed each time if the call is per-call
By default, its per-session, but you might get different GUIDs if the binding does not support session (example basicHttpBinding). Test it by opening the WCfTestClient tool.
If you try to enforce session in the contract by specifying  -
[ServiceContract(SessionMode = SessionMode.Required)]
    public interface IService1

You will get an error when you try to add a reference to the service (if the binding is still basicHttpBinding).
The above method is a good way to test the service instance creation and its dependency on binding.

No comments:

Post a Comment