public static void ServiceRestart_Throws_CommunicationException() { // This test validates that if the Service were to shut-down, re-start or otherwise die in the // middle of an operation the client will not just hang. It should instead receive a CommunicationException. string restartServiceAddress = ""; ChannelFactory <IWcfService> setupHostFactory = null; IWcfService setupHostServiceProxy = null; ChannelFactory <IWcfRestartService> factory = null; IWcfRestartService serviceProxy = null; BasicHttpBinding binding = new BasicHttpBinding(); // *** Step 1 *** \\ // We need the Service to create and open a ServiceHost and then give us the endpoint address for it. try { setupHostFactory = new ChannelFactory <IWcfService>(binding, new EndpointAddress(Endpoints.HttpBaseAddress_Basic)); setupHostServiceProxy = setupHostFactory.CreateChannel(); restartServiceAddress = setupHostServiceProxy.GetRestartServiceEndpoint(); // *** CLEANUP *** \\ ((ICommunicationObject)setupHostServiceProxy).Close(); setupHostFactory.Close(); } finally { // *** ENSURE CLEANUP *** \\ ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)setupHostServiceProxy, setupHostFactory); } // *** Additional Setup *** \\ // The restartServiceAddress we got from the Service used localhost as the host name. // We need the actual host name for the client call to work. // To make it easier to parse, localhost was replaced with '[HOST]'. // Use Endpoints.HttpBaseAddress_Basic only for the purpose of extracting the Service host name. // Then update 'restartServiceAddress' with it. string hostName = new Uri(Endpoints.HttpBaseAddress_Basic).Host; restartServiceAddress = restartServiceAddress.Replace("[HOST]", hostName); // Get the last portion of the restart service url which is a Guid and convert it back to a Guid // This is needed by the RestartService operation as a Dictionary key to get the ServiceHost string uniqueIdentifier = restartServiceAddress.Substring(restartServiceAddress.LastIndexOf("/") + 1); Guid guid = new Guid(uniqueIdentifier); // *** Step 2 *** \\ // Simple echo call to make sure the newly created endpoint is working. try { factory = new ChannelFactory <IWcfRestartService>(binding, new EndpointAddress(restartServiceAddress)); serviceProxy = factory.CreateChannel(); // *** EXECUTE *** \\ string result = serviceProxy.NonRestartService(guid); Assert.True(result == "Success!", string.Format("Test Case failed, expected the returned string to be: {0}, instead it was: {1}", "Success!", result)); } catch (Exception ex) { string exceptionMessage = ex.Message; string innerExceptionMessage = ex.InnerException?.Message; string testExceptionMessage = $"The ping to validate the newly created endpoint failed.\nThe endpoint pinged was: {restartServiceAddress}\nThe GUID used to extract the ServiceHost from the server side dictionary was: {guid}"; string fullExceptionMessage = $"testExceptionMessage: {testExceptionMessage}\nexceptionMessage: {exceptionMessage}\ninnerExceptionMessage: {innerExceptionMessage}"; Assert.True(false, fullExceptionMessage); } finally { // *** ENSURE CLEANUP *** \\ ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)serviceProxy, factory); } // *** Step 3 *** \\ // The actual part of the test where the host is killed in the middle of the operation. // We expect the test should not hang and should receive a CommunicationException. CommunicationException exception = Assert.Throws <CommunicationException>(() => { factory = new ChannelFactory <IWcfRestartService>(binding, new EndpointAddress(restartServiceAddress)); serviceProxy = factory.CreateChannel(); try { // *** EXECUTE *** \\ serviceProxy.RestartService(guid); } finally { // *** ENSURE CLEANUP *** \\ ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)serviceProxy, factory); } }); }
public static void ServiceRestart_Throws_CommunicationException() { StringBuilder errorBuilder = new StringBuilder(); string restartServiceAddress = ""; BasicHttpBinding binding = new BasicHttpBinding(); try { using (ChannelFactory <IWcfService> factory = new ChannelFactory <IWcfService>(binding, new EndpointAddress(Endpoints.HttpBaseAddress_Basic))) { IWcfService serviceProxy = factory.CreateChannel(); restartServiceAddress = serviceProxy.GetRestartServiceEndpoint(); } } catch (Exception e) { string error = String.Format("Unexpected exception thrown while calling the 'GetRestartServiceEndpoint' operation. {0}", e.ToString()); if (e.InnerException != null) { error += String.Format("\r\nInnerException:\r\n{0}", e.InnerException.ToString()); } errorBuilder.AppendLine(error); } if (errorBuilder.Length == 0) { // Get the Service host name and replace localhost with it UriBuilder builder = new UriBuilder(Endpoints.HttpBaseAddress_Basic); string hostName = builder.Uri.Host; restartServiceAddress = restartServiceAddress.Replace("[HOST]", hostName); //On .NET Native retail, exception message is stripped to include only parameter string expectExceptionMsg = restartServiceAddress; try { using (ChannelFactory <IWcfRestartService> factory = new ChannelFactory <IWcfRestartService>(binding, new EndpointAddress(restartServiceAddress))) { // Get the last portion of the restart service url which is a Guid and convert it back to a Guid // This is needed by the RestartService operation as a Dictionary key to get the ServiceHost string uniqueIdentifier = restartServiceAddress.Substring(restartServiceAddress.LastIndexOf("/") + 1); Guid guid = new Guid(uniqueIdentifier); IWcfRestartService serviceProxy = factory.CreateChannel(); serviceProxy.RestartService(guid); } errorBuilder.AppendLine("Expected CommunicationException exception, but no exception thrown."); } catch (Exception e) { if (e.GetType() == typeof(CommunicationException)) { if (e.Message.Contains(expectExceptionMsg)) { } else { errorBuilder.AppendLine(string.Format("Expected exception message contains: {0}, actual: {1}", expectExceptionMsg, e.Message)); } } else { errorBuilder.AppendLine(string.Format("Expected exception: {0}, actual: {1}/n Exception was: {2}", "CommunicationException", e.GetType(), e.ToString())); } } } Assert.True(errorBuilder.Length == 0, string.Format("Test Scenario: ServiceRestart_Throws_CommunicationException FAILED with the following errors: {0}", errorBuilder)); }