public void DuplexEcho() { string testString = new string('a', 3000); IWebHost host = ServiceHelper.CreateWebHostBuilder <Startup>(_output).Build(); using (host) { System.ServiceModel.ChannelFactory <ServiceContract.IDuplexTestService> factory = null; ServiceContract.IDuplexTestService channel = null; host.Start(); try { System.ServiceModel.NetTcpBinding binding = ClientHelper.GetBufferedModeBinding(); var callback = new ServiceContract.DuplexTestCallback(); factory = new System.ServiceModel.DuplexChannelFactory <ServiceContract.IDuplexTestService>( new System.ServiceModel.InstanceContext(callback), binding, new System.ServiceModel.EndpointAddress(host.GetNetTcpAddressInUse() + Startup.DuplexRelativeAddress)); channel = factory.CreateChannel(); ((IChannel)channel).Open(); var registerSuccess = channel.RegisterDuplexChannel(); Assert.True(registerSuccess, "Registration was not successful"); channel.SendMessage(testString); Assert.Equal(1, callback.ReceivedMessages.Count); Assert.Equal(testString, callback.ReceivedMessages[0]); ((IChannel)channel).Close(); factory.Close(); } finally { ServiceHelper.CloseServiceModelObjects((IChannel)channel, factory); } } }
/// <summary> /// Attempts to create a communication connection. /// </summary> /// <param name="exception">Returns an <see cref="Exception"/> if anything should go wrong in the attempt.</param> /// <returns>The state of the channel's connection.</returns> protected override bool OnOpen(out Exception exception) { exception = null; bool result = false; if (_Factory == null) { try { _ClientActual = new ClientActual(base.ClientConfiguration, base.TransportController, new Action <MessageEventArgs>((e) => { // OnSendMessage base.SendMessageOnMessageBus(e); }), new Action(() => { // OnCloseClient Task.Run(() => { base.Close(out Exception ex); }); }), new Action <int, TimeSpan>((retryCount, retryDuration) => { // OnRestartClient Task.Run(() => { // close client base.Close(out Exception exClose); // attempt to restart client for (int i = 0; i < retryCount; i++) { Threading.ThreadingHelper.Sleep(retryDuration); var openState = base.Open(out Exception exState); if (openState == ChannelStates.Open) { break; } } }); })); _Factory = new System.ServiceModel.DuplexChannelFactory <IService>(new System.ServiceModel.InstanceContext(_ClientActual), Binding, new System.ServiceModel.EndpointAddress(AddressUri)); _ServiceProxy = _Factory.CreateChannel(); result = true; } catch (Exception ex) { DestroyConnection(); exception = new Exception("Unable to establish connection with server at " + Address, ex); } } return(result); }
private void DestroyConnection() { if (_Factory != null) { try { _Factory.Close(); } catch { _Factory.Abort(); } ((IDisposable)_Factory).Dispose(); _Factory = null; } _ServiceProxy = null; _ClientActual = null; }
static private void BuildProxy <TProxyInterface, TCallback>(System.Uri uriEndpoint, Udbus.WCF.Client.IBindingFactory bindingFactory , TCallback callback , out TProxyInterface proxy, out System.ServiceModel.ICommunicationObject comms , uint maxTries, int wait) where TProxyInterface : class { System.ServiceModel.Channels.Binding binding = bindingFactory.Create(); System.ServiceModel.InstanceContext context = new System.ServiceModel.InstanceContext(callback); System.ServiceModel.DuplexChannelFactory <TProxyInterface> factory = new System.ServiceModel.DuplexChannelFactory <TProxyInterface>( context , binding , new System.ServiceModel.EndpointAddress(uriEndpoint) ); proxy = factory.CreateChannel(); comms = proxy as System.ServiceModel.ICommunicationObject; Proxy <TProxyInterface_> .AttemptClientOpen(ref comms, ref proxy, factory, maxTries, wait); }
public void InitializeTest() { System.Threading.Monitor.Enter(arithmeticProxyTable); try { arithmeticProxyTable.TryGetValue(System.Threading.Thread.CurrentThread.ManagedThreadId, out arithmeticClient); if (((arithmeticClient == null) || (((System.ServiceModel.ICommunicationObject)(arithmeticClient)).State == System.ServiceModel.CommunicationState.Faulted))) { // The following line may need to be customised to select the appropriate binding from the configuration file System.ServiceModel.ChannelFactory <Contracts.IArithmetic> arithmeticFactory = new System.ServiceModel.ChannelFactory <Contracts.IArithmetic>("Arithmetic"); arithmeticClient = arithmeticFactory.CreateChannel(); ((System.ServiceModel.ICommunicationObject)(arithmeticClient)).Open(); arithmeticProxyTable[System.Threading.Thread.CurrentThread.ManagedThreadId] = arithmeticClient; } } finally { System.Threading.Monitor.Exit(arithmeticProxyTable); } System.Threading.Monitor.Enter(collectionsProxyTable); try { collectionsProxyTable.TryGetValue(System.Threading.Thread.CurrentThread.ManagedThreadId, out collectionsClient); if (((collectionsClient == null) || (((System.ServiceModel.ICommunicationObject)(collectionsClient)).State == System.ServiceModel.CommunicationState.Faulted))) { // The following line may need to be customised to select the appropriate binding from the configuration file System.ServiceModel.ChannelFactory <Contracts.ICollections> collectionsFactory = new System.ServiceModel.ChannelFactory <Contracts.ICollections>("Collections"); collectionsClient = collectionsFactory.CreateChannel(); ((System.ServiceModel.ICommunicationObject)(collectionsClient)).Open(); collectionsProxyTable[System.Threading.Thread.CurrentThread.ManagedThreadId] = collectionsClient; } } finally { System.Threading.Monitor.Exit(collectionsProxyTable); } System.Threading.Monitor.Enter(customContractsProxyTable); try { customContractsProxyTable.TryGetValue(System.Threading.Thread.CurrentThread.ManagedThreadId, out customContractsClient); if (((customContractsClient == null) || (((System.ServiceModel.ICommunicationObject)(customContractsClient)).State == System.ServiceModel.CommunicationState.Faulted))) { // The following line may need to be customised to select the appropriate binding from the configuration file System.ServiceModel.ChannelFactory <Contracts.Custom.ICustomContracts> customContractsFactory = new System.ServiceModel.ChannelFactory <Contracts.Custom.ICustomContracts>("Custom"); customContractsClient = customContractsFactory.CreateChannel(); ((System.ServiceModel.ICommunicationObject)(customContractsClient)).Open(); customContractsProxyTable[System.Threading.Thread.CurrentThread.ManagedThreadId] = customContractsClient; } } finally { System.Threading.Monitor.Exit(customContractsProxyTable); } System.Threading.Monitor.Enter(customContracts2ProxyTable); try { customContracts2ProxyTable.TryGetValue(System.Threading.Thread.CurrentThread.ManagedThreadId, out customContracts2Client); if (((customContracts2Client == null) || (((System.ServiceModel.ICommunicationObject)(customContracts2Client)).State == System.ServiceModel.CommunicationState.Faulted))) { // The following line may need to be customised to select the appropriate binding from the configuration file System.ServiceModel.ChannelFactory <Contracts.Custom.ICustomContracts2> customContracts2Factory = new System.ServiceModel.ChannelFactory <Contracts.Custom.ICustomContracts2>("Custom2"); customContracts2Client = customContracts2Factory.CreateChannel(); ((System.ServiceModel.ICommunicationObject)(customContracts2Client)).Open(); customContracts2ProxyTable[System.Threading.Thread.CurrentThread.ManagedThreadId] = customContracts2Client; } } finally { System.Threading.Monitor.Exit(customContracts2ProxyTable); } System.Threading.Monitor.Enter(bufferedStreamServiceProxyTable); try { bufferedStreamServiceProxyTable.TryGetValue(System.Threading.Thread.CurrentThread.ManagedThreadId, out bufferedStreamServiceClient); if (((bufferedStreamServiceClient == null) || (((System.ServiceModel.ICommunicationObject)(bufferedStreamServiceClient)).State == System.ServiceModel.CommunicationState.Faulted))) { // The following line may need to be customised to select the appropriate binding from the configuration file System.ServiceModel.ChannelFactory <Contracts.IBufferedStreamService> bufferedStreamServiceFactory = new System.ServiceModel.ChannelFactory <Contracts.IBufferedStreamService>("BufferedStreams"); bufferedStreamServiceClient = bufferedStreamServiceFactory.CreateChannel(); ((System.ServiceModel.ICommunicationObject)(bufferedStreamServiceClient)).Open(); bufferedStreamServiceProxyTable[System.Threading.Thread.CurrentThread.ManagedThreadId] = bufferedStreamServiceClient; } } finally { System.Threading.Monitor.Exit(bufferedStreamServiceProxyTable); } System.Threading.Monitor.Enter(streamedStreamServiceProxyTable); try { streamedStreamServiceProxyTable.TryGetValue(System.Threading.Thread.CurrentThread.ManagedThreadId, out streamedStreamServiceClient); if (((streamedStreamServiceClient == null) || (((System.ServiceModel.ICommunicationObject)(streamedStreamServiceClient)).State == System.ServiceModel.CommunicationState.Faulted))) { // The following line may need to be customised to select the appropriate binding from the configuration file System.ServiceModel.ChannelFactory <Contracts.IStreamedStreamService> streamedStreamServiceFactory = new System.ServiceModel.ChannelFactory <Contracts.IStreamedStreamService>("StreamedStreams"); streamedStreamServiceClient = streamedStreamServiceFactory.CreateChannel(); ((System.ServiceModel.ICommunicationObject)(streamedStreamServiceClient)).Open(); streamedStreamServiceProxyTable[System.Threading.Thread.CurrentThread.ManagedThreadId] = streamedStreamServiceClient; } } finally { System.Threading.Monitor.Exit(streamedStreamServiceProxyTable); } System.Threading.Monitor.Enter(shapeServiceProxyTable); try { shapeServiceProxyTable.TryGetValue(System.Threading.Thread.CurrentThread.ManagedThreadId, out shapeServiceClient); if (((shapeServiceClient == null) || (((System.ServiceModel.ICommunicationObject)(shapeServiceClient)).State == System.ServiceModel.CommunicationState.Faulted))) { // The following line may need to be customised to select the appropriate binding from the configuration file System.ServiceModel.ChannelFactory <Contracts.IShapeService> shapeServiceFactory = new System.ServiceModel.ChannelFactory <Contracts.IShapeService>("Shape"); shapeServiceClient = shapeServiceFactory.CreateChannel(); ((System.ServiceModel.ICommunicationObject)(shapeServiceClient)).Open(); shapeServiceProxyTable[System.Threading.Thread.CurrentThread.ManagedThreadId] = shapeServiceClient; } } finally { System.Threading.Monitor.Exit(shapeServiceProxyTable); } System.Threading.Monitor.Enter(serviceKnownTypeProxyTable); try { serviceKnownTypeProxyTable.TryGetValue(System.Threading.Thread.CurrentThread.ManagedThreadId, out serviceKnownTypeClient); if (((serviceKnownTypeClient == null) || (((System.ServiceModel.ICommunicationObject)(serviceKnownTypeClient)).State == System.ServiceModel.CommunicationState.Faulted))) { // The following line may need to be customised to select the appropriate binding from the configuration file System.ServiceModel.ChannelFactory <Contracts.IServiceKnownType> serviceKnownTypeFactory = new System.ServiceModel.ChannelFactory <Contracts.IServiceKnownType>("ServiceKnownType"); serviceKnownTypeClient = serviceKnownTypeFactory.CreateChannel(); ((System.ServiceModel.ICommunicationObject)(serviceKnownTypeClient)).Open(); serviceKnownTypeProxyTable[System.Threading.Thread.CurrentThread.ManagedThreadId] = serviceKnownTypeClient; } } finally { System.Threading.Monitor.Exit(serviceKnownTypeProxyTable); } System.Threading.Monitor.Enter(sharePricesProxyTable); try { sharePricesProxyTable.TryGetValue(System.Threading.Thread.CurrentThread.ManagedThreadId, out sharePricesClient); if (((sharePricesClient == null) || (((System.ServiceModel.ICommunicationObject)(sharePricesClient)).State == System.ServiceModel.CommunicationState.Faulted))) { // The following line may need to be customised to select the appropriate binding from the configuration file System.ServiceModel.DuplexChannelFactory <Contracts.ISharePrices> sharePricesFactory = new System.ServiceModel.DuplexChannelFactory <Contracts.ISharePrices>(new InstanceContext(new SharePricesCallback()), "SharePrices"); sharePricesClient = sharePricesFactory.CreateChannel(); ((System.ServiceModel.ICommunicationObject)(sharePricesClient)).Open(); sharePricesProxyTable[System.Threading.Thread.CurrentThread.ManagedThreadId] = sharePricesClient; } } finally { System.Threading.Monitor.Exit(sharePricesProxyTable); } System.Threading.Monitor.Enter(dataSetsProxyTable); try { dataSetsProxyTable.TryGetValue(System.Threading.Thread.CurrentThread.ManagedThreadId, out dataSetsClient); if (((dataSetsClient == null) || (((System.ServiceModel.ICommunicationObject)(dataSetsClient)).State == System.ServiceModel.CommunicationState.Faulted))) { // The following line may need to be customised to select the appropriate binding from the configuration file System.ServiceModel.ChannelFactory <Contracts.IDataSets> dataSetsFactory = new System.ServiceModel.ChannelFactory <Contracts.IDataSets>("DataSets"); dataSetsClient = dataSetsFactory.CreateChannel(); ((System.ServiceModel.ICommunicationObject)(dataSetsClient)).Open(); dataSetsProxyTable[System.Threading.Thread.CurrentThread.ManagedThreadId] = dataSetsClient; } } finally { System.Threading.Monitor.Exit(dataSetsProxyTable); } }