Пример #1
0
        static void AddServiceAsyncWithDataContract()
        {
            try
            {
                Console.Title = "Asynchronous Pattern in Windows Communication Foundation";

                Console.WriteLine("Enter first number:");
                int number1 = Convert.ToInt32(Console.ReadLine());

                Console.WriteLine("Enter second number:");
                int             number2 = Convert.ToInt32(Console.ReadLine());
                AddDataContract input   = new AddDataContract
                {
                    Nbr1 = number1,
                    Nbr2 = number2
                };
                Console.ForegroundColor = ConsoleColor.Yellow;
                _addServiceProxy        = new ProxyFactory <IAddService>("AddService");
                _service = _addServiceProxy.CreateProxy();

                Console.WriteLine("Service Proxy created.");
                Console.WriteLine("Calling BeginAdd of WCF service");
                IAsyncResult res = _service.BeginAddDC(input, new AsyncCallback(AddCallbackDC), _service);

                Console.WriteLine("Sent request BeginAdd of WCF service");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                Console.ResetColor();
            }
        }
Пример #2
0
 static void AddCallbackDC(IAsyncResult ar)
 {
     try
     {
         Console.ForegroundColor = ConsoleColor.Yellow;
         Console.WriteLine("in AddCallbackDC");
         IAddService res = ar.AsyncState as IAddService;
         if (res != null)
         {
             Console.WriteLine("Result returned from WCF service");
             Console.WriteLine(res.EndAddDC(ar).Result.ToString());
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
     finally
     {
         if (_addServiceProxy != null)
         {
             _addServiceProxy.CloseProxy();
             Console.WriteLine("Proxy closed.");
         }
         Console.ResetColor();
     }
 }
Пример #3
0
        /// <summary>
        /// Tests the fact that the ISimpleServiceContainer set as parameter is conform to the way the interface should be used.
        /// </summary>
        /// <typeparam name="T">the service implemented by the servicecontainer's baseprovider </typeparam>
        /// <param name="container">the ISimpleServiceContainer implementation to test</param>
        /// <param name="baseProviderServiceToTest"></param>
        public void IServiceContainerConformanceTest <T>(ISimpleServiceContainer container, ISimpleServiceContainer baseProvider, T baseProviderServiceToTest)
        {
            Func <IAddService> creatorFunc = () => new AddServiceImpl();

            IServiceContainerCoAndContravariance(container, creatorFunc);

            IServiceContainerConformanceAddRemove(container, creatorFunc);

            IServiceContainerConformanceAddFailsWhenExisting(container, creatorFunc);

            IServiceContainerConformanceRemoveRecursive(container);

            container.Add <IAddService>(creatorFunc);
            container.Add <ISubstractService>(new SubstractServiceImpl());

            IAddService service = container.GetService <IAddService>();

            Assert.That(service != null);
            Assert.That(service.GetType(), Is.EqualTo(typeof(AddServiceImpl)));
            Assert.That(service.Add(1, 1), Is.EqualTo(2));

            ISubstractService substractService = container.GetService <ISubstractService>();

            Assert.That(substractService != null);
            Assert.That(substractService.GetType(), Is.EqualTo(typeof(SubstractServiceImpl)));
            Assert.That(substractService.Substract(1, 1), Is.EqualTo(0));

            //clear test
            container.Clear();

            Assert.That(container.GetService <IAddService>(), Is.Null);
            Assert.That(container.GetService <ISubstractService>(), Is.Null);

            //base provider test
            if (baseProvider != null && baseProviderServiceToTest != null)
            {
                T baseService = container.GetService <T>();
                Assert.That(baseService != null, "The baseProvider contains the specified service.");

                container.Remove(typeof(T));
                baseService = container.GetService <T>();
                Assert.That(baseService != null, "Trying to remove a base service from a child provider does nothing.");

                container.AddDisabled(typeof(T));
                Assert.That(container.GetService <T>(), Is.Null, "Access to this service is disabled");

                baseProvider.Remove(typeof(T));
                Assert.That(container.GetService <T>(), Is.Null, "Access to this service is disabled & The service doesn't exist anymore on the baseProvider");

                container.Remove(typeof(T));
                Assert.That(container.GetService <T>(), Is.Null, "The service doesn't exist anymore on the baseProvider");

                baseProvider.Add(baseProviderServiceToTest);
                Assert.That(container.GetService <T>(), Is.Not.Null, "Back to the beginning's state, the service is retrieved from the base provider.");
            }
        }
Пример #4
0
        private async Task MethodInfoTest(bool hasSessionId, RegisterOptions registerOptions, CallOptions callOptions)
        {
            WampPlayground playground = new WampPlayground();

            CallerCallee dualChannel = await playground.GetCallerCalleeDualChannel();

            IWampChannel calleeChannel = dualChannel.CalleeChannel;
            IWampChannel callerChannel = dualChannel.CallerChannel;

            MyService service = new MyService();

            Task registerTask =
                calleeChannel.RealmProxy.Services.RegisterCallee(service,
                                                                 new CalleeRegistrationInterceptor(registerOptions));

            await registerTask;

            IAddService calleeProxy =
                callerChannel.RealmProxy.Services.GetCalleeProxyPortable <IAddService>(new CalleeProxyInterceptor(callOptions));

            WampException caughtException = null;

            try
            {
                int seven = calleeProxy.Add2(3, 4);
            }
            catch (WampException ex)
            {
                caughtException = ex;
            }

            InvocationDetails details = service.Details;

            long?expectedCaller = null;

            if (hasSessionId)
            {
                expectedCaller = dualChannel.CallerSessionId;
            }

            if (registerOptions.DiscloseCaller == true && callOptions.DiscloseMe == false)
            {
                Assert.That(caughtException.ErrorUri, Is.EqualTo(WampErrors.DiscloseMeNotAllowed));
                Assert.That(details, Is.EqualTo(null));
            }
            else
            {
                Assert.That(details.Caller, Is.EqualTo(expectedCaller));
            }
        }
Пример #5
0
        static void AddServiceAsync()
        {
            Console.Title = "Asynchronous Pattern in Windows Communication Foundation";

            Console.WriteLine("Enter Number 1:");
            int number1 = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Enter Number 2:");
            int number2 = Convert.ToInt32(Console.ReadLine());

            _addServiceProxy        = new ProxyFactory <IAddService>("AddService");
            _service                = _addServiceProxy.CreateProxy();
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("Calling BeginAdd of WCF service");
            IAsyncResult res = _service.BeginAdd(number1, number2, new AsyncCallback(AddCallback), _service);

            Console.WriteLine("Sent request BeginAdd of WCF service");
            Console.ResetColor();
        }
Пример #6
0
        public void removing_a_registered_service()
        {
            int removedServicesCount = 0;

            SimpleServiceContainer container = new SimpleServiceContainer();

            container.Add(typeof(IAddService), new AddServiceImpl(), o => removedServicesCount++);

            IAddService service = container.GetService <IAddService>();

            Assert.That(service, Is.InstanceOf <AddServiceImpl>());

            Assert.That(removedServicesCount, Is.EqualTo(0));
            container.Remove(typeof(IAddService));
            Assert.That(removedServicesCount, Is.EqualTo(1));

            Assert.That(container.GetService(typeof(IAddService)), Is.Null);
            Assert.That(container.GetService <IAddService>(), Is.Null);
            Assert.Throws <CKException>(() => container.GetService <IAddService>(true));
        }
Пример #7
0
        public void removing_a_registered_service()
        {
            int removedServicesCount = 0;

            SimpleServiceContainer container = new SimpleServiceContainer();

            container.Add(typeof(IAddService), new AddServiceImpl(), o => removedServicesCount++);

            IAddService service = container.GetService <IAddService>();

            service.Should().BeOfType <AddServiceImpl>();

            removedServicesCount.Should().Be(0);
            container.Remove(typeof(IAddService));
            removedServicesCount.Should().Be(1);

            container.GetService(typeof(IAddService)).Should().BeNull();
            container.GetService <IAddService>().Should().BeNull();
            container.Invoking(sut => sut.GetService <IAddService>(true)).Should().Throw <Exception>();
        }
Пример #8
0
 static void AddCallback(IAsyncResult ar)
 {
     try
     {
         Console.ForegroundColor = ConsoleColor.Yellow;
         Console.WriteLine("in AddCallback");
         IAddService res = ar.AsyncState as IAddService;
         if (res != null)
         {
             Console.WriteLine("Result from callback");
             Console.WriteLine(res.EndAdd(ar).ToString());
         }
     }
     finally
     {
         Console.ResetColor();
         if (_addServiceProxy != null)
         {
             _addServiceProxy.CloseProxy();
         }
     }
 }
Пример #9
0
        public void SimpleServiceContainerFallbackTest()
        {
            int removedServicesCount = 0;

            SimpleServiceContainer container = new SimpleServiceContainer();

            container.Add(typeof(IAddService), new AddServiceImpl(), o => removedServicesCount++);

            IAddService service = container.GetService <IAddService>();

            Assert.That(service != null);
            Assert.That(service.GetType(), Is.EqualTo(typeof(AddServiceImpl)));
            Assert.That(service.Add(1, 1), Is.EqualTo(2));

            Assert.That(removedServicesCount, Is.EqualTo(0));
            container.Remove(typeof(IAddService));
            Assert.That(removedServicesCount, Is.EqualTo(1));

            Assert.That(container.GetService(typeof(IAddService)), Is.Null);
            Assert.That(container.GetService <IAddService>(), Is.Null);
            Assert.Throws <CKException>(() => container.GetService <IAddService>(true));
        }
Пример #10
0
 static void OnRemoveService(IAddService s)
 {
     _onRemoveServiceCalled = true;
 }
Пример #11
0
 public AddController(IAddService addService)
 {
     this.addService = addService;
 }
Пример #12
0
 public ManagementServices(IAddService <T> addService, IEditService <T> editService, IDeleteService <T> deleteService)
 {
     this._addService    = new Lazy <IAddService <T> >(() => addService);
     this._editService   = new Lazy <IEditService <T> >(() => editService);
     this._deleteService = new Lazy <IDeleteService <T> >(() => deleteService);
 }
Пример #13
0
 public void Setup()
 {
     this.addService = new AddService();
 }
Пример #14
0
 public MathController(IAddService addService)
 {
     _addService = addService;
 }
Пример #15
0
        static void Main(string[] args)
        {
            Console.WriteLine("Client started.");

            Console.WriteLine("==== simple sample ================================================================");

            ISeparationLayer separationLayer = SeparationLayerFactory.Create();
            IAddService      addService      = separationLayer.GetService <IAddService>();
            double           x = 3;
            double           y = 5;
            double           r = addService.Add(3, 5);

            Console.WriteLine("[addService demo] {0} + {1} => {2}", x, y, r);

            Console.WriteLine("==== Synchronously sample ================================================================");

            x = 30;
            y = 50;
            SeparationOperation <IAddService> addServiceOperation1 = separationLayer.CreateOperation <IAddService>();

            addServiceOperation1
            .Perform(() => { r = addServiceOperation1.Service.Add(x, y); })
            .Synchronously()
            .OnFinishInvoke(() => Console.WriteLine("finish"))
            .PostOperation();
            Console.WriteLine("[addService demo] {0} + {1} => {2}", x, y, r);

            Console.WriteLine("==== Asynchronously sample ================================================================");

            x = 300;
            y = 500;
            SeparationOperation <IAddService> addServiceOperation2 = separationLayer.CreateOperation <IAddService>();
            IAsyncOperation operation2 = addServiceOperation2
                                         .Perform(() => { r = addServiceOperation2.Service.Add(x, y); })
                                         .Asynchronously()
                                         .OnFinishInvoke(() => Console.WriteLine("finish"))
                                         .PostOperation();

            Console.WriteLine("waiting, r={0}", r);
            operation2.Wait();
            Console.WriteLine("[addService demo] {0} + {1} => {2}", x, y, r);

            Console.WriteLine("==== Cancel/Progress sample ================================================================");

            x = 3000;
            y = 5000;
            r = 0;
            SeparationOperation <IAddService> addServiceOperation3 = separationLayer.CreateOperation <IAddService>();
            IAsyncOperation operation3 = addServiceOperation3
                                         .Perform(() => { r = addServiceOperation3.Service.Add(x, y); })
                                         .Asynchronously()
                                         .OnErrorInvoke(e => Console.WriteLine("Error: {0}", e.Message))
                                         .OnCancelInvoke(() => Console.WriteLine("cancel"))
                                         .OnProgressInvoke(progressMessage => Console.WriteLine("{0} from {1}", progressMessage.Progress, progressMessage.Total))
                                         .OnFinishInvoke(() => Console.WriteLine("finish"))
                                         .PostOperation();

            Console.WriteLine("waiting, r={0}", r);
            Thread.Sleep(3000);
            operation3.Cancel();
            operation3.Wait();

            Console.WriteLine("==== Error sample ================================================================");

            SeparationOperation <IAddService> addServiceOperation4 = separationLayer.CreateOperation <IAddService>();
            IAsyncOperation operation4 = addServiceOperation4
                                         .Perform(() => { r = addServiceOperation3.Service.Add(0, 0); })
                                         .Asynchronously()
                                         .OnErrorInvoke(e => Console.WriteLine("Error: {0}", e.Message))
                                         .OnCancelInvoke(() => Console.WriteLine("cancel"))
                                         .OnFinishInvoke(() => Console.WriteLine("finish"))
                                         .PostOperation();

            Console.WriteLine("waiting, r={0}", r);
            operation4.Wait();

            Console.WriteLine("Client completed.");
        }
 public MathsService(IAddService addService)
 {
     _addService = addService;
 }
Пример #17
0
 private void CreateChannelWsDualHttpBinding()
 {
     InstanceContext instanceContext = new InstanceContext(new CallbackHandler());
     var channelFactory = new DuplexChannelFactory<IAddService>(instanceContext, "AddService_wsDualHttpBinding");
     channel = channelFactory.CreateChannel();
 }
Пример #18
0
 public Calculator(IAddService addService)
 {
     _addService = addService;
 }