예제 #1
0
        public void InitializeInstanceContext(InstanceContext instanceContext, System.ServiceModel.Channels.Message message, IContextChannel channel)
        {
            //Look if the Client has given us a unique ID to add to this InstanceContext
            int    headerIndex = message.Headers.FindHeader(CustomHeader.HeaderName, CustomHeader.HeaderNamespace);
            String headerId    = null;

            if (headerIndex != -1)
            {
                headerId = message.Headers.GetHeader <string>(headerIndex);
            }

            if (headerId == null)
            {
                //If no header was sent by the Client, then create a new one and assign it to this InstanceContext.
                headerId = Guid.NewGuid().ToString();
                Utility.WriteMessageToConsole(String.Format(ResourceHelper.GetString("NoHeaderFound")));
            }

            Utility.WriteMessageToConsole(String.Format(ResourceHelper.GetString("InstanceContextAddedToCache"), headerId));

            //Add this to the Cache
            this.instanceContextCache[headerId] = instanceContext;

            //Register the Closing event of this InstancContext so it can be removed from the collection
            instanceContext.Closing += this.RemoveInstanceContext;

            IExtension <InstanceContext> customLeaseExtension =
                new CustomLeaseExtension(timeout, headerId);

            instanceContext.Extensions.Add(customLeaseExtension);
        }
예제 #2
0
        /// <summary>
        /// This implements a PerCall InstanceContextMode behavior. If a cached InstanceContext is not found
        /// then WCF will create a new one.
        /// </summary>
        public InstanceContext GetExistingInstanceContext(System.ServiceModel.Channels.Message message, IContextChannel channel)
        {
            //Per Session behavior
            //To implement a PerSession behavior (If underlyin binding supports it) where in all
            //methods from one ChannelFactory will be serviced by the same InstanceContext

            //Check if the incoming request has the InstanceContext id it wants to connect with.
            if (message.Headers.FindHeader(CustomHeader.HeaderName, CustomHeader.HeaderNamespace) != -1)
            {
                String sharingId = message.Headers.GetHeader <string>(CustomHeader.HeaderName, CustomHeader.HeaderNamespace);
                if (sharingId != null && instanceContextCache.ContainsKey(sharingId))
                {
                    Utility.WriteMessageToConsole(String.Format(ResourceHelper.GetString("InstanceContextLookup"), sharingId));
                    //Retrieve the InstanceContext from the map
                    InstanceContext context = instanceContextCache[sharingId];
                    if (context != null)
                    {
                        //Before returning, stop the timer on this InstanceContext
                        CustomLeaseExtension extension = context.Extensions.Find <CustomLeaseExtension>();
                        Utility.WriteMessageToConsole(String.Format(ResourceHelper.GetString("StopInstanceContextIdleTimer"), sharingId));
                        extension.StopTimer();

                        Utility.WriteMessageToConsole(ResourceHelper.GetString("CachedInstanceContextFound"));
                        return(instanceContextCache[sharingId]);
                    }
                }
            }

            //No existing InstanceContext was found so return null and WCF will create a new one.
            return(null);
        }
예제 #3
0
        public void RemoveInstanceContext(object o, EventArgs args)
        {
            InstanceContext      context   = o as InstanceContext;
            CustomLeaseExtension extension = context.Extensions.Find <CustomLeaseExtension>();
            String id = (extension != null) ? extension.InstanceId : null;

            if (this.instanceContextCache[id] != null)
            {
                Utility.WriteMessageToConsole(String.Format(ResourceHelper.GetString("InstanceContextRemovedFromCache"), id));
                this.instanceContextCache.Remove(id);
            }
        }