public void WfcServiceHost_Http_WcfClientContext() { // Verify that we can create a service instance and then call it using WcfClientContext. WcfServiceHost host; host = new WcfServiceHost(new TestService()); try { host.AddServiceEndpoint(typeof(ITestService), @"binding=HTTP;uri=http://localhost:8008/Unit/Test.svc;settings=<wsHttpBinding><security mode=""None""/></wsHttpBinding>"); host.ExposeServiceDescription(null, null); host.Start(); using (WcfChannelFactory <ITestService> factory = new WcfChannelFactory <ITestService>(@"binding=HTTP;uri=http://localhost:8008/Unit/Test.svc;settings=<wsHttpBinding><security mode=""None""/></wsHttpBinding>")) using (WcfClientContext <ITestService> client = new WcfClientContext <ITestService>(factory.CreateChannel())) { client.Open(); client.Proxy.Set("Hello World!"); Assert.AreEqual("Hello World!", client.Proxy.Get()); } } finally { host.Stop(); } }
/// <summary> /// Closes the connection if one has been established. /// </summary> public void Close() { lock (syncLock) { if (context != null) { context.Close(); context = null; } remote = null; if (proxyFactory != null) { proxyFactory.Dispose(); proxyFactory = null; } } }
/// <summary> /// Establishes a connection with the remote server manager, if a connection /// has not already been established. /// </summary> /// <exception cref="TimeoutException">Thrown if the remote instance did not respond.</exception> /// <exception cref="CommunicationException">Thrown if communication with the remote instance failed.</exception> /// <exception cref="SecurityException">Thrown if the credentials are invalid or if security policy forbids the operation.</exception> public void Open() { lock (syncLock) { if (proxyFactory != null) { return; } proxyFactory = new WcfChannelFactory <IServerManager>(serverEP); context = null; remote = null; try { context = new WcfClientContext <IServerManager>(proxyFactory.CreateChannel()); context.Open(); remote = context.Proxy; Ping(); } catch { if (context != null) { context.Close(); } remote = null; proxyFactory.Close(); proxyFactory = null; throw; } } }