Exemple #1
0
        public void CallbackToSyncContext()
        {
            var path    = @"net.pipe://127.0.0.1/" + this.GetType().Name + MethodBase.GetCurrentMethod().Name;
            var binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None)
            {
                MaxConnections = 5
            };

            using (var server = new ServiceHost(new SyncCallbackService(), new Uri(path)))
            {
                server.AddServiceEndpoint(typeof(ISyncCallbackService), binding, path);



                server.Open();
                using (var syncContext = new StaSynchronizationContext())
                {
                    InstanceContext context = null;
                    DuplexChannelFactory <ISyncCallbackService> channelFactory = null;
                    ISyncCallbackService client = null;
                    syncContext.Send(_ => SynchronizationContext.SetSynchronizationContext(syncContext), null);
                    syncContext.Send(_ => context        = new InstanceContext(new SyncCallbackServiceCallback()), null);
                    syncContext.Send(_ => channelFactory = new DuplexChannelFactory <ISyncCallbackService>(context, binding), null);
                    syncContext.Send(_ => client         = channelFactory.CreateChannel(new EndpointAddress(path)), null);
                    using (channelFactory)
                    {
                        var callbackThread = client.Call();
                        Assert.AreEqual(syncContext.ManagedThreadId, callbackThread);
                    }
                }
            }
        }
Exemple #2
0
        protected ServiceMetadata EnsureMetadataInitialized()
        {
            Func <ServiceMetadata> factory = () => { _synchronizer.Send(InitMetadata, this); return(this._serviceMetadata); };

            System.Threading.LazyInitializer.EnsureInitialized <ServiceMetadata>(ref this._serviceMetadata, factory);
            return(this._serviceMetadata);
        }
Exemple #3
0
        private static void NonStaThread(object state)
        {
            int id = Thread.CurrentThread.ManagedThreadId;

            for (int i = 0; i < 10; i++)
            {
                var param = new Params {
                    OriginalThread = id, CallCounter = i
                };
                mStaSyncContext.Send(RunOnStaThread, param);
                Debug.Assert(param.Output == "Processed", "Unexpected behavior by STA thread");
            }
        }
 public static void ExecuteOnSTA(SendOrPostCallback action, object state)
 {
     _synchronizer.Send(action, state);
 }