Exemplo n.º 1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ResolveRequest"/> class.
 /// </summary>
 /// <param name="providerName">
 /// Provider name
 /// </param>
 /// <param name="serviceName">
 /// Service name
 /// </param>
 /// <param name="bindingAccessPoint">
 /// Binding access point
 /// </param>
 /// <param name="bindingUrlType">
 /// Binding URL type
 /// </param>
 /// <param name="messageType">
 /// Message type
 /// </param>
 /// <param name="operationName">
 /// Operation name
 /// </param>
 /// <param name="messageRole">
 /// Message role
 /// </param>
 /// <param name="parameters">
 /// General parameters
 /// </param>
 /// <param name="messageDirection">
 /// Message direction
 /// </param>
 /// <param name="policyName">
 /// Policy name
 /// </param>
 /// <param name="version">
 /// Policy version in the format of x.y where x is the major and y is the minor version number.
 /// </param>
 public ResolveRequest(
     string providerName,
     string serviceName,
     string bindingAccessPoint,
     string bindingUrlType,
     string messageType,
     string operationName,
     string messageRole,
     Facts.Dictionaries.Parameters parameters,
     Facts.Interchange.MessageDirectionTypes messageDirection,
     string policyName,
     string version)
 {
     this.ProviderName       = providerName;
     this.ServiceName        = serviceName;
     this.BindingAccessPoint = bindingAccessPoint;
     this.BindingUrlType     = bindingUrlType;
     this.MessageType        = messageType;
     this.OperationName      = operationName;
     this.MessageRole        = messageRole;
     this.Parameters         = parameters;
     this.MessageDirection   = messageDirection;
     this.PolicyName         = policyName;
     this.Version            = version;
 }
        /// <summary>
        /// Evaluate the policy
        /// </summary>
        /// <param name="policyName">
        /// The name of the policy.
        /// </param>
        /// <param name="version">
        /// The policy version.
        /// </param>
        /// <param name="providerName">
        /// The provider name
        /// </param>
        /// <param name="serviceName">
        /// The service name
        /// </param>
        /// <param name="bindingAccessPoint">
        /// The binding access point.
        /// </param>
        /// <param name="bindingUrlType">
        /// The binding URL type.
        /// </param>
        /// <param name="messageType">
        /// The message type.
        /// </param>
        /// <param name="operationName">
        /// The operation name
        /// </param>
        /// <param name="messageRole">
        /// The role of the message.
        /// </param>
        /// <param name="parameters">
        /// A dictionary of parameters.
        /// </param>
        /// <param name="messageDirection">
        /// The direction of the message flow.
        /// </param>
        /// <returns>
        /// An interchange object containing the information resolved.
        /// </returns>
        internal static Facts.Interchange Eval(
            string policyName,
            Version version,
            string providerName,
            string serviceName,
            string bindingAccessPoint,
            string bindingUrlType,
            string messageType,
            string operationName,
            string messageRole,
            Facts.Dictionaries.Parameters parameters,
            Facts.Interchange.MessageDirectionTypes messageDirection)
        {
            var trace        = Convert.ToBoolean(ConfigurationManager.AppSettings[Properties.Resources.AppSettingsEsbBreTrace]);
            var tempFileName = string.Empty;

            if (trace)
            {
                var traceFileFolder = ConfigurationManager.AppSettings[Properties.Resources.AppSettingsEsbBreTraceFileLocation];

                if (!string.IsNullOrWhiteSpace(traceFileFolder))
                {
                    while (traceFileFolder.EndsWith(@"\"))
                    {
                        traceFileFolder = traceFileFolder.Substring(0, traceFileFolder.Length - 1);
                    }
                }

                if (string.IsNullOrWhiteSpace(traceFileFolder))
                {
                    traceFileFolder = @".";
                }

                tempFileName = string.Format(
                    @"{0}\DirectivePolicyTrace_{1}_{2}.txt",
                    traceFileFolder,
                    DateTime.Now.ToString("yyyyMMdd"),
                    Guid.NewGuid());
            }

            // Create the array of short-term facts
            var interchange = new Facts.Interchange
            {
                ProviderName       = providerName,
                ServiceName        = serviceName,
                BindingAccessPoint = bindingAccessPoint,
                BindingUrlType     = bindingUrlType,
                MessageType        = messageType,
                OperationName      = operationName,
                MessageRole        = messageRole,
                Parameters         = parameters,
                MessageDirection   = messageDirection
            };

            // Creates a fact array with an optional UDDI fact. Safely create a UDDI Inquiry Service
            // object using a dynamic reference.  UDDI use is optional, and the UDDI library may not
            // be installed.
            Func <object[]> createFactsWithOptionalUddi = () =>
            {
                try
                {
                    return(new[]
                    {
                        interchange,
                        Activator.CreateInstance(Properties.Resources.UddiAssembly, Properties.Resources.UddiInquiryService).Unwrap()
                    });
                }
                catch
                {
                    return(new object[] { interchange });
                }
            };

            // Determine if static support is being used by rule engine and only assert InquiryServices if not.
            var shortTermFacts = IsStaticSupport()
                ? new object[] { interchange }
                : createFactsWithOptionalUddi();

            if (Convert.ToBoolean(ConfigurationManager.AppSettings[Properties.Resources.AppSettingsEsbBrePolicyTester]))
            {
                PolicyTester policyTester = null;

                int min;
                int maj;

                if (version == null)
                {
                    maj = 1;
                    min = 0;
                }
                else
                {
                    maj = version.Major;
                    min = version.Minor;
                }

                try
                {
                    // Use PolicyTester
                    var srs     = new SqlRuleStore(GetRuleStoreConnectionString());
                    var rsi     = new RuleSetInfo(policyName, maj, min);
                    var ruleSet = srs.GetRuleSet(rsi);

                    if (ruleSet == null)
                    {
                        throw new EsbResolutionServiceException(string.Format(Properties.Resources.ExceptionRsNotInStore, policyName, maj, min));
                    }

                    policyTester = new PolicyTester(ruleSet);

                    if (trace)
                    {
                        // Create the debug tacking object
                        var dti = new DebugTrackingInterceptor(tempFileName);

                        // Execute the policy with trace
                        policyTester.Execute(shortTermFacts, dti);

                        // This is deliberately using Trace, rather than Debug
                        Trace.Write("[BusinessRuleEnginePolicyEvaluator] Eval Trace: " + dti.GetTraceOutput());
                    }
                    else
                    {
                        // Execute the policy
                        policyTester.Execute(shortTermFacts);
                    }
                }
                catch (Exception ex)
                {
                    EventLog.WriteEntry("ESBResolutionService", GetFullMessage(ex));
                    throw;
                }
                finally
                {
                    if (policyTester != null)
                    {
                        policyTester.Dispose();
                    }
                }
            }
            else
            {
                var policy = version == null ? new Policy(policyName) : new Policy(policyName, version.Major, version.Minor);

                try
                {
                    if (trace)
                    {
                        // Create the debug tacking object
                        var dti = new DebugTrackingInterceptor(tempFileName);

                        // Execute the policy with trace
                        policy.Execute(shortTermFacts, dti);

                        // This is deliberately using Trace, rather than Debug
                        Trace.Write("[BusinessRuleEnginePolicyEvaluator] Eval Trace: " + dti.GetTraceOutput());
                    }
                    else
                    {
                        // Execute the policy
                        policy.Execute(shortTermFacts);
                    }
                }
                catch (Exception ex)
                {
                    EventLog.WriteEntry("ESBResolutionService", GetFullMessage(ex));
                    throw;
                }
                finally
                {
                    policy.Dispose();
                }
            }

            Debug.Write("[BusinessRuleEnginePolicyEvaluator] Eval - Returned # directives: " + interchange.Directives.Count);

            // Perform any directive-level validations
            interchange.ValidateDirectives();

            // If any directives are invalid, raise an exception.
            var validityStrings = from directive in interchange.Directives
                                  where !directive.Value.IsValid
                                  select directive.Value.ValidErrors;

            var validityStringList = validityStrings as IList <string> ?? validityStrings.ToList();

            if (validityStringList.Any())
            {
                throw new EsbResolutionServiceException(validityStringList.Aggregate((s1, s2) => s1 + s2).Trim());
            }

            return(interchange);
        }