public void Create_New_Customer() { Uri baseAddress = new Uri("http://localhost:8080/"); CustomServiceHost host = new CustomServiceHost(typeof(CustomerService), baseAddress); using (host) { host.Open(); HttpClient client = new HttpClient(baseAddress); client.Channel = new WebRequestChannel(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "Customers"); request.Content = new StringContent("Id = 7; Name = NewCustomer7"); HttpResponseMessage response = client.Send(request); using (response) { Assert.IsNotNull(response, "The response should not have been null."); Assert.AreEqual(HttpStatusCode.Created, response.StatusCode, "The status code should have been 'Created'."); Assert.IsNotNull(response.Headers.Location, "The location header should not have been null."); Assert.AreEqual(new Uri("http://localhost:8080/Customers?id=7"), response.Headers.Location, "The location header should have beeen 'http://localhost:8080/Customers?id=7'."); Assert.AreEqual("Id = 7; Name = NewCustomer7", response.Content.ReadAsString(), "The response content should have been 'Id = 7; Name = NewCustomer7'."); } // Put server back in original state request = new HttpRequestMessage(HttpMethod.Delete, "Customers?id=7"); client.Send(request); } }
public void Delete_Existing_Customer() { Uri baseAddress = new Uri("http://localhost:8080/"); CustomServiceHost host = new CustomServiceHost(typeof(CustomerService), baseAddress); using (host) { host.Open(); HttpClient client = new HttpClient(baseAddress); client.Channel = new WebRequestChannel(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Delete, "Customers?id=3"); HttpResponseMessage response = client.Send(request); using (response) { Assert.IsNotNull(response, "The response should not have been null."); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "The status code should have been 'OK'."); Assert.AreEqual(string.Empty, response.Content.ReadAsString(), "The response content should have been an empty string."); } // Put server back in the original state request = new HttpRequestMessage(HttpMethod.Post, "Customers"); request.Content = new StringContent("Id = 3; Name = Customer3"); client.Send(request); } }
private void StartUp() { // default configuration EnvId env = IntClient.Target.ClientInfo.ConfigEnv; // derived configuration _serverPort = OtherSettings.GetValue(WFPropName.Port, EnvHelper.SvcPort(env, SvcId.GridSwitch)); _nodeId = OtherSettings.GetValue(WFPropName.NodeId, Guid.NewGuid()); // create router/worker worksteps Routers = GridWorksteps.Create().ToArray(); Workers = GridWorksteps.Create().ToArray(); // start discovery endpoint string endpoint = ServiceHelper.FormatEndpoint(WcfConst.NetTcp, _serverPort); string svcName = EnvHelper.SvcPrefix(SvcId.GridSwitch); _discoServerHost = new CustomServiceHost <IDiscoverV111, DiscoverRecverV111>( Logger, new DiscoverRecverV111(this), endpoint, svcName, typeof(IDiscoverV111).Name, true); IWorkContext context = new WorkContext(Logger, IntClient.Target, HostInstance, ServerInstance); // start gridswitch endpoints - routers before workers foreach (IWorkstep router in Routers) { router.Initialise(context); router.EnableGrid(GridLevel.Router, _nodeId, _serverPort, null); } foreach (IWorkstep worker in Workers) { worker.Initialise(context); worker.EnableGrid(GridLevel.Worker, _nodeId, _serverPort, null); } }
protected override void OnStart(string[] args) { if (_serviceHost != null) { _serviceHost.Close(); } _serviceHost = CustomServiceHost.CreateCustomServiceHost(_serviceHost, typeof(Server.Distributor), "http://localhost:30200/", "net.tcp://localhost:30201/", "Catch", typeof(Server.IDistributor)); _serviceHost.Open(); }
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) { var container = InitializeDIContainer(); // Add this line and try again. AutofacServiceHost.Container = container; var customHost = new CustomServiceHost(serviceType, baseAddresses); customHost.AddDependencyInjectionBehavior(serviceType, container); return(customHost); }
protected override void OnStart(string[] args) { if (_serviceHost != null) { _serviceHost.Close(); } _service = new AudioAnalyzer.Provider.Server.DataProvider(); _service1 = new Server.AudioDetalis(); _serviceHost = CustomServiceHost.CreateCustomServiceHost(_serviceHost, _service.GetType(), "http://localhost:8023/", "net.tcp://localhost:8024/", "Catch", typeof(AudioAnalyzer.Provider.Server.Contract.IDataProvider)); _secondServiceHost = CustomServiceHost.CreateCustomServiceHost(_secondServiceHost, _service1.GetType(), "http://localhost:8025/", "net.tcp://localhost:8026/", "AudioDetails", typeof(AudioAnalyzer.Provider.Server.Contract.IDetalis)); _serviceHost.Open(); _secondServiceHost.Open(); _service.StartApplication(); }
public void Get_Non_Existing_Customer() { Uri baseAddress = new Uri("http://localhost:8080/"); CustomServiceHost host = new CustomServiceHost(typeof(CustomerService), baseAddress); using (host) { host.Open(); HttpClient client = new HttpClient(baseAddress); client.Channel = new WebRequestChannel(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "Customers?id=5"); HttpResponseMessage response = client.Send(request); using (response) { Assert.IsNotNull(response, "The response should not have been null."); Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode, "The status code should have been 'NotFound'."); Assert.AreEqual("There is no customer with id '5'.", response.Content.ReadAsString(), "The response content should have been 'There is no customer with id '5'.'"); } } }
public void Send_Request_With_Unknown_Uri() { Uri baseAddress = new Uri("http://localhost:8080/"); CustomServiceHost host = new CustomServiceHost(typeof(CustomerService), baseAddress); using (host) { host.Open(); HttpClient client = new HttpClient(baseAddress); client.Channel = new WebRequestChannel(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "UnknownUri"); HttpResponseMessage response = client.Send(request); using (response) { Assert.IsNotNull(response, "The response should not have been null."); Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode, "The status code should have been 'NotFound'."); Assert.AreEqual("The uri and/or method is not valid for any customer resource.", response.Content.ReadAsString(), "The response content should have been 'The uri and/or method is not valid for any customer resource.'."); } } }
public void Get_With_Non_Integer_Id() { Uri baseAddress = new Uri("http://localhost:8080/"); CustomServiceHost host = new CustomServiceHost(typeof(CustomerService), baseAddress); using (host) { host.Open(); HttpClient client = new HttpClient(baseAddress); client.Channel = new WebRequestChannel(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "Customers?id=foo"); HttpResponseMessage response = client.Send(request); using (response) { Assert.IsNotNull(response, "The response should not have been null."); Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode, "The status code should have been 'BadRequest'."); Assert.AreEqual("An 'id' with a integer value must be provided in the query string.", response.Content.ReadAsString(), "The response content should have been 'An 'id' with a integer value must be provided in the query string.'"); } } }
public void Get_With_Custom_Header_For_Customer_Names_Only() { Uri baseAddress = new Uri("http://localhost:8080/"); CustomServiceHost host = new CustomServiceHost(typeof(CustomerService), baseAddress); using (host) { host.Open(); HttpClient client = new HttpClient(baseAddress); client.Channel = new WebRequestChannel(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "Customers"); request.Headers.Add("NamesOnly", "Ok"); HttpResponseMessage response = client.Send(request); using (response) { Assert.IsNotNull(response, "The response should not have been null."); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "The status code should have been 'OK'."); Assert.AreEqual("Customer1, Customer2, Customer3", response.Content.ReadAsString(), "The response content should have been 'Customer1, Customer2, Customer3'."); } } }
public void Create_Customer_That_Already_Exists() { Uri baseAddress = new Uri("http://localhost:8080/"); CustomServiceHost host = new CustomServiceHost(typeof(CustomerService), baseAddress); using (host) { host.Open(); HttpClient client = new HttpClient(baseAddress); client.Channel = new WebRequestChannel(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "Customers?id=2"); request.Content = new StringContent("Id = 2; Name = AlreadyCustomer2"); HttpResponseMessage response = client.Send(request); using (response) { Assert.IsNotNull(response, "The response should not have been null."); Assert.AreEqual(HttpStatusCode.Conflict, response.StatusCode, "The status code should have been 'Conflict'."); Assert.AreEqual("There already a customer with id '2'.", response.Content.ReadAsString(), "The response content should have been 'There already a customer with id '2'.'"); } } }
public void Get_All_Existing_Customer() { Uri baseAddress = new Uri("http://localhost:8080/"); CustomServiceHost host = new CustomServiceHost(typeof(CustomerService), baseAddress); using (host) { host.Open(); HttpClient client = new HttpClient(baseAddress); client.Channel = new WebRequestChannel(); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "Customers"); HttpResponseMessage response = client.Send(request); using (response) { Assert.IsNotNull(response, "The response should not have been null."); Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "The status code should have been 'OK'."); string[] responseContent = response.Content.ReadAsString().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); Assert.AreEqual("Id = 1; Name = Customer1", responseContent[0], "The response content should have been 'Id = 1; Name = Customer1'."); Assert.AreEqual("Id = 2; Name = Customer2", responseContent[1], "The response content should have been 'Id = 2; Name = Customer2'."); Assert.AreEqual("Id = 3; Name = Customer3", responseContent[2], "The response content should have been 'Id = 3; Name = Customer3'."); } } }
public void ApplyConfigurationNoDescription() { CustomServiceHost customHost = new CustomServiceHost(); customHost.ApplyConfiguration(); }
protected override void OnStart() { // restore un-expired connections Logger.LogDebug("Restoring connections..."); DateTimeOffset dtNow = DateTimeOffset.Now; List <CommonItem> connItems = _cacheEngine.GetCacheItems( null, ItemKind.Local, typeof(ClientConnectionState).FullName, null, 0, dtNow, true, false); foreach (CommonItem item in connItems) { try { var oldConn = XmlSerializerHelper.DeserializeFromString <ClientConnectionState>( CompressionHelper.DecompressToString(item.YData)); IConnection connection = null; if (oldConn.Contract == typeof(ITransferV341).FullName) { var clientId = new Guid(oldConn.SourceId); connection = new ConnectionV34( Logger, _cacheEngine, _serverCfg, clientId, oldConn.ReplyAddress, NodeType.Client); connection.ExtendExpiry(); _connectionIndex.Set(clientId, connection); } if (connection != null) { Logger.LogDebug("Restored connection:"); Logger.LogDebug(" Client Id. : {0}", connection.ClientId); Logger.LogDebug(" Client Addr: {0}", connection.ReplyAddress); } else { Logger.LogDebug("Ignoring unsupported connection: '{0}'", oldConn.ReplyAddress); } } catch (Exception e) { // failed, however the show must go on Logger.Log(e); } } // restore subscriptions Logger.LogDebug("Restoring subscriptions..."); List <CommonItem> subsItems = _cacheEngine.GetCacheItems( null, ItemKind.Local, typeof(ClientSubscriptionState).FullName, null, 0, dtNow, true, false); foreach (CommonItem item in subsItems) { try { var oldSubscription = XmlSerializerHelper.DeserializeFromString <ClientSubscriptionState>( CompressionHelper.DecompressToString(item.YData)); var subscription = new ClientSubscription(oldSubscription); var clientId = new Guid(oldSubscription.ConnectionId); IConnection connection = GetValidConnection(clientId); if (connection != null) { _cacheEngine.RestoreSubscription(subscription); Logger.LogDebug("Restored subscription:"); Logger.LogDebug(" Client Id: {0}", connection.ClientId); Logger.LogDebug(" Address : {0}", connection.ReplyAddress); Logger.LogDebug(" Subs. Id : {0}", subscription.SubscriptionId); Logger.LogDebug(" AppScopes: {0}", (subscription.AppScopes == null) ? "*" : String.Join(",", subscription.AppScopes)); Logger.LogDebug(" ItemKind : {0}", (subscription.ItemKind == ItemKind.Undefined) ? "(any)" : subscription.ItemKind.ToString()); Logger.LogDebug(" DataType : {0}", subscription.DataTypeName ?? "(any)"); Logger.LogDebug(" Query : {0}", subscription.Expression.DisplayString()); Logger.LogDebug(" MinimumUSN > : {0}", subscription.MinimumUSN); Logger.LogDebug(" Excl.Deleted?: {0}", (subscription.ExcludeDeleted)); Logger.LogDebug(" Excl.DataBody: {0}", subscription.ExcludeDataBody); } else { _cacheEngine.DeleteSubscriptionState(subscription.SubscriptionId); Logger.LogDebug("Ignoring expired subscription id: {0}", oldSubscription.SubscriptionId); } } catch (Exception e) { // failed, however the show must go on Logger.Log(e); } } string svcName = EnvHelper.SvcPrefix(SvcId.CoreServer); // discovery service _discoverV111ServerHost = new CustomServiceHost <IDiscoverV111, DiscoverRecverV111>( Logger, new DiscoverRecverV111(this), _serverCfg.V31DiscoEndpoints, svcName, typeof(IDiscoverV111).Name, true); // V3.4 services _sessCtrlV131ServerHost = new CustomServiceHost <ISessCtrlV131, SessCtrlRecverV131>( Logger, new SessCtrlRecverV131(this), _serverCfg.V31DiscoEndpoints, svcName, typeof(ISessCtrlV131).Name, true); _transferV341ServerHost = new CustomServiceHost <ITransferV341, TransferRecverV341>( Logger, new TransferRecverV341(this), _serverCfg.V31AsyncEndpoints, svcName, typeof(ITransferV341).Name, true); // start housekeeping timer _housekeepTimer = new Timer(DispatchHousekeepTimeout, null, ServerCfg.CommsHousekeepInterval, ServerCfg.CommsHousekeepInterval); }
protected override void OnServerStarted() { // default configuration _activeProviders = new[] { MDSProviderId.Bloomberg }; // custom configuration string[] enabledProviders = OtherSettings.GetArray <string>(MdsPropName.EnabledProviders); if (enabledProviders != null && enabledProviders.Length > 0) { _activeProviders = enabledProviders.Select(providerName => EnumHelper.Parse <MDSProviderId>(providerName, true)).ToArray(); } // derived configuration EnvId envId = IntClient.Target.ClientInfo.ConfigEnv; string envName = EnvHelper.EnvName(envId); var port = OtherSettings.GetValue(MdsPropName.Port, EnvHelper.SvcPort(envId, SvcId.MarketData)); // service endpoints string transEndpoints = ServiceHelper.FormatEndpoint(WcfConst.NetTcp, port); string discoEndpoints = ServiceHelper.FormatEndpoint(WcfConst.NetTcp, port); transEndpoints = OtherSettings.GetValue(MdsPropName.Endpoints, transEndpoints); // add default port to endpoints if required var tempEndpoints = new List <string>(); foreach (string ep in transEndpoints.Split(';')) { var epParts = ep.Split(':'); var scheme = epParts[0]; var tport = port; if (epParts.Length > 1) { tport = Int32.Parse(epParts[1]); } tempEndpoints.Add($"{scheme}:{tport}"); } transEndpoints = String.Join(";", tempEndpoints.ToArray()); // get user identity and full name WindowsIdentity winIdent = WindowsIdentity.GetCurrent(); UserPrincipal principal = null; try { var principalContext = new PrincipalContext(ContextType.Domain); principal = UserPrincipal.FindByIdentity(principalContext, IdentityType.SamAccountName, winIdent.Name); } catch (PrincipalException principalException) { // swallow - can occur on machines not connected to domain controller Logger.LogWarning("UserPrincipal.FindByIdentity failed: {0}: {1}", principalException.GetType().Name, principalException.Message); } string userFullName = null; if (principal != null) { userFullName = principal.GivenName + " " + principal.Surname; } _serverCfg = new ServerCfg( new ModuleInfo(envName, Guid.NewGuid(), winIdent.Name, userFullName, null, null), transEndpoints, discoEndpoints); foreach (MDSProviderId providerId in _activeProviders) { _providers[(int)providerId] = new MarketDataRealtimeClient(LoggerRef, MainThreadQueue, IntClient.Target, providerId); } string svcName = EnvHelper.SvcPrefix(SvcId.MarketData); // V2.2 _mrktDataV221ServerHost = new CustomServiceHost <IMrktDataV221, MrktDataRecverV221>( Logger, new MrktDataRecverV221(this), _serverCfg.TransEndpoints, svcName, typeof(IMrktDataV221).Name, true); _sessCtrlV131ServerHost = new CustomServiceHost <ISessCtrlV131, SessCtrlRecverV131>( Logger, new SessCtrlRecverV131(this), _serverCfg.DiscoEndpoints, svcName, typeof(ISessCtrlV131).Name, true); _discoServerHostV111 = new CustomServiceHost <IDiscoverV111, DiscoverRecverV111>( Logger, new DiscoverRecverV111(this), _serverCfg.DiscoEndpoints, svcName, typeof(IDiscoverV111).Name, true); }
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) { var host = new CustomServiceHost(serviceType, baseAddresses, this.container); return host; }
public TestServer(ILogger logger, int port) { _ServerHost = new CustomServiceHost <ITestService, TestServer>( logger, this, ServiceHelper.FormatEndpoint(WcfConst.NetTcp, port), "Test", typeof(ITestService).Name, true); }