Beispiel #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="WcfClientInfo"/> class.
        /// </summary>
        internal WcfClientInfo(long id, string service, IClientVersionConfig clientConfig)
        {
            Id               = id;
            Service          = service;
            ClientVersion    = clientConfig.ClientVersion;
            MinServerVersion = clientConfig.MinServerVersion;

            ServerVersion    = Unknown;
            MinClientVersion = Unknown;
            Uri = Unknown;

            State = ConnectionState.New;
        }
        /// <summary>
        /// Adds a new client to the monitoring quene.
        /// </summary>
        private long AddClientToMonitor <T, TK>(IClientVersionConfig config, object callbackService, Action <ConnectionState, T> callback, Binding binding)
            where T : ClientBase <TK>
            where TK : class
        {
            // ReSharper disable UseObjectOrCollectionInitializer
            // ReSharper disable ConvertToLambdaExpression

            StartOnDemand();

            var serviceName = typeof(TK).Name;
            var clientType  = typeof(T);

            // Add client to collection
            var request = new MonitoredClient
            {
                Binding         = binding,
                ClientType      = clientType,
                ServiceName     = serviceName,
                CallbackService = callbackService,
                ClientInfo      = new WcfClientInfo(CreateClientId(), serviceName, config),
            };

            //Wraps the client callback in safe delegate to avoid invoke exceptions
            request.ConnectionCallback = delegate(ConnectionState state, ICommunicationObject client)
            {
                try
                {
                    callback(state, (T)client);
                }
                catch (Exception e)
                {
                    Logger.LogException(LogLevel.Error, e, "Factory callback threw exception");
                }
            };

            //Set delegate to get the inner channel of the wcf client
            request.GetInnerChannel = client => ((T)client).InnerChannel;

            //Set delegate to get the ClientCredentials of the wcf client
            request.GetClientCredentials = client => ((T)client).ClientCredentials;

            //Set delegate to add additional endpoint behaviors
            request.AddEndpointBehavior = (client, behavior) => ((T)client).Endpoint.Behaviors.Add(behavior);

            if (string.IsNullOrEmpty(config.MinServerVersion))
            {
                Logger.Log(LogLevel.Info, "Creating WCF client {0} for '{1}' without version check", request.ClientInfo.Id, serviceName);
            }
            else
            {
                Logger.Log(LogLevel.Info, "Creating WCF client {0} for '{1}', client version {2}, min. service version {3}",
                           request.ClientInfo.Id, serviceName, config.ClientVersion, config.MinServerVersion);
            }

            var clientConfig = config as IClientConfig;

            if (clientConfig != null)
            {
                request.Config         = clientConfig;
                request.ClientInfo.Uri = CreateEndpointAddress(request.ServiceConfiguration, request.Config).Uri.ToString();
            }

            lock (_monitoredClients)
                _monitoredClients.Add(request);

            _clientInfos.Add(request.ClientInfo);

            RaiseClientInfoChanged(request.ClientInfo);

            return(request.ClientInfo.Id);
        }