/// <summary> /// Asserts that an <see cref="HttpRequestMessage"/> sent to the self-hosted HTTP <paramref name="serviceSingleton"/> /// results in the actual <see cref="HttpResponseMessage"/> given by the <paramref name="onGetActualResponse"/> <see cref="Action<>"/>. /// </summary> /// <param name="serviceSingleton">The singleton for the self-hosted HTTP service. Should not be <c>null</c>.</param> /// <param name="behavior">The <see cref="HttpBehavior"/> to use with the HTTP service.</param> /// <param name="binding">The <see cref="HttpBinding"/> to use with the HTTP service.</param> /// <param name="request">The <see cref="HttpRequestMessage"/> instance to send to the self-hosted HTTP service. Should not be <c>null</c>.</param> /// <param name="onGetActualResponse">The actual <see cref="HttpResponseMessage"/> instance provided as an <see cref="Action<>"/>. Should not be <c>null</c>.</param> public static void Execute(object serviceSingleton, HttpBehavior behavior, HttpBinding binding, HttpRequestMessage request, Action<HttpResponseMessage> onGetActualResponse) { Assert.IsNotNull(serviceSingleton, "The 'serviceSingleton' parameter should not be null."); Assert.IsNotNull(request, "The 'request' parameter should not be null."); Assert.IsNotNull(onGetActualResponse, "The 'onGetActualResponse' parameter should not be null."); ServiceHostAssert.Execute((baseAddress) => GetServiceHost(serviceSingleton, behavior, binding, baseAddress), request, onGetActualResponse); }
static void HostWithHttpsEndpoint() { var repository = new ToDoMemoryRepository(); repository.Add(new ToDo("Must learn HTTP better")); var instance = new TodoResource(repository); using (var host = new HttpServiceHost(instance, "http://localhost:8080/todo")) { host.AddServiceEndpoint(typeof(TodoResource), new HttpBinding(), "http://localhost:8080/todo"); var binding = new HttpBinding(HttpBindingSecurityMode.Transport); binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None; host.AddServiceEndpoint(typeof(TodoResource), binding, "https://localhost:8435/todo"); //host.Credentials.ServiceCertificate.SetCertificate(StoreLocation.CurrentUser,StoreName.My,X509FindType.FindBySubjectName, "gaviao"); host.Open(); ShowEndpointsOf(host); WaitForKey(); } }
private static ServiceHost GetServiceHost(Type serviceType, HttpBehavior behavior, HttpBinding binding, Uri baseAddress) { ServiceHost host = new ServiceHost(serviceType, baseAddress); ConfigureInstanceContextMode(host, InstanceContextMode.PerCall); AddEndpoint(host, serviceType, behavior, binding); return host; }
private static ServiceHost GetServiceHost(object serviceSingleton, HttpBehavior behavior, HttpBinding binding, Uri baseAddress) { ServiceHost host = new ServiceHost(serviceSingleton, baseAddress); ConfigureInstanceContextMode(host, InstanceContextMode.Single); AddEndpoint(host, serviceSingleton.GetType(), behavior, binding); return host; }
private static void AddEndpoint(ServiceHost host, Type serviceType, HttpBehavior behavior, HttpBinding binding) { if (behavior == null) { behavior = new HttpBehavior(); behavior.HelpEnabled = true; } if (binding == null) { binding = new HttpBinding(); } ServiceEndpoint endpoint = host.AddServiceEndpoint(serviceType, binding, string.Empty); endpoint.Behaviors.Add(behavior); }
/// <summary> /// Asserts that an <see cref="HttpRequestMessage"/> sent to the self-hosted HTTP <paramref name="serviceType"/> /// results in a response that is equivalent to the expected <see cref="HttpResponseMessage"/>. /// </summary> /// <param name="serviceType">The service type to use for the self-hosted HTTP service. Should not be <c>null</c>.</param> /// <param name="behavior">The <see cref="HttpBehavior"/> to use with the HTTP service.</param> /// <param name="binding">The <see cref="HttpBinding"/> to use with the HTTP service.</param> /// <param name="request">The <see cref="HttpRequestMessage"/> instance to send to the self-hosted HTTP service. Should not be <c>null</c>.</param> /// <param name="expectedResponse">The expected <see cref="HttpResponseMessage"/>. Should not be <c>null</c>.</param> public static void Execute(Type serviceType, HttpBehavior behavior, HttpBinding binding, HttpRequestMessage request, HttpResponseMessage expectedResponse) { Assert.IsNotNull(serviceType, "The 'serviceType' parameter should not be null."); Assert.IsNotNull(request, "The 'request' parameter should not be null."); Assert.IsNotNull(expectedResponse, "The 'expectedResponse' parameter should not be null."); ServiceHostAssert.Execute((baseAddress) => GetServiceHost(serviceType, behavior, binding, baseAddress), request, expectedResponse); }