internal void SaveServiceInstance(
            ServiceExecutionHost host,
            Guid instanceID,
            Guid parentInstanceID,
            ServiceConfiguration config,
            ServiceStateInfo stateInfo,
            SchedulingInfo schedulingInfo
            )
        {
            // The first time we save write the configuration to XML. Otherwise ignore.
            string serializedConfig = null;

            if (stateInfo.State == ServiceState.Initializing)
            {
                var stringWriter = new StringWriter();
                using (var writer = new XmlTextWriter(stringWriter))
                    new NetDataContractSerializer().WriteObject(writer, config);
                serializedConfig = stringWriter.ToString();
            }

            var env = this.EnvironmentConfiguration;

            using (var connection = new SqlConnection(env.ConnectionString))
            {
                var command = new SqlCommand(env.SP_InstanceSave, connection);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddWithValue("@instanceID", instanceID.ToString("N"));
                command.Parameters.AddWithValue("@parentInstanceID", SqlUtility.SqlValue(parentInstanceID, Guid.Empty, () => parentInstanceID.ToString("N")));
                command.Parameters.AddWithValue("@profileID", SqlUtility.SqlValue(config.Profile, () => config.Profile.ProfileID.ToString("N")));
                command.Parameters.AddWithValue("@serviceName", config.ServiceName);
                command.Parameters.AddWithValue("@hostName", host.HostName);
                command.Parameters.AddWithValue("@hostGuid", host.HostGuid.ToString("N"));
                command.Parameters.AddWithValue("@progress", stateInfo.Progress);
                command.Parameters.AddWithValue("@state", stateInfo.State);
                command.Parameters.AddWithValue("@outcome", stateInfo.Outcome);
                command.Parameters.AddWithValue("@timeInitialized", SqlUtility.SqlValue(stateInfo.TimeInitialized, DateTime.MinValue));
                command.Parameters.AddWithValue("@timeStarted", SqlUtility.SqlValue(stateInfo.TimeStarted, DateTime.MinValue));
                command.Parameters.AddWithValue("@timeEnded", SqlUtility.SqlValue(stateInfo.TimeEnded, DateTime.MinValue));
                command.Parameters.AddWithValue("@timeLastPaused", SqlUtility.SqlValue(stateInfo.TimeLastPaused, DateTime.MinValue));
                command.Parameters.AddWithValue("@timeLastResumed", SqlUtility.SqlValue(stateInfo.TimeLastResumed, DateTime.MinValue));
                command.Parameters.AddWithValue("@resumeCount", stateInfo.ResumeCount);
                command.Parameters.AddWithValue("@configuration", SqlUtility.SqlValue(serializedConfig));
                command.Parameters.AddWithValue("@Scheduling_Status", SqlUtility.SqlValue(schedulingInfo, () => schedulingInfo.SchedulingStatus));
                command.Parameters.AddWithValue("@Scheduling_Scope", SqlUtility.SqlValue(schedulingInfo, () => schedulingInfo.SchedulingScope));
                command.Parameters.AddWithValue("@Scheduling_MaxDeviationBefore", SqlUtility.SqlValue(schedulingInfo, () => schedulingInfo.MaxDeviationBefore));
                command.Parameters.AddWithValue("@Scheduling_MaxDeviationAfter", SqlUtility.SqlValue(schedulingInfo, () => schedulingInfo.MaxDeviationAfter));
                command.Parameters.AddWithValue("@Scheduling_RequestedTime", SqlUtility.SqlValue(schedulingInfo, () => SqlUtility.SqlValue(schedulingInfo.RequestedTime, DateTime.MinValue)));
                command.Parameters.AddWithValue("@Scheduling_ExpectedStartTime", SqlUtility.SqlValue(schedulingInfo, () => SqlUtility.SqlValue(schedulingInfo.ExpectedStartTime, DateTime.MinValue)));
                command.Parameters.AddWithValue("@Scheduling_ExpectedEndTime", SqlUtility.SqlValue(schedulingInfo, () => SqlUtility.SqlValue(schedulingInfo.ExpectedEndTime, DateTime.MinValue)));

                connection.Open();
                command.ExecuteNonQuery();
            }
        }
        internal void UnregisterHost(ServiceExecutionHost host)
        {
            var env = this.EnvironmentConfiguration;

            using (var connection = new SqlConnection(env.ConnectionString))
            {
                var command = new SqlCommand(env.SP_HostUnregister, connection);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddWithValue("@hostName", host.HostName);
                connection.Open();
                command.ExecuteNonQuery();
            }
        }
        internal void RegisterHost(ServiceExecutionHost host)
        {
            var env = this.EnvironmentConfiguration;

            using (var connection = new SqlConnection(env.ConnectionString))
            {
                // FUTURE: in the future save all endpoints to DB
                var command = new SqlCommand(env.SP_HostRegister, connection);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddWithValue("@hostName", host.HostName);
                command.Parameters.AddWithValue("@hostGuid", host.HostGuid.ToString("N"));
                command.Parameters.AddWithValue("@endpointName", host.WcfHost.Description.Endpoints.First(endpoint => endpoint.Name == typeof(ServiceExecutionHost).FullName).Name);
                command.Parameters.AddWithValue("@endpointAddress", host.WcfHost.Description.Endpoints.First(endpoint => endpoint.Name == typeof(ServiceExecutionHost).FullName).Address.ToString());
                connection.Open();
                command.ExecuteNonQuery();
            }
        }
Esempio n. 4
0
        internal void Init(ServiceExecutionHost host, ServiceEnvironmentConfiguration envConfig, ServiceConfiguration config, SchedulingInfo schedulingInfo, Guid instanceID, Guid parentInstanceID)
        {
            Host             = host;
            this.Environment = new ServiceEnvironment(envConfig);

            this.InstanceID     = instanceID;
            this.Configuration  = config;
            this.SchedulingInfo = schedulingInfo;

            if (parentInstanceID != Guid.Empty)
            {
                this.ParentInstance = Environment.GetServiceInstance(parentInstanceID);
            }

            Current = this;

            // Monitor app domain-level events
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(this.DomainUnhandledException);
            AppDomain.CurrentDomain.DomainUnload       += new EventHandler(this.DomainUnload);

            StateInfo.TimeInitialized = DateTime.Now;
            StateInfo.State           = ServiceState.Ready;
            NotifyState();
        }