private void Configure(HttpHostConfiguration configuration)
 {
     this.configuration           = configuration;
     this.OperationHandlerFactory = configuration.OperationHandlerFactory;
     this.MessageHandlerFactory   = configuration.MessageHandlerFactory;
     AddDefaultEndpoints();
 }
 private void Configure(HttpHostConfiguration configuration)
 {
     this.configuration = configuration;
     this.OperationHandlerFactory = configuration.OperationHandlerFactory;
     this.MessageHandlerFactory = configuration.MessageHandlerFactory;
     AddDefaultEndpoints();
 }
    public void WhenQueryingRepeatedArgument_ThenServiceGetsBoth()
    {
        var baseUri = new Uri("http://localhost:20000");
        var service = new ProductsService();
        var config  = HttpHostConfiguration.Create()
                      .SetResourceFactory(new SingletonResourceFactory(service))
                      .UseJsonNet()
                      .AddMessageHandlers(typeof(TracingChannel));

        using (new SafeHostDisposer(
                   new HttpQueryableServiceHost(typeof(ProductsService), 25, config, new Uri(baseUri, "products"))))
        {
            var client = new HttpEntityClient(baseUri);
            var query  = (IHttpEntityQuery <Product>)client.Query <Product>("products/search2",
                                                                            new HttpNameValueCollection {
                { "search", "10" }, { "search", "20" }, { "count", "25" }
            });

            var result = query.Execute();

            Assert.Equal(2, result.TotalCount);
            Assert.Equal(2, result.Count());
            Assert.Equal(10, result.First().Id);
        }
    }
 public void WhenCreatedThenOperationHandlerFactoryIsSet()
 {
     var config = new HttpHostConfiguration();
     config.OperationHandlerFactory = new TestOperationHandlerFactory();
     var host = new HttpConfigurableServiceHost(typeof(TestService), config, new Uri(DummyUri));
     Assert.AreEqual(config.OperationHandlerFactory, host.OperationHandlerFactory);
 }
Example #5
0
        public void WhenCreatedWithATypeThenConfigurationIsSet()
        {
            var config = new HttpHostConfiguration();
            var host   = new HttpConfigurableServiceHost(typeof(TestService), config, new Uri(DummyUri));

            Assert.AreEqual(config, host.configuration);
        }
 public void WhenCreatedThenHttpEndpointIsAddedToTheService()
 {
     var config = new HttpHostConfiguration();
     var host = new HttpConfigurableServiceHost(typeof (TestService), config, new Uri(DummyUri));
     var httpEndpoint = (HttpEndpoint) host.Description.Endpoints.Find(typeof (TestService));
     Assert.IsNotNull(httpEndpoint);
 }
Example #7
0
        public void WhenCreatedWithAnInstanceThenServiceTypeIsSet()
        {
            var config = new HttpHostConfiguration();
            var host   = new HttpConfigurableServiceHost(new TestService(), config, new Uri(DummyUri));

            Assert.AreEqual(typeof(TestService), host.serviceType);
        }
Example #8
0
        public void WhenCreatedThenAspNetCompatibilityRequirementsAttributeIsAdded()
        {
            var config = new HttpHostConfiguration();
            var host   = new HttpConfigurableServiceHost(new TestService(), config, new Uri(DummyUri));

            Assert.IsTrue(host.Description.Behaviors.Contains(typeof(AspNetCompatibilityRequirementsAttribute)));
        }
Example #9
0
    public void WhenOrdering_ThenSucceeds()
    {
        var config = HttpHostConfiguration.Create();

        config.Configuration.OperationHandlerFactory.Formatters.Insert(0, new JsonNetMediaTypeFormatter());

        using (var ws = new HttpWebService <TestService>("http://localhost:20000", "products", config))
        {
            var client = new HttpClient("http://localhost:20000");
            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("text/json"));

            var context = new DataServiceContext(new Uri("http://localhost:20000"));
            // We always specify how many to take, to be explicit.
            var query = context.CreateQuery <Product>("products")
                        .Where(x => x.Owner.Name == "kzu")
                        .OrderBy(x => x.Id)
                        .ThenBy(x => x.Owner.Id)
                        .Skip(1)
                        .Take(1);

            //var uri = ((DataServiceQuery)query).RequestUri;
            var uri = new Uri(((DataServiceQuery)query).RequestUri.ToString().Replace("()?$", "?$"));
            Console.WriteLine(uri);
            var response = client.Get(uri);

            Assert.True(response.IsSuccessStatusCode, "Failed : " + response.StatusCode + " " + response.ReasonPhrase);

            var products = new JsonSerializer().Deserialize <List <Product> >(new JsonTextReader(new StreamReader(response.Content.ContentReadStream)));

            Assert.Equal(1, products.Count);
        }
    }
Example #10
0
        public void WhenSetErrorHandlerIsCalledWithAnInstanceThenErrorHandlerIsSet()
        {
            var builder = HttpHostConfiguration.Create();
            HttpErrorHandler errorHandler = new TestHttpErrorHandler();

            builder.SetErrorHandler(errorHandler);
        }
Example #11
0
        public void WhenCreatedThenHttpEndpointIsAddedToTheService()
        {
            var config       = new HttpHostConfiguration();
            var host         = new HttpConfigurableServiceHost(typeof(TestService), config, new Uri(DummyUri));
            var httpEndpoint = (HttpEndpoint)host.Description.Endpoints.Find(typeof(TestService));

            Assert.IsNotNull(httpEndpoint);
        }
Example #12
0
        public void WhenSetInstanceFactoryIsCalledPassingALambdaThenInstanceFactoryIsSetToDelegateFactory()
        {
            var builder = HttpHostConfiguration.Create();

            builder.SetResourceFactory((t, i, o) => null, (i, o) => { });
            Assert.IsNotNull(builder.ToConfiguration().InstanceFactory);
            Assert.IsTrue(builder.ToConfiguration().InstanceFactory is DelegateInstanceFactory);
        }
Example #13
0
        public void WhenSetErrorHandlerIsCalledPassingALamdaThenErrorHandlerIsSet()
        {
            var builder = HttpHostConfiguration.Create();

            builder.SetErrorHandler((e) => true, (e) => null);
            Assert.IsNotNull(builder.ToConfiguration().ErrorHandler);
            Assert.IsTrue(builder.ToConfiguration().ErrorHandler is DelegateErrorHandler);
        }
Example #14
0
        public void WhenSetInstanceFactoryIsCalledWithAnInstanceThenInstanceFactoryIsSet()
        {
            var builder         = HttpHostConfiguration.Create();
            var instanceFactory = new TestInstanceFactory();

            builder.SetResourceFactory(instanceFactory);
            Assert.AreEqual(instanceFactory, builder.ToConfiguration().InstanceFactory);
        }
Example #15
0
        public void WhenSetMessageHandlerFactoryIsCalledPassingALamdaThenMessageHandlerFactoryIsSetToDelegateFactory()
        {
            var builder = HttpHostConfiguration.Create();

            builder.SetMessageHandlerFactory((c) => null);
            Assert.IsNotNull(builder.ToConfiguration().MessageHandlerFactory);
            Assert.IsTrue(builder.ToConfiguration().MessageHandlerFactory is DelegateMessageHandlerFactory);
        }
Example #16
0
        public void WhenSetMessageHandlerFactoryIsCalledWithAnInstanceThenMessageHandlerFactoryIsSet()
        {
            var builder = HttpHostConfiguration.Create();
            var factory = new TestMessageHandlerFactory();

            builder.SetMessageHandlerFactory(factory);
            Assert.AreEqual(factory, builder.ToConfiguration().MessageHandlerFactory);
        }
Example #17
0
        public void WhenCreatedThenMessageHandlerFactoryIsSet()
        {
            var config = new HttpHostConfiguration();

            config.MessageHandlerFactory = new TestMessageHandlerFactory();
            var host = new HttpConfigurableServiceHost(typeof(TestService), config, new Uri(DummyUri));

            Assert.AreEqual(config.MessageHandlerFactory, host.MessageHandlerFactory);
        }
Example #18
0
        public void WhenAddFormattersIsCalledThenFormattersAreAdded()
        {
            var builder   = HttpHostConfiguration.Create();
            var formatter = new TestMediaTypeFormatter();

            builder.AddFormatters(formatter);
            var config = (HttpHostConfiguration)builder;

            Assert.IsTrue(config.formatters.Contains(formatter));
        }
Example #19
0
        public void WhenAddResponseHandlersIsCalledThenHandlerItemIsAdded()
        {
            var builder = HttpHostConfiguration.Create();
            Action <Collection <HttpOperationHandler> > handlers = c => { };

            builder.AddResponseHandlers(handlers, (s, o) => true);
            var config = (HttpHostConfiguration)builder;

            Assert.AreEqual(handlers, config.responseHandlers[0].Handlers);
        }
Example #20
0
        public void WhenHttpEndpointIsCreatedThenMessageHandlerFactoryIsSet()
        {
            var config = new HttpHostConfiguration();

            config.MessageHandlerFactory = new TestMessageHandlerFactory();
            var host         = new HttpConfigurableServiceHost(typeof(TestService), config, new Uri(DummyUri));
            var httpEndpoint = (HttpEndpoint)host.Description.Endpoints.Find(typeof(TestService));

            Assert.AreEqual(config.MessageHandlerFactory, httpEndpoint.MessageHandlerFactory);
        }
Example #21
0
        public void WhenHttpEndpointIsCreatedThenInstanceProviderBehaviorIsAdded()
        {
            var config = new HttpHostConfiguration();

            config.InstanceFactory = new TestInstanceFactory();
            var host         = new HttpConfigurableServiceHost(typeof(TestService), config, new Uri(DummyUri));
            var httpEndpoint = (HttpEndpoint)host.Description.Endpoints.Find(typeof(TestService));

            Assert.IsTrue(httpEndpoint.Behaviors.Contains(typeof(InstanceProviderBehavior)));
        }
Example #22
0
        protected void Application_Start(object sender, EventArgs e)
        {
            var containerBuilder = new ContainerBuilder();

            containerBuilder.RegisterModule(new MainModule());
            var container = containerBuilder.Build();

            _configuration = HttpHostConfiguration.Create()
                             .AddFormatters(new RestbucksMediaTypeFormatter())
                             .SetResourceFactory(new RestbucksResourceFactory(container));

            RestbucksResources.RegisterRoutes(Configuration);
        }
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default",                                                                 // Route name
                "{controller}/{action}/{id}",                                              // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
                new { controller = "^(?!SignupSvc).*" }
                );

            var bldr = HttpHostConfiguration.Create().SetOperationHandlerFactory(new JsonOnlyFactory());

            routes.MapServiceRoute <SignupSvc>("SignupSvc", bldr);
        }
Example #24
0
        public static void Initialize()
        {
            Container.AddFacility <TypedFactoryFacility>();
            Container.Kernel.Resolver.AddSubResolver(new CollectionResolver(Container.Kernel, true));
            Container.Install(FromAssembly.This());

            var config = HttpHostConfiguration.Create()
                         .SetErrorHandler(Container.Resolve <HttpErrorHandler>())
                         .SetResourceFactory(new WindsorResourceFactory(Container))
                         .SetOperationHandlerFactory(Container.Resolve <HttpOperationHandlerFactory>());

            RouteTableConfigurator.Configure(config);

            //Initialize Barista
            Container.Resolve <Barista>();
        }
Example #25
0
    public void WhenHostingDisposableService_ThenDefaultActivatorFactoryDisposesIt()
    {
        using (var webservice = new HttpWebService <TestService>(
                   serviceBaseUrl: "http://localhost:20000",
                   serviceResourcePath: "test",
                   serviceConfiguration: HttpHostConfiguration.Create()))
        {
            var client = new HttpClient(webservice.BaseUri);

            // Builds: http://localhost:2000/test/23
            var uri      = webservice.Uri("23");
            var response = client.Get(uri);
        }

        Assert.True(TestService.IsDisposed);
    }
Example #26
0
    public void WhenHostingService_ThenCanInvokeIt()
    {
        using (var webservice = new HttpWebService <TestService>(
                   serviceBaseUrl: "http://localhost:20000",
                   serviceResourcePath: "test",
                   serviceConfiguration: HttpHostConfiguration.Create()))
        {
            var client = new HttpClient(webservice.BaseUri);

            // Builds: http://localhost:2000/test/23
            var uri      = webservice.Uri("23");
            var response = client.Get(uri);

            Assert.True(response.IsSuccessStatusCode, response.ToString());
            Assert.True(response.Content.ReadAsString().Contains("23"));
        }
    }
Example #27
0
        protected void Application_Start(object sender, EventArgs e)
        {
            // use MEF for providing instances
            var catalog   = new AssemblyCatalog(typeof(Global).Assembly);
            var container = new CompositionContainer(catalog);
            var config    = HttpHostConfiguration.Create().
                            AddFormatters(
                new ContactPngFormatter(),
                new ContactFeedFormatter("http://localhost:9000/Contact"),
                new VCardFormatter(),
                new CalendarFormatter()).
                            SetResourceFactory(new MefResourceFactory(container)).
                            AddMessageHandlers(typeof(LoggingChannel), typeof(UriFormatExtensionMessageChannel));

            SetMappings();

            RouteTable.Routes.MapServiceRoute <ContactResource>("Contact", config);
            RouteTable.Routes.MapServiceRoute <ContactsResource>("Contacts", config);
        }
    private static void TestWithContentType(string contentType, Func <HttpContent, JsonReader> readerFactory)
    {
        var config = HttpHostConfiguration.Create().UseJsonNet();

        using (var webservice = new HttpWebService <TestService>("http://localhost:20000", "test", config))
        {
            var client = new HttpClient(webservice.BaseUri);
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType));

            var response = client.Get(webservice.Uri(25));

            Assert.True(response.IsSuccessStatusCode, response.ToString());

            var product = new JsonSerializer().Deserialize <Product>(readerFactory(response.Content));

            Assert.Equal(25, product.Id);
            Assert.Equal("kzu", product.Owner.Name);
        }
    }
Example #29
0
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);

            var container = new Container();

            container.Configure(x =>
            {
                x.For <IContactRepository>()
                .Use <SisoDbContactRepository>();
            });

            var configuration = HttpHostConfiguration.Create()
                                .SetResourceFactory((t, i, m) => container.GetInstance(t), (i, o) => { })
                                .AddMessageHandlers(typeof(PostStatusCodeMessageHandler));

            RouteTable.Routes.MapServiceRoute <ContactsResource>("contacts", configuration);
        }
Example #30
0
    public void WhenPaging_ThenGetsResponse()
    {
        var config = HttpHostConfiguration.Create();

        config.Configuration.OperationHandlerFactory.Formatters.Insert(0, new JsonNetMediaTypeFormatter());

        using (var ws = new HttpWebService <TestService>("http://localhost:20000", "products", config))
        {
            var client = new HttpClient("http://localhost:20000");
            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("text/json"));

            var response = client.Query <Product>("products", x => x.Id <= 2, skip: 1, take: 1);

            Assert.True(response.IsSuccessStatusCode);

            var products = new JsonSerializer().Deserialize <List <Product> >(new JsonTextReader(new StreamReader(response.Content.ContentReadStream)));

            Assert.Equal(1, products.Count);
            Assert.Equal(2, products[0].Id);
        }
    }
    public void WhenQueryingOverLimit_ThenGetsLimitedResults()
    {
        var baseUri = new Uri("http://localhost:20000");
        var service = new ProductsService();
        var config  = HttpHostConfiguration.Create()
                      .SetResourceFactory(new SingletonResourceFactory(service))
                      .AddMessageHandlers(typeof(TracingChannel));

        using (new SafeHostDisposer(
                   new HttpQueryableServiceHost(typeof(ProductsService), 25, config, new Uri(baseUri, "products"))))
        {
            var client = new HttpEntityClient(baseUri);
            var query  = (IHttpEntityQuery <Product>)client.Query <Product>("products").Skip(10).Take(50);

            var result = query.Execute();

            Assert.Equal(100, result.TotalCount);
            Assert.Equal(25, result.Count());
            Assert.Equal(10, result.First().Id);
        }
    }
Example #32
0
    public void WhenHostingCachedServiceInstance_ThenGetsSameResultAlways()
    {
        using (var webservice = new HttpWebService <TestService>(
                   cacheServiceInstance: true,
                   serviceBaseUrl: "http://localhost:20000",
                   serviceResourcePath: "test",
                   serviceConfiguration: HttpHostConfiguration.Create()))
        {
            var client = new HttpClient(webservice.BaseUri);

            // Builds: http://localhost:2000/test/23
            var uri      = webservice.Uri("23");
            var response = client.Get(uri);

            Assert.True(response.IsSuccessStatusCode, response.ToString());

            var content1 = response.Content.ReadAsString();
            var content2 = client.Get(uri).Content.ReadAsString();
            Assert.Equal(content1, content2);
        }
    }
Example #33
0
    public void WhenExecutingRequest_ThenTracesInformationHeaders()
    {
        var listener = new ConsoleTraceListener();

        TracerExtensibility.AddListener(SourceName.For <TracingChannel>(), listener);
        TracerExtensibility.SetTracingLevel(SourceName.For <TracingChannel>(), SourceLevels.All);

        var config = HttpHostConfiguration.Create()
                     .UseJsonNet()
                     .AddMessageHandlers(typeof(TracingChannel));

        using (var ws = new HttpWebService <TestService>("http://localhost:20000", "products", true, config))
        {
            var client   = new HttpEntityClient(ws.BaseUri);
            var products = client.Query <Product>().Skip(1).Take(1).ToList();

            Assert.Equal(1, products.Count);
            Assert.Equal(2, products[0].Id);
        }

        listener.Flush();
    }
 public void WhenHttpEndpointIsCreatedThenOperationHandlerFactoryIsSet()
 {
     var config = new HttpHostConfiguration();
     config.OperationHandlerFactory = new TestOperationHandlerFactory();
     var host = new HttpConfigurableServiceHost(typeof(TestService), config, new Uri(DummyUri));
     var httpEndpoint = (HttpEndpoint)host.Description.Endpoints.Find(typeof(TestService));
     Assert.AreEqual(config.OperationHandlerFactory, httpEndpoint.OperationHandlerFactory);
 }
 public void WhenCreatedThenAspNetCompatibilityRequirementsAttributeIsAdded()
 {
     var config = new HttpHostConfiguration();
     var host = new HttpConfigurableServiceHost(new TestService(), config, new Uri(DummyUri));
     Assert.IsTrue(host.Description.Behaviors.Contains(typeof (AspNetCompatibilityRequirementsAttribute)));
 }
 public void WhenCreatedWithAnInstanceThenConfigurationIsSet()
 {
     var config = new HttpHostConfiguration();
     var host = new HttpConfigurableServiceHost(new TestService(), config, new Uri(DummyUri));
     Assert.AreEqual(config, host.configuration);
 }
 public void WhenHttpEndpointIsCreatedThenInstanceProviderBehaviorIsAdded()
 {
     var config = new HttpHostConfiguration();
     config.InstanceFactory = new TestInstanceFactory();
     var host = new HttpConfigurableServiceHost(typeof(TestService), config, new Uri(DummyUri));
     var httpEndpoint = (HttpEndpoint)host.Description.Endpoints.Find(typeof(TestService));
     Assert.IsTrue(httpEndpoint.Behaviors.Contains(typeof (InstanceProviderBehavior)));
 }
 protected void Application_Start(object sender, EventArgs e)
 {
     var config = new HttpHostConfiguration();
       config.AddFormatters(new ContactsMediaTypeFormatterXml("http://localhost:62192"), new ContactsMediaTypeFormatterJson("http://localhost:62192"));
       RouteTable.Routes.MapServiceRoute<ContactsApi>("contacts", config);
 }
 public void WhenCreatedWithATypeThenServiceTypeIsSet()
 {
     var config = new HttpHostConfiguration();
     var host = new HttpConfigurableServiceHost(typeof (TestService), config,new Uri(DummyUri));
     Assert.AreEqual(typeof(TestService), host.serviceType);
 }