コード例 #1
0
        public void CallbackToSyncContext()
        {
            var path    = @"net.pipe://127.0.0.1/" + this.GetType().Name + MethodBase.GetCurrentMethod().Name;
            var binding = new NetNamedPipeBinding()
            {
                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;
                    NDceRpc.ServiceModel.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 NDceRpc.ServiceModel.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);
                    }
                }
            }
        }
コード例 #2
0
        public void CallbackToSyncContext()
        {
            var path = @"net.pipe://127.0.0.1/" + this.GetType().Name + MethodBase.GetCurrentMethod().Name;
            var binding = new NetNamedPipeBinding() { 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;
                    NDceRpc.ServiceModel.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 NDceRpc.ServiceModel.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);
                    }
                }

            }
        }
コード例 #3
0
        private static void DoHostWithCallbackInternal(Reportwatch reportWatch, Binding binding, string path)
        {
            reportWatch.Start("ServiceHost ctor");
            using (var server = new ServiceHost(new CallbackService(), new Uri(path)))
            {
                reportWatch.Stop("ServiceHost ctor");

                reportWatch.Start("AddServiceEndpoint");
                server.AddServiceEndpoint(typeof(ICallbackService), binding, path);
                reportWatch.Stop("AddServiceEndpoint");

                reportWatch.Start("Open");
                server.Open();
                reportWatch.Stop("Open");

                reportWatch.Start("ChannelFactory ctor");
                using (var context = new InstanceContext(new CallbackServiceCallback()))
                {
                    using (var channelFactory = new NDceRpc.ServiceModel.DuplexChannelFactory <ICallbackService>(context, binding))
                    {
                        reportWatch.Stop("ChannelFactory ctor");

                        reportWatch.Start("CreateChannel");
                        var client = channelFactory.CreateChannel(new EndpointAddress(path));
                        reportWatch.Stop("CreateChannel");

                        reportWatch.Start("Execute");
                        client.Call();
                        reportWatch.Stop("Execute");
                    }
                }
            }
        }
コード例 #4
0
ファイル: StartupTimeTests.cs プロジェクト: OpenSharp/NDceRpc
        private static void DoHostWithCallbackInternal(Reportwatch reportWatch, Binding binding, string path)
        {
            reportWatch.Start("ServiceHost ctor");
            using (var server = new ServiceHost(new CallbackService(), new Uri(path)))
            {
                reportWatch.Stop("ServiceHost ctor");

                reportWatch.Start("AddServiceEndpoint");
                server.AddServiceEndpoint(typeof(ICallbackService), binding, path);
                reportWatch.Stop("AddServiceEndpoint");

                reportWatch.Start("Open");
                server.Open();
                reportWatch.Stop("Open");

                reportWatch.Start("ChannelFactory ctor");
                using (  var context = new InstanceContext(new CallbackServiceCallback()))
                {
                    using (var channelFactory = new NDceRpc.ServiceModel.DuplexChannelFactory<ICallbackService>(context, binding))
                    {
                        reportWatch.Stop("ChannelFactory ctor");

                        reportWatch.Start("CreateChannel");
                        var client = channelFactory.CreateChannel(new EndpointAddress(path));
                        reportWatch.Stop("CreateChannel");

                        reportWatch.Start("Execute");
                        client.Call();
                        reportWatch.Stop("Execute");
                    }
                }

            }
        }