示例#1
0
        public override void Execute()
        {
            ServiceObject so = Service.ServiceObjects[0];

            try
            {
                if (base.Service.ServiceConfiguration.ServiceAuthentication.AuthenticationMode == AuthenticationMode.ServiceAccount)
                {
                    System.Security.Principal.WindowsIdentity.Impersonate(IntPtr.Zero);
                }

                //TODO: improve performance? http://bloggingabout.net/blogs/vagif/archive/2010/04/02/don-t-use-activator-createinstance-or-constructorinfo-invoke-use-compiled-lambda-expressions.aspx

                // This creates an instance of the object responsible to handle the execution.
                // We can't cache the instance itself, as that gives threading issue because the
                // object can be re-used by the k2 host server for multiple different SMO calls
                // so we always need to know which ServiceObject we actually want to execute and
                // create an instance first. This is  "late" initalization. We can also not keep a list of
                // service objects that have been instanciated around in memory as this would be to resource
                // intensive and slow (as we would constantly initialize all).
                if (so == null || string.IsNullOrEmpty(so.Name))
                {
                    throw new ApplicationException("ServiceObject is not set.");
                }
                if (!ServiceObjectToType.ContainsKey(so.Name))
                {
                    throw new ApplicationException(string.Format("{0} is not a valid service object in the ServiceObjectType collection.", so.Name));
                }
                Type              soType      = ServiceObjectToType[so.Name];
                object[]          constParams = new object[] { this };
                ServiceObjectBase soInstance  = Activator.CreateInstance(soType, constParams) as ServiceObjectBase;

                soInstance.Execute();
                ServicePackage.IsSuccessful = true;
            }
            catch (Exception ex)
            {
                StringBuilder error = new StringBuilder();
                error.AppendFormat("Exception.Message: {0}\n", ex.Message);
                error.AppendFormat("Exception.StackTrace: {0}\n", ex.StackTrace);

                Exception innerEx = ex;
                int       i       = 0;
                while (innerEx.InnerException != null)
                {
                    error.AppendFormat("{0} InnerException.Message: {1}\n", i, innerEx.InnerException.Message);
                    error.AppendFormat("{0} InnerException.StackTrace: {1}\n", i, innerEx.InnerException.StackTrace);
                    innerEx = innerEx.InnerException;
                    i++;
                }
                ServicePackage.ServiceMessages.Add(error.ToString(), MessageSeverity.Error);
                ServicePackage.IsSuccessful = false;
            }
        }
示例#2
0
        public override void Execute()
        {
            // Value can't be set in K2Connection constructor, because the SmartBroker sets the UserName value after Init
            K2Connection.UserName = Service.ServiceConfiguration.ServiceAuthentication.UserName;

            ServiceObject so = Service.ServiceObjects[0];

            try
            {
                //TODO: improve performance? http://bloggingabout.net/blogs/vagif/archive/2010/04/02/don-t-use-activator-createinstance-or-constructorinfo-invoke-use-compiled-lambda-expressions.aspx

                // This creates an instance of the object responsible to handle the execution.
                // We can't cache the instance itself, as that gives threading issue because the
                // object can be re-used by the k2 host server for multiple different SMO calls
                // so we always need to know which ServiceObject we actually want to execute and
                // create an instance first. This is  "late" initalization. We can also not keep a list of
                // service objects that have been instanciated around in memory as this would be to resource
                // intensive and slow (as we would constantly initialize all).
                if (so == null || string.IsNullOrEmpty(so.Name))
                {
                    throw new ApplicationException(Resources.SOIsNotSet);
                }
                Type soType = GetServiceObjectByType(so.Name);
                if (soType == null)
                {
                    throw new ApplicationException("Could not get Type based on Service Object name");
                }
                object[]          constParams = new object[] { this };
                ServiceObjectBase soInstance  = Activator.CreateInstance(soType, constParams) as ServiceObjectBase;

                soInstance.Execute();
                ServicePackage.IsSuccessful = true;
            }
            catch (Exception ex)
            {
                StringBuilder error = new StringBuilder();
                error.AppendFormat("Exception.Message: {0}\n", ex.Message);
                error.AppendFormat("Exception.StackTrace: {0}\n", ex.StackTrace);

                Exception innerEx = ex;
                int       i       = 0;
                while (innerEx.InnerException != null)
                {
                    error.AppendFormat("{0} InnerException.Message: {1}\n", i, innerEx.InnerException.Message);
                    error.AppendFormat("{0} InnerException.StackTrace: {1}\n\n", i, innerEx.InnerException.StackTrace);
                    innerEx = innerEx.InnerException;
                    i++;
                }

                error.AppendLine();
                if (base.Service.ServiceObjects.Count > 0)
                {
                    foreach (ServiceObject executingSo in base.Service.ServiceObjects)
                    {
                        error.AppendFormat("Service Object Name: {0}\n", executingSo.Name);
                        foreach (Method method in executingSo.Methods)
                        {
                            error.AppendFormat("Service Object Methods: {0}\n", method.Name);
                        }

                        foreach (Property prop in executingSo.Properties)
                        {
                            string val = prop.Value as string;
                            if (!string.IsNullOrEmpty(val))
                            {
                                error.AppendFormat("[{0}].{1}: {2}\n", executingSo.Name, prop.Name, val);
                            }
                            else
                            {
                                error.AppendFormat("[{0}].{1}: [String.Empty]\n", executingSo.Name, prop.Name);
                            }
                        }
                    }
                }


                ServicePackage.ServiceMessages.Add(error.ToString(), MessageSeverity.Error);
                ServicePackage.IsSuccessful = false;
            }
        }