예제 #1
0
        private void TearDown()
        {
            if (encapsulatedHost == null)
            {
                return;
            }

            try
            {
                if (encapsulatedHost.Host.State == CommunicationState.Opened || encapsulatedHost.Host.State == CommunicationState.Opening)
                {
                    encapsulatedHost.Host.Close(new TimeSpan(0, 0, 0, 1));
                }
            }
            catch (TimeoutException timeoutEx)
            {
                Debug.WriteLine("Failed to close the service host - Exception: " + timeoutEx.Message);
            }
            catch (CommunicationException communicationEx)
            {
                Debug.WriteLine("Failed to close the service host - Exception: " + communicationEx.Message);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception: " + ex.Message);
                throw;
            }
            finally
            {
                encapsulatedHost = null;
            }
        }
예제 #2
0
        public TServiceContract CreateProxy()
        {
            encapsulatedHost =
                new WcfServiceFactory <TServiceContract>()
                .Create(serviceInstance, serviceConfiguration, registerErrorHandler);

            factory = new ChannelFactory <TServiceContract>(serviceConfiguration.Binding);
            proxy   = factory.CreateChannel(new EndpointAddress(serviceConfiguration.Url));

            return(proxy);
        }
예제 #3
0
        public EncapsulatedHost Create(TServiceContract service, ServiceConfiguration serviceConfiguration, bool registerErrorHandler)
        {
            PrepareForLongOperation(serviceConfiguration);

            serviceConfiguration.Binding.OpenTimeout    = timeSpan;
            serviceConfiguration.Binding.CloseTimeout   = timeSpan;
            serviceConfiguration.Binding.SendTimeout    = timeSpan;
            serviceConfiguration.Binding.ReceiveTimeout = timeSpan;

            var host =
                new ServiceHost(service.GetType())
            {
                CloseTimeout = timeSpan,
                OpenTimeout  = timeSpan
            };

            var encapsulatedHost =
                new EncapsulatedHost(
                    host,
                    typeof(TServiceContract),
                    service,
                    serviceConfiguration.Binding,
                    serviceConfiguration.Url,
                    registerErrorHandler);

            try
            {
                host.Open();
            }
            catch (TimeoutException timeoutException)
            {
                Debug.WriteLine("Failed to close the service host - Exception: " + timeoutException.Message);
                throw;
            }
            catch (CommunicationException communicationException)
            {
                if (communicationException.Message.Contains("10013"))
                {
                    PortAlreadyUsed(serviceConfiguration, communicationException);
                }

                Debug.WriteLine("Failed to close the service host - Exception: " + communicationException.Message);
                throw;
            }

            return(encapsulatedHost);
        }