public static void ClientBaseOfT_Sync_RoundTrip_Call_Using_NetTcpTransport() { // This test verifies ClientBase<T> can be used to create a proxy and invoke an operation over Tcp // (request reply over Tcp) // This test verifies ClientBase<T> can be used to create a proxy and invoke an operation over Http CustomBinding binding = new CustomBinding( new TextMessageEncodingBindingElement(), new TcpTransportBindingElement()); MyClientBase client = new MyClientBase(binding, new EndpointAddress(Endpoints.Tcp_CustomBinding_NoSecurity_Text_Address)); IWcfServiceGenerated serviceProxy = client.ChannelFactory.CreateChannel(); try { string result = serviceProxy.Echo("Hello"); Assert.Equal("Hello", result); } finally { if (client != null && client.State != CommunicationState.Closed) { client.Abort(); } } }
public static void ClientBaseOfT_Sync_RoundTrip_Call_Using_NetTcpTransport() { // This test verifies ClientBase<T> can be used to create a proxy and invoke an operation over Tcp // (request reply over Tcp) MyClientBase client = null; IWcfServiceGenerated serviceProxy = null; try { // *** SETUP *** \\ CustomBinding binding = new CustomBinding(new TextMessageEncodingBindingElement(), new TcpTransportBindingElement()); client = new MyClientBase(binding, new EndpointAddress(Endpoints.Tcp_CustomBinding_NoSecurity_Text_Address)); serviceProxy = client.ChannelFactory.CreateChannel(); // *** EXECUTE *** \\ string result = serviceProxy.Echo("Hello"); // *** VALIDATE *** \\ Assert.Equal("Hello", result); // *** CLEANUP *** \\ ((ICommunicationObject)client).Close(); ((ICommunicationObject)serviceProxy).Close(); } finally { // *** ENSURE CLEANUP *** \\ ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)serviceProxy, (ICommunicationObject)client); } }
private static void ServiceContract_TypedProxy_AsyncTask_Call(Binding binding, string endpoint, string testName) { // This test verifies a typed proxy can call a service operation asynchronously using Task<string> StringBuilder errorBuilder = new StringBuilder(); try { ChannelFactory <IWcfServiceGenerated> factory = new ChannelFactory <IWcfServiceGenerated>(binding, new EndpointAddress(endpoint)); IWcfServiceGenerated serviceProxy = factory.CreateChannel(); Task <string> task = serviceProxy.EchoAsync("Hello"); string result = task.Result; if (!string.Equals(result, "Hello")) { errorBuilder.AppendLine(String.Format("Expected response from Service: {0} Actual was: {1}", "Hello", result)); } factory.Close(); } catch (Exception ex) { errorBuilder.AppendLine(String.Format("Unexpected exception was caught: {0}", ex.ToString())); } Assert.True(errorBuilder.Length == 0, string.Format("Test Scenario: TypedProxyAsyncTaskCall FAILED with the following errors: {0}", errorBuilder)); }
public static void ServiceContract_TypedProxy_Synchronous_Call() { // This test verifies a typed proxy can call a service operation synchronously StringBuilder errorBuilder = new StringBuilder(); try { CustomBinding customBinding = new CustomBinding(); customBinding.Elements.Add(new TextMessageEncodingBindingElement()); customBinding.Elements.Add(new HttpTransportBindingElement()); // Note the service interface used. It was manually generated with svcutil. ChannelFactory <IWcfServiceGenerated> factory = new ChannelFactory <IWcfServiceGenerated>(customBinding, new EndpointAddress(Endpoints.DefaultCustomHttp_Address)); IWcfServiceGenerated serviceProxy = factory.CreateChannel(); string result = serviceProxy.Echo("Hello"); if (!string.Equals(result, "Hello")) { errorBuilder.AppendLine(String.Format("Expected response from Service: {0} Actual was: {1}", "Hello", result)); } factory.Close(); } catch (Exception ex) { errorBuilder.AppendLine(String.Format("Unexpected exception was caught: {0}", ex.ToString())); } Assert.True(errorBuilder.Length == 0, string.Format("Test Scenario: TypedProxySynchronousCall FAILED with the following errors: {0}", errorBuilder)); }
// Create the channel factory using BasicHttpBinding and open the channel using a user generated interface public static void CreateChannel_Of_Typed_Proxy_Using_BasicHttpBinding() { ChannelFactory <IWcfServiceGenerated> factory = null; try { BasicHttpBinding binding = new BasicHttpBinding(); // Create the channel factory factory = new ChannelFactory <IWcfServiceGenerated>(binding, new EndpointAddress(Constants.BaseAddress.HttpBaseAddress)); factory.Open(); // Create the channel. IWcfServiceGenerated channel = factory.CreateChannel(); Assert.IsAssignableFrom <IWcfServiceGenerated>(channel); } finally { if (factory != null) { factory.Close(); } } }
public static void ServiceContract_Call_Operation_With_MessageParameterAttribute() { // This test verifies the scenario where MessageParameter attribute is used in the contract StringBuilder errorBuilder = new StringBuilder(); ChannelFactory <IWcfServiceGenerated> factory = null; IWcfServiceGenerated serviceProxy = null; try { // *** SETUP *** \\ CustomBinding customBinding = new CustomBinding(); customBinding.Elements.Add(new TextMessageEncodingBindingElement()); customBinding.Elements.Add(new HttpTransportBindingElement()); // Note the service interface used. It was manually generated with svcutil. factory = new ChannelFactory <IWcfServiceGenerated>(customBinding, new EndpointAddress(Endpoints.DefaultCustomHttp_Address)); serviceProxy = factory.CreateChannel(); // *** EXECUTE *** \\ string echoString = "you"; string result = serviceProxy.EchoMessageParameter(echoString); // *** VALIDATE *** \\ Assert.True(string.Equals(result, "Hello " + echoString), String.Format("Expected response from Service: {0} Actual was: {1}", "Hello " + echoString, result)); // *** CLEANUP *** \\ ((ICommunicationObject)serviceProxy).Close(); factory.Close(); } finally { // *** ENSURE CLEANUP *** \\ ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)serviceProxy, factory); } }
public static void ClientBaseOfT_Call() { // This test verifies ClientBase<T> can be used to create a proxy and invoke an operation StringBuilder errorBuilder = new StringBuilder(); try { CustomBinding customBinding = new CustomBinding(); customBinding.Elements.Add(new TextMessageEncodingBindingElement()); customBinding.Elements.Add(new HttpTransportBindingElement()); MyClientBase client = new MyClientBase(customBinding, new EndpointAddress(BaseAddress.HttpBaseAddress)); IWcfServiceGenerated serviceProxy = client.ChannelFactory.CreateChannel(); string result = serviceProxy.Echo("Hello"); if (!string.Equals(result, "Hello")) { errorBuilder.AppendLine(String.Format("Expected response from Service: {0} Actual was: {1}", "Hello", result)); } } catch (Exception ex) { errorBuilder.AppendLine(String.Format("Unexpected exception was caught: {0}", ex.ToString())); } Assert.True(errorBuilder.Length == 0, string.Format("Test Scenario: ClientBaseOfTCall FAILED with the following errors: {0}", errorBuilder)); }
private static void ServiceContract_TypedProxy_AsyncTask_Call_TestImpl(Binding binding, string endpoint, string testName) { // This test verifies a typed proxy can call a service operation asynchronously using Task<string> ChannelFactory <IWcfServiceGenerated> factory = null; EndpointAddress endpointAddress = null; IWcfServiceGenerated serviceProxy = null; string result = null; try { // *** SETUP *** \\ endpointAddress = new EndpointAddress(endpoint); factory = new ChannelFactory <IWcfServiceGenerated>(binding, endpointAddress); serviceProxy = factory.CreateChannel(); // *** EXECUTE *** \\ Task <string> task = serviceProxy.EchoAsync("Hello"); result = task.Result; // *** VALIDATE *** \\ Assert.True(String.Equals(result, "Hello"), String.Format("Expected response from Service: {0} Actual was: {1}", "Hello", result)); // *** CLEANUP *** \\ factory.Close(); ((ICommunicationObject)serviceProxy).Close(); } finally { // *** ENSURE CLEANUP *** \\ ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)serviceProxy, factory); } }
public static void ClientBaseOfT_Sync_RoundTrip_Check_CommunicationState() { CustomBinding customBinding = new CustomBinding(); customBinding.Elements.Add(new TextMessageEncodingBindingElement()); customBinding.Elements.Add(new HttpTransportBindingElement()); MyClientBase client = new MyClientBase(customBinding, new EndpointAddress(Endpoints.HttpSoap12_Address)); Assert.Equal(CommunicationState.Created, client.State); IWcfServiceGenerated serviceProxy = client.ChannelFactory.CreateChannel(); Assert.Equal(CommunicationState.Opened, client.State); try { string result = serviceProxy.Echo("Hello"); Assert.Equal(CommunicationState.Opened, client.State); ((ICommunicationObject)client).Close(); Assert.Equal(CommunicationState.Closed, client.State); } finally { // normally we'd also check for if (client != null && client.State != CommuncationState.Closed), // but this is a test and it'd be good to have the Abort happen and the channel is still Closed if (client != null) { client.Abort(); Assert.Equal(CommunicationState.Closed, client.State); } } }
// Create the channel factory using BasicHttpBinding and open the channel using a user generated interface public static void CreateChannel_Of_Typed_Proxy_Using_BasicHttpBinding() { ChannelFactory <IWcfServiceGenerated> factory = null; try { BasicHttpBinding binding = new BasicHttpBinding(); // Create the channel factory factory = new ChannelFactory <IWcfServiceGenerated>(binding, new EndpointAddress(FakeAddress.HttpAddress)); factory.Open(); // Create the channel. IWcfServiceGenerated channel = factory.CreateChannel(); Assert.True(typeof(IWcfServiceGenerated).GetTypeInfo().IsAssignableFrom(channel.GetType().GetTypeInfo()), String.Format("Channel type '{0}' was not assignable to '{1}'", channel.GetType(), typeof(IRequestChannel))); } finally { if (factory != null) { factory.Close(); } } }
public static void ClientMessageInspector_Verify_Invoke() { // This test verifies ClientMessageInspector can be added to the client endpoint behaviors // and this is it called properly when a message is sent. StringBuilder errorBuilder = new StringBuilder(); try { CustomBinding customBinding = new CustomBinding(); customBinding.Elements.Add(new TextMessageEncodingBindingElement()); customBinding.Elements.Add(new HttpTransportBindingElement()); MyClientBase client = new MyClientBase(customBinding, new EndpointAddress(BaseAddress.HttpBaseAddress)); // Add the ClientMessageInspector and give it an instance where it can record what happens when it is called. ClientMessageInspectorData data = new ClientMessageInspectorData(); client.Endpoint.EndpointBehaviors.Add(new ClientMessageInspectorBehavior(data)); IWcfServiceGenerated serviceProxy = client.ChannelFactory.CreateChannel(); // This proxy call should invoke the client message inspector string result = serviceProxy.Echo("Hello"); if (!string.Equals(result, "Hello")) { errorBuilder.AppendLine(String.Format("Expected response from Service: {0} Actual was: {1}", "Hello", result)); } if (!data.BeforeSendRequestCalled) { errorBuilder.AppendLine(String.Format("Did not call BeforeSendRequest")); } if (data.Request == null) { errorBuilder.AppendLine(String.Format("Did not call pass Request to BeforeSendRequest")); } if (data.Channel == null) { errorBuilder.AppendLine(String.Format("Did not call pass Channel to BeforeSendRequest")); } if (!data.AfterReceiveReplyCalled) { errorBuilder.AppendLine(String.Format("Did not call AfterReceiveReplyCalled")); } if (data.Reply == null) { errorBuilder.AppendLine(String.Format("Did not call pass Reply to AfterReceiveReplyCalled")); } } catch (Exception ex) { errorBuilder.AppendLine(String.Format("Unexpected exception was caught: {0}", ex.ToString())); } Assert.True(errorBuilder.Length == 0, string.Format("Test Scenario: ClientMessageInspectorScenario FAILED with the following errors: {0}", errorBuilder)); }
public static void ClientBaseOfT_Async_Open_Close_Events_Fire() { MyClientBase client = null; List <string> eventsCalled = new List <string>(4); List <string> proxyEventsCalled = new List <string>(4); IWcfServiceGenerated proxy = null; try { // *** SETUP *** \\ CustomBinding customBinding = new CustomBinding(); customBinding.Elements.Add(new TextMessageEncodingBindingElement()); customBinding.Elements.Add(new HttpTransportBindingElement()); client = new MyClientBase(customBinding, new EndpointAddress(Endpoints.HttpSoap12_Address)); // Listen to all events on the ClientBase and on the generated proxy ClientBaseTestHelpers.RegisterForEvents(client, eventsCalled); proxy = client.Proxy; ClientBaseTestHelpers.RegisterForEvents((ICommunicationObject)proxy, proxyEventsCalled); // *** EXECUTE *** \\ Task <string> task = proxy.EchoAsync("Hello"); task.GetAwaiter().GetResult(); IAsyncResult ar = ((ICommunicationObject)client).BeginClose(null, null); ((ICommunicationObject)client).EndClose(ar); // *** VALIDATE *** \\ // We expect both the ClientBase and the generated proxy to have fired all the open/close events string expected = "Opening,Opened,Closing,Closed"; string actual = String.Join(",", eventsCalled); Assert.True(String.Equals(expected, actual), String.Format("Expected client to receive events '{0}' but actual was '{1}'", expected, actual)); actual = String.Join(",", proxyEventsCalled); Assert.True(String.Equals(expected, actual), String.Format("Expected proxy to receive events '{0}' but actual was '{1}'", expected, actual)); // *** CLEANUP *** \\ } finally { // *** ENSURE CLEANUP *** \\ ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)proxy, (ICommunicationObject)client); } }
public static void ServiceContract_TypedProxy_Task_Call_WithSyncContext_ContinuesOnSameThread() { // This test verifies a task based call to a service operation continues on the same thread StringBuilder errorBuilder = new StringBuilder(); try { CustomBinding customBinding = new CustomBinding(); customBinding.Elements.Add(new TextMessageEncodingBindingElement()); customBinding.Elements.Add(new HttpTransportBindingElement()); ChannelFactory <IWcfServiceGenerated> factory = new ChannelFactory <IWcfServiceGenerated>(customBinding, new EndpointAddress(Endpoints.DefaultCustomHttp_Address)); IWcfServiceGenerated serviceProxy = factory.CreateChannel(); string result = String.Empty; bool success = Task.Run(() => { SingleThreadSynchronizationContext.Run(async delegate { int startThread = Environment.CurrentManagedThreadId; result = await serviceProxy.EchoAsync("Hello"); if (startThread != Environment.CurrentManagedThreadId) { errorBuilder.AppendLine(String.Format("Expected continuation to happen on thread {0} but actually continued on thread {1}", startThread, Environment.CurrentManagedThreadId)); } }); }).Wait(ScenarioTestHelpers.TestTimeout); if (!success) { errorBuilder.AppendLine(String.Format("Test didn't complete within the expected time")); } if (!string.Equals(result, "Hello")) { errorBuilder.AppendLine(String.Format("Expected response from Service: {0} Actual was: {1}", "Hello", result)); } factory.Close(); } catch (Exception ex) { errorBuilder.AppendLine(String.Format("Unexpected exception was caught: {0}", ex.ToString())); } Assert.True(errorBuilder.Length == 0, string.Format("Test Scenario: TaskCallWithSynchContextContinuesOnSameThread FAILED with the following errors: {0}", errorBuilder)); }
public static void ClientBaseOfT_Dispose_Closes_Factory_And_Proxy() { MyClientBase client = null; IWcfServiceGenerated serviceProxy = null; ChannelFactory <IWcfServiceGenerated> factory = null; try { // *** SETUP *** \\ CustomBinding customBinding = new CustomBinding(); customBinding.Elements.Add(new TextMessageEncodingBindingElement()); customBinding.Elements.Add(new HttpTransportBindingElement()); string endpoint = Endpoints.HttpSoap12_Address; client = new MyClientBase(customBinding, new EndpointAddress(endpoint)); factory = client.ChannelFactory; // *** EXECUTE *** \\ // Explicitly open the ClientBase to follow general WCF guidelines ((ICommunicationObject)client).Open(); // Use the internal proxy generated by ClientBase to most resemble how svcutil-generated code works. // Customers cannot normally access this protected member explicitly, but the generated code uses it. serviceProxy = client.Proxy; // *** EXECUTE *** \\ // ClientBase is IDisposable, which should close the client, factory and proxy ((IDisposable)client).Dispose(); // *** VALIDATE *** \\ // Closing the ClientBase closes the internal channel and factory Assert.True(CommunicationState.Closed == client.State, String.Format("Expected client state to be Closed but actual was '{0}'", client.State)); // Closing the ClientBase also closes the internal channel Assert.True(CommunicationState.Closed == ((ICommunicationObject)serviceProxy).State, String.Format("Expected proxy state to be Closed but actual was '{0}'", ((ICommunicationObject)serviceProxy).State)); // Closing the ClientBase also closes the channel factory Assert.True(CommunicationState.Closed == factory.State, String.Format("Expected channel factory state to be Closed but actual was '{0}'", factory.State)); } finally { // *** ENSURE CLEANUP *** \\ ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)serviceProxy, client, factory); } }
public static void ClientBaseOfT_Sync_Removed_Open_Close_Events_Do_Not_Fire() { MyClientBase client = null; List <string> eventsCalled = new List <string>(4); List <string> proxyEventsCalled = new List <string>(4); IWcfServiceGenerated proxy = null; try { // *** SETUP *** \\ CustomBinding customBinding = new CustomBinding(); customBinding.Elements.Add(new TextMessageEncodingBindingElement()); customBinding.Elements.Add(new HttpTransportBindingElement()); client = new MyClientBase(customBinding, new EndpointAddress(Endpoints.HttpSoap12_Address)); // Both Add and Remove event handlers ClientBaseTestHelpers.RegisterForEvents(client, eventsCalled, deregister: true); proxy = client.Proxy; ClientBaseTestHelpers.RegisterForEvents((ICommunicationObject)proxy, proxyEventsCalled, deregister: true); // *** EXECUTE *** \\ proxy.Echo("Hello"); ((ICommunicationObject)client).Close(); // *** VALIDATE *** \\ // We expect both the ClientBase and the generated proxy to have NOT fired all the open/close events string actual = String.Join(",", eventsCalled); Assert.True(eventsCalled.Count == 0, String.Format("Expected client NOT to receive events but actual was '{0}'", actual)); actual = String.Join(",", proxyEventsCalled); Assert.True(proxyEventsCalled.Count == 0, String.Format("Expected proxy NOT to receive events but actual was '{0}'", actual)); // *** CLEANUP *** \\ } finally { // *** ENSURE CLEANUP *** \\ ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)proxy, (ICommunicationObject)client); } }
public static void ServiceContract_TypedProxy_Task_Call_WithSyncContext_ContinuesOnSameThread() { // This test verifies a task based call to a service operation continues on the same thread CustomBinding customBinding = null; ChannelFactory <IWcfServiceGenerated> factory = null; EndpointAddress endpointAddress = null; IWcfServiceGenerated serviceProxy = null; string result = null; try { // *** SETUP *** \\ customBinding = new CustomBinding(); customBinding.Elements.Add(new TextMessageEncodingBindingElement()); customBinding.Elements.Add(new HttpTransportBindingElement()); endpointAddress = new EndpointAddress(Endpoints.DefaultCustomHttp_Address); factory = new ChannelFactory <IWcfServiceGenerated>(customBinding, endpointAddress); serviceProxy = factory.CreateChannel(); // *** EXECUTE *** \\ bool success = Task.Run(() => { SingleThreadSynchronizationContext.Run(async delegate { int startThread = Environment.CurrentManagedThreadId; result = await serviceProxy.EchoAsync("Hello"); Assert.True(startThread == Environment.CurrentManagedThreadId, String.Format("Expected continuation to happen on thread {0} but actually continued on thread {1}", startThread, Environment.CurrentManagedThreadId)); }); }).Wait(ScenarioTestHelpers.TestTimeout); // *** VALIDATE *** \\ Assert.True(success, String.Format("Test didn't complete within the expected time")); Assert.True(String.Equals(result, "Hello"), String.Format("Expected response from Service: {0} Actual was: {1}", "Hello", result)); // *** CLEANUP *** \\ factory.Close(); ((ICommunicationObject)serviceProxy).Close(); } finally { // *** ENSURE CLEANUP *** \\ ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)serviceProxy, factory); } }
public static void ClientMessageInspector_Verify_Invoke() { // This test verifies ClientMessageInspector can be added to the client endpoint behaviors // and this is it called properly when a message is sent. MyClientBase client = null; IWcfServiceGenerated serviceProxy = null; try { // *** SETUP *** \\ CustomBinding customBinding = new CustomBinding(); customBinding.Elements.Add(new TextMessageEncodingBindingElement()); customBinding.Elements.Add(new HttpTransportBindingElement()); client = new MyClientBase(customBinding, new EndpointAddress(Endpoints.DefaultCustomHttp_Address)); // Add the ClientMessageInspector and give it an instance where it can record what happens when it is called. ClientMessageInspectorData data = new ClientMessageInspectorData(); client.Endpoint.EndpointBehaviors.Add(new ClientMessageInspectorBehavior(data)); serviceProxy = client.ChannelFactory.CreateChannel(); // *** EXECUTE *** \\ // This proxy call should invoke the client message inspector string result = serviceProxy.Echo("Hello"); // *** VALIDATE *** \\ Assert.Equal("Hello", result); Assert.True(data.BeforeSendRequestCalled, "BeforeSendRequest should have been called"); Assert.True(data.Request != null, "Did not call pass Request to BeforeSendRequest"); Assert.True(data.Channel != null, "Did not call pass Channel to BeforeSendRequest"); Assert.True(data.AfterReceiveReplyCalled, "AfterReceiveReplyCalled should have been called"); Assert.True(data.Reply != null, "Did not call pass Reply to AfterReceiveReplyCalled"); // *** CLEANUP *** \\ ((ICommunicationObject)client).Close(); ((ICommunicationObject)serviceProxy).Close(); } finally { // *** ENSURE CLEANUP *** \\ ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)serviceProxy, (ICommunicationObject)client); } }
public static void ServiceContract_TypedProxy_Synchronous_Call() { // This test verifies a typed proxy can call a service operation synchronously CustomBinding customBinding = null; ChannelFactory <IWcfServiceGenerated> factory = null; EndpointAddress endpointAddress = null; IWcfServiceGenerated serviceProxy = null; string result = null; try { // *** SETUP *** \\ customBinding = new CustomBinding(); customBinding.Elements.Add(new TextMessageEncodingBindingElement()); customBinding.Elements.Add(new HttpTransportBindingElement()); endpointAddress = new EndpointAddress(Endpoints.DefaultCustomHttp_Address); // Note the service interface used. It was manually generated with svcutil. factory = new ChannelFactory <IWcfServiceGenerated>(customBinding, endpointAddress); serviceProxy = factory.CreateChannel(); // *** EXECUTE *** \\ result = serviceProxy.Echo("Hello"); // *** VALIDATE *** \\ Assert.True(String.Equals(result, "Hello"), String.Format("Expected response from Service: {0} Actual was: {1}", "Hello", result)); // *** CLEANUP *** \\ factory.Close(); ((ICommunicationObject)serviceProxy).Close(); } finally { // *** ENSURE CLEANUP *** \\ ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)serviceProxy, factory); } }
public static void ClientBaseOfT_Async_Open_Close_TimeSpan_Factory_And_Proxy_CommunicationState() { MyClientBase client = null; IWcfServiceGenerated serviceProxy = null; ChannelFactory <IWcfServiceGenerated> factory = null; TimeSpan timeout = ScenarioTestHelpers.TestTimeout; try { // *** SETUP *** \\ CustomBinding customBinding = new CustomBinding(); customBinding.Elements.Add(new TextMessageEncodingBindingElement()); customBinding.Elements.Add(new HttpTransportBindingElement()); string endpoint = Endpoints.HttpSoap12_Address; client = new MyClientBase(customBinding, new EndpointAddress(endpoint)); factory = client.ChannelFactory; // *** VALIDATE *** \\ Assert.True(CommunicationState.Created == client.State, String.Format("Expected client state to be Created but actual was '{0}'", client.State)); Assert.True(CommunicationState.Created == factory.State, String.Format("Expected channel factory state to be Created but actual was '{0}'", factory.State)); // Note: both the full framework and this NET Core version open the channel factory // when asking for the internal channel. Attempting to Open the ClientBase // after obtaining the internal channel with throw InvalidOperationException attempting // to reopen the channel factory. Customers don't encounter this situation in normal use // because access to the internal channel is protected and cannot be acquired. So we // defer asking for the internal channel in this test to allow the Open() to follow the // same code path as customer code. // *** EXECUTE *** \\ // Explicitly async open the ClientBase to follow general WCF guidelines IAsyncResult ar = ((ICommunicationObject)client).BeginOpen(timeout, null, null); ((ICommunicationObject)client).EndOpen(ar); // Use the internal proxy generated by ClientBase to most resemble how svcutil-generated code works. // This test defers asking for it until the ClientBase is open to avoid the issue described above. serviceProxy = client.Proxy; Assert.True(CommunicationState.Opened == client.State, String.Format("Expected client state to be Opened but actual was '{0}'", client.State)); Assert.True(CommunicationState.Opened == factory.State, String.Format("Expected channel factory state to be Opened but actual was '{0}'", factory.State)); Assert.True(CommunicationState.Opened == ((ICommunicationObject)serviceProxy).State, String.Format("Expected proxy state to be Opened but actual was '{0}'", ((ICommunicationObject)serviceProxy).State)); // *** EXECUTE *** \\ // Explicitly close the ClientBase to follow general WCF guidelines ar = ((ICommunicationObject)client).BeginClose(timeout, null, null); ((ICommunicationObject)client).EndClose(ar); // *** VALIDATE *** \\ // Closing the ClientBase closes the internal channel and factory Assert.True(CommunicationState.Closed == client.State, String.Format("Expected client state to be Closed but actual was '{0}'", client.State)); // Closing the ClientBase also closes the internal channel Assert.True(CommunicationState.Closed == ((ICommunicationObject)serviceProxy).State, String.Format("Expected proxy state to be Closed but actual was '{0}'", ((ICommunicationObject)serviceProxy).State)); // Closing the ClientBase also closes the channel factory Assert.True(CommunicationState.Closed == factory.State, String.Format("Expected channel factory state to be Closed but actual was '{0}'", factory.State)); } finally { // *** ENSURE CLEANUP *** \\ ScenarioTestHelpers.CloseCommunicationObjects((ICommunicationObject)serviceProxy, client, factory); } }