private void RemoveScope(ServiceStub sender)
 {
     sender.Disposing -= RemoveScope;
     DefaultLifetimeScope scope = _createdScopes[sender];
     _createdScopes.Remove(sender);
     scope.Dispose();
 }
        public void InjectionRule_TwoTypesRegistered_ResolvedWithTypeFromRule()
        {
            // Arrange
            var serviceStub1 = new ServiceStub();
            var serviceStub2 = new ServiceStub();

            this.registrationContext.Register <IServiceStub1>().AsSingleton(serviceStub1);
            this.registrationContext.Register <IServiceStub2>().AsSingleton(serviceStub2);

            var objectInfo = new ContainerInstance(typeof(ServiceWithStubBaseInjection), this.container);

            objectInfo.As(typeof(ServiceWithStubBaseInjection));

            // Act
            objectInfo.InjectionRule(typeof(IServiceStubBase), typeof(IServiceStub2));
            var stub = (ServiceWithStubBaseInjection)objectInfo.Resolve();

            // Assert
            Assert.AreEqual(serviceStub2, stub.Child);
        }
        public void Resolve_TypeWithMethodInjection_ShouldInjectMethod()
        {
            // Arrange
            var serviceStub1 = new ServiceStub();
            var serviceStub2 = new ServiceStub();

            this.registrationContext.Register <IServiceStub1>().AsSingleton(serviceStub1);
            this.registrationContext.Register <IServiceStub2>().AsSingleton(serviceStub2);

            var objectInfo = new ContainerInstance(typeof(ServiceWithMethodInjection), this.container);

            objectInfo.As(typeof(ServiceWithMethodInjection));

            // Act
            var result = (ServiceWithMethodInjection)objectInfo.Resolve();

            // Assert
            Assert.AreEqual(serviceStub1, result.Child);
            Assert.AreEqual(serviceStub2, result.Child2);
        }
Esempio n. 4
0
        private static void ScanTypeForConstructors(Type t, List <ServiceStub> stubs)
        {
            var methods = GetStaticMethods(t);

            foreach (var method in methods)
            {
                var attrib = SRReflection.GetAttribute <ServiceConstructorAttribute>(method);

                if (attrib == null)
                {
                    continue;
                }

                if (method.ReturnType != attrib.ServiceType)
                {
                    Debug.LogError("ServiceConstructor must have return type of {2} ({0}.{1}())".Fmt(t.Name, method.Name,
                                                                                                     attrib.ServiceType));
                    continue;
                }

                if (method.GetParameters().Length > 0)
                {
                    Debug.LogError("ServiceConstructor must have no parameters ({0}.{1}())".Fmt(t.Name, method.Name));
                    continue;
                }

                var stub = stubs.FirstOrDefault(p => p.InterfaceType == attrib.ServiceType);

                if (stub == null)
                {
                    stub = new ServiceStub {
                        InterfaceType = attrib.ServiceType
                    };

                    stubs.Add(stub);
                }

                var m = method;
                stub.Constructor = () => m.Invoke(null, null);
            }
        }
Esempio n. 5
0
        public void Get_SimpleExpectationSetUpInvokingEndpointAndExpectationMet_InputParametersArePassedDownTheChain()
        {
            MsmqHelpers.Purge("shippingservice");

            ServiceStub service = Configure.Stub().NServiceBusSerializers().Restful().Create(@".\Private$\orderservice");

            const string BaseUrl      = "http://localhost:9101/";
            RestApi      restEndpoint = service.RestEndpoint(BaseUrl);

            IRouteTemplate <bool> get = restEndpoint.AddGet <bool>("/order/{id}");

            restEndpoint.Configure(get).With(Parameter.RouteParameter <int>("id").Equals(1))
            .Returns(true)
            .Send <IOrderWasPlaced, int>((msg, id) =>
            {
                msg.OrderedProduct = "stockings";
                msg.OrderNumber    = id;
            }, "shippingservice");

            service.Start();

            var client = new HttpClient {
                BaseAddress = new Uri(BaseUrl)
            };

            Task <string> getAsync = client.GetStringAsync("/order/1");

            string result = WaitVerifyNoExceptionsAndGetResult(getAsync);

            do
            {
                Thread.Sleep(100);
            } while (MsmqHelpers.GetMessageCount("shippingservice") == 0);

            client.Dispose();
            service.Dispose();

            Assert.That(result, Is.EqualTo("true"));
            Assert.That(MsmqHelpers.GetMessageCount("shippingservice"), Is.EqualTo(1), "shipping service did not recieve send");
        }
Esempio n. 6
0
        public void Post_PostWitBodyAndRouteParameter_BodyAndRouteParameterIsPassedDownTheChain()
        {
            MsmqHelpers.Purge("shippingservice");

            ServiceStub service = Configure.Stub().NServiceBusSerializers().Restful().Create(@".\Private$\orderservice");

            const string BaseUrl = "http://localhost:9101/";
            RestApi      api     = service.RestEndpoint(BaseUrl);

            IRouteTemplate post = api.AddPost("/order/{id}");

            api.Configure(post).With(Body.AsDynamic().IsEqualTo(body => body.product == "socks"))
            .Send <IOrderWasPlaced, dynamic, int>((msg, body, id) =>
            {
                msg.OrderedProduct = body.product;
                msg.OrderNumber    = id;
            }, "shippingservice");

            service.Start();

            var client = new HttpClient {
                BaseAddress = new Uri(BaseUrl)
            };

            Task <HttpResponseMessage> postAsync = client.PostAsync("/order/2", new StringContent("{\"product\":\"socks\"}", Encoding.UTF8, "application/json"));

            HttpResponseMessage message = WaitVerifyNoExceptionsAndGetResult(postAsync);

            MsmqHelpers.WaitForMessages("shippingservice");

            client.Dispose();
            service.Dispose();

            object actual = MsmqHelpers.PickMessageBody("shippingservice");

            Assert.That(actual, Is.StringContaining("socks"));
            Assert.That(actual, Is.StringContaining("2"));
            Assert.That(message.StatusCode, Is.EqualTo(HttpStatusCode.OK));
        }
Esempio n. 7
0
        public void Get_SimpleExpectationSetUpInHeaderInvokingEndpointAndExpectationMet_MessageIsSentToQueue()
        {
            MsmqHelpers.Purge("shippingservice");

            ServiceStub service = Configure.Stub().NServiceBusSerializers().Restful().Create(@".\Private$\orderservice");

            const string BaseUrl      = "http://localhost:9101/";
            RestApi      restEndpoint = service.RestEndpoint(BaseUrl);

            IRouteTemplate <bool> get = restEndpoint.AddGet <bool>("/list");

            restEndpoint.Configure(get).With(Parameter.HeaderParameter <DateTime>("Today").Equals(dt => dt.Day == 3)).Returns(true)
            .Send <IOrderWasPlaced>(msg => msg.OrderedProduct = "stockings", "shippingservice");

            service.Start();

            var client = new HttpClient {
                BaseAddress = new Uri(BaseUrl)
            };

            client.DefaultRequestHeaders.Add("Today", new DateTime(2000, 2, 3).ToString());

            Task <string> getAsync = client.GetStringAsync("list");

            string result = WaitVerifyNoExceptionsAndGetResult(getAsync);

            do
            {
                Thread.Sleep(100);
            } while (MsmqHelpers.GetMessageCount("shippingservice") == 0);

            client.Dispose();
            service.Dispose();

            Assert.That(result, Is.EqualTo("true"));
            Assert.That(MsmqHelpers.GetMessageCount("shippingservice"), Is.EqualTo(1), "shipping service did not recieve send");
        }
Esempio n. 8
0
        public void Post_PostWitBodyAndQueryParameters_BodyAndParametersFromRequestAreBound()
        {
            // Arrange
            MsmqHelpers.Purge("shippingservice");

            ServiceStub service = Configure.Stub().NServiceBusSerializers().Restful().Create(@".\Private$\orderservice");

            const string BaseUrl = "http://localhost:9101/";
            RestApi      api     = service.RestEndpoint(BaseUrl);

            IRouteTemplate post = api.AddPost("/order?id&size");

            api.Configure(post).With(Body.AsDynamic().IsEqualTo(body => body.orderId == 1)
                                     .And(Parameter.QueryParameter <int>("id").Equals(2))
                                     .And(Parameter.QueryParameter <int>("size").Equals(1))).Returns(3);

            service.Start();

            var client = new HttpClient {
                BaseAddress = new Uri(BaseUrl)
            };

            Task <HttpResponseMessage> postAsync = client.PostAsync("/order?id=2&size=1", new StringContent("{\"orderId\":\"1\"}", Encoding.UTF8, "application/json"));

            HttpResponseMessage message = WaitVerifyNoExceptionsAndGetResult(postAsync);

            Task <string> readAsStringAsync = message.Content.ReadAsStringAsync();

            readAsStringAsync.Wait();

            client.Dispose();
            service.Dispose();

            Assert.That(message.StatusCode, Is.EqualTo(HttpStatusCode.OK));
            Assert.That(readAsStringAsync.Result, Is.EqualTo("3"));
        }
        public static RestApi RestEndpoint(this ServiceStub stub, string baseUrl)
        {
            IRestApiFactory factory = stub.Extensions.OfType <IRestApiFactory>().First();

            return(factory.Create(baseUrl, stub));
        }
        public void Resolve_TypeWithPrivateMethodInjection_ShouldInjectMethod()
        {
            // Arrange
            var serviceStub1 = new ServiceStub();
            this.registrationContext.Register<IServiceStub1>().AsSingleton(serviceStub1);

            var objectInfo = new ContainerInstance(typeof(ServiceWithPrivateMethodInjection), this.container);
            objectInfo.As(typeof(ServiceWithPrivateMethodInjection));

            // Act
            var result = (ServiceWithPrivateMethodInjection)objectInfo.Resolve();

            // Assert
            Assert.AreEqual(serviceStub1, result.Child);
        }
        public void Resolve_SetInstanceAsSingleton_ShouldReturnTheSameInstance()
        {
            // Arrange
            var objectInfo = new ContainerInstance(this.registeredType, this.container);
            var instance = new ServiceStub();
            objectInfo.AsSingleton(instance);

            // Act
            var serviceStub1 = objectInfo.Resolve();

            // Assert
            Assert.AreSame(instance, serviceStub1);
        }
        public void Resolve_SetFactory_ShouldCreateNewInstance()
        {
            // Arrange
            var objectInfo = new ContainerInstance(this.registeredType, this.container);
            var serviceStub = new ServiceStub();
            objectInfo.As(() => serviceStub);

            // Act
            var result = objectInfo.Resolve();

            // Assert
            Assert.AreEqual(serviceStub, result);
        }
        public void Resolve_ResolveTypeWithDependency_ShouldAskContainerToResolveArgumentsForCtor()
        {
            // Arrange
            var serviceStub = new ServiceStub();
            this.registrationContext.Register<IServiceStub1>().AsSingleton(serviceStub);

            var objectInfo = new ContainerInstance(typeof(ServiceWithDependencyStub), this.container);
            objectInfo.As(typeof(ServiceWithDependencyStub));

            // Act
            var result = (ServiceWithDependencyStub)objectInfo.Resolve();

            // Assert
            Assert.AreEqual(serviceStub, result.Child);
        }
        public void InjectionRule_TwoTypesRegistered_ResolvedWithTypeFromRule()
        {
            // Arrange
            var serviceStub1 = new ServiceStub();
            var serviceStub2 = new ServiceStub();
            this.registrationContext.Register<IServiceStub1>().AsSingleton(serviceStub1);
            this.registrationContext.Register<IServiceStub2>().AsSingleton(serviceStub2);

            var objectInfo = new ContainerInstance(typeof(ServiceWithStubBaseInjection), this.container);
            objectInfo.As(typeof(ServiceWithStubBaseInjection));

            // Act
            objectInfo.InjectionRule(typeof(IServiceStubBase), typeof(IServiceStub2));
            var stub = (ServiceWithStubBaseInjection)objectInfo.Resolve();

            // Assert
            Assert.AreEqual(serviceStub2, stub.Child);
        }
 public InvokePostConfiguration(IRouteTemplate route, ServiceStub service)
 {
     _route = route;
     _service = service;
 }
Esempio n. 16
0
 public ReturnFromPostInvocationConfiguration(TriggeredMessageSequence sequenceBeingConfigured, ServiceStub componentBeingConfigured, NullOrInvocationReturnValueProducer returnValueProxy) : base(sequenceBeingConfigured, componentBeingConfigured)
 {
     _sequenceBeingConfigured  = sequenceBeingConfigured;
     _componentBeingConfigured = componentBeingConfigured;
     _returnValueProxy         = returnValueProxy;
 }
Esempio n. 17
0
 public InvokePostConfiguration(IRouteTemplate route, ServiceStub service)
 {
     _route   = route;
     _service = service;
 }
 /// <summary>
 /// Specify an endpoint which is configured in the .config file
 /// </summary>
 /// <example>
 /// Service contract name: Acme.IShippingService service name in app.config: Acme.IShippingServiceStub
 /// </example>
 public static WcfProxy <T> WcfEndPoint <T>(this ServiceStub stub) where T : class
 {
     return(stub.WcfEndPoint <T>(String.Empty));
 }
Esempio n. 19
0
 public SenderConfiguration(ServiceStub componentBeingConfigured, IStepConfigurableMessageSequence sequenceBeingConfigured, IStep lastSendStep)
 {
     _componentBeingConfigured = componentBeingConfigured;
     _sequenceBeingConfigured  = sequenceBeingConfigured;
     _lastSendStep             = lastSendStep;
 }
Esempio n. 20
0
 public ReturnFromGetInvocationConfiguration(IInvocationMatcher matcher, IRouteTemplate <R> route, ServiceStub service)
 {
     _matcher = matcher;
     _route   = route;
     _service = service;
 }
Esempio n. 21
0
 public InvokeGetConfiguration(IRouteTemplate <R> routeToConfigure, ServiceStub service)
 {
     _routeToConfigure = routeToConfigure;
     _service          = service;
 }
Esempio n. 22
0
 public MessageSequenceConfiguration(ServiceStub componentBeingConfigured)
 {
     _componentBeingConfigured = componentBeingConfigured;
 }
 public SendMessageExpectedNumberOfTimesConfiguration(ServiceStub componentBeingConfigured, IStepConfigurableMessageSequence sequenceBeingConfigured)
 {
     _componentBeingConfigured = componentBeingConfigured;
     _sequenceBeingConfigured  = sequenceBeingConfigured;
 }
Esempio n. 24
0
        private static void StopService(ServiceStub service)
        {
            service.Stop();
            while (service.IsRunning)
            {}

            service.Dispose();
        }
Esempio n. 25
0
 public ExpectationConfiguration(ServiceStub componentBeingConfigured, IStepConfigurableMessageSequence sequenceBeingConfigured)
 {
     _componentBeingConfigured = componentBeingConfigured;
     _sequenceBeingConfigured  = sequenceBeingConfigured;
 }
Esempio n. 26
0
 public REPL(Input input, ServiceStub service)
 {
     this.input   = input;
     this.service = service;
 }