public void InitializeInstanceContext (InstanceContext instanceContext, Message message, IContextChannel channel)
		{
			var key = channel.SessionId ?? String.Empty;
			pool [key] = instanceContext;
			channel.Closed += delegate {
				pool.Remove (key);
				instanceContext.Close (); // FIXME: timeout?
			};
		}
Пример #2
0
		InstanceContext CreateInstanceContext (MessageProcessingContext mrc)
		{
			InstanceContext iCtx = null;
			DispatchRuntime dispatchRuntime = mrc.OperationContext.EndpointDispatcher.DispatchRuntime;
			IInstanceContextProvider p = dispatchRuntime.InstanceContextProvider;

			if (p != null) {
				iCtx = p.GetExistingInstanceContext (mrc.IncomingMessage, mrc.OperationContext.Channel);
			}
			if (iCtx == null) {
				ServiceHostBase host = dispatchRuntime.ChannelDispatcher.Host;
				iCtx = new InstanceContext (dispatchRuntime.ChannelDispatcher.Host, null, false);
				// FIXME: could be easier way to identify session channel
				if ((mrc.Channel is ISessionChannel<IInputSession> || mrc.Channel is ISessionChannel<IDuplexSession>) && host.Description.Behaviors.Find<ServiceBehaviorAttribute> ().InstanceContextMode == InstanceContextMode.PerSession)
					mrc.Channel.Closed += delegate { iCtx.Close (); };
			}

			iCtx.InstanceManager = new InstanceManager (dispatchRuntime);
			return iCtx;
		}		
Пример #3
0
        static void Main(string[] args)
        {
            ServiceHost host = null;
            try
            {
                using (host = new ServiceHost(typeof(Test)))
                {
                    host.Open();

                    Thread.Sleep(2500);
                    Console.WriteLine("\npress any key to continue ...\n");
                    Console.ReadLine();

                    var testCallback = new TestCallback();
                    var context = new InstanceContext(testCallback);

                    // action
                    worker(1234567890, "gaga", context);

                    Console.WriteLine("\n+++ press any key to close a ServiceHost +++\n");
                    Console.ReadLine();

                    context.Close();
                    host.Close();
                }
            }
            catch (CommunicationException ex)
            {
                if (host != null)
                    host.Abort();
 
                Console.WriteLine(ex.Message);
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                if (host != null)
                    host.Abort();
   
                Console.WriteLine(ex);
                Console.ReadLine();
            }
            finally
            {
                Console.WriteLine("\n*** press any key to exit ... ***\n");
                Console.ReadLine();
            }

        }