See Also: irServer - Rule Execution Service
The following example demonstrates calling the irServer - Rule Execution Service REST interface synchronously with the legacy WebRequest class.
// The name of the ruleApp in the Catalog
string ruleApp = "MortgageCalculator";
// The address of the Rule Execution Service
string ruleExecutionServiceURI = "http://localhost/InRuleRuleEngineService_v5.0.16/HttpService.svc";
// The name of the Entity
string entityName = "Mortgage";// The name of the RuleSet to execute, or use 'ApplyRules' if found to be null.
string ruleSetName = "PaymentSummaryRules";
// The entity state as a C# dynamic type
dynamic entityState = new
{
LoanInfo = new
{
Principal = 53000m, APR = 7.625f, TermInYears = 15
}
};
// We use Newtonsoft's Json.Net library to convert this dynamic C# object to a JSON string
string entityStateJsonInput = JsonConvert.SerializeObject(entityState);
// New create the full request, inserting the JSON string that represents EntityState.
dynamic requestData = new
{
RuleApp = new
{
RepositoryRuleAppRevisionSpec = new
{
RuleApplicationName = ruleApp
}
},
EntityName = entityName,
EntityState = entityStateJsonInput,
RuleSetName = ruleSetName
};
string requestDataString = JsonConvert.SerializeObject(requestData);
//If no ruleSetName exists, execute Auto rules, otherwise execute explicit rule.
string postUri;
if (string.IsNullOrEmpty(ruleSetName))
{ postUri = ruleExecutionServiceURI + "/ApplyRules"; }
else
{ postUri = ruleExecutionServiceURI + "/ExecuteRuleSet"; }
// Encode our string into an array of bytes, needed by WebRequest.
UTF8Encoding encoding = new UTF8Encoding();
byte[] bytes = encoding.GetBytes(requestDataString);
// Create and prepare the request
WebRequest webRequest = WebRequest.Create(postUri);
// ApplyRules only works as a POST, not a GET
webRequest.Method = "POST";
// Our EntityState is encoded as an JSON document, not XML.
webRequest.ContentType = "application/json";
// WebRequest is old, and can't count by itself.
webRequest.ContentLength = bytes.Length;
string httpResponse;
// Create our requestStream by opening the request, make sure it gets disposed when done.
using (var requestStream = webRequest.GetRequestStream())
{
// send the data
requestStream.Write(bytes, 0, bytes.Length);
}
// In case it has not already been mentioned this is a rather primitive way of
// making an HTTP based request. We recommend you read the HttpClient based code samples
// elsewhere in this document, as that Asynchronous approach is a better way to go.
try
{
HttpWebResponse response = webRequest.GetResponse() as HttpWebResponse;
using (var stream = response.GetResponseStream())
{
var reader = new StreamReader(stream, Encoding.UTF8);
httpResponse = reader.ReadToEnd();
}
}
catch (WebException ex)
{
using (var stream = ex.Response.GetResponseStream())
{
var reader = new StreamReader(stream, Encoding.UTF8);
httpResponse = reader.ReadToEnd();
throw new Exception(httpResponse, ex);
}
}
dynamic resultJson = JsonConvert.DeserializeObject<dynamic>(httpResponse);
string entityJsonString = resultJson.EntityState;
dynamic entityJson = JsonConvert.DeserializeObject<dynamic>(entityJsonString);
string paymentSummaryJsonString = JsonConvert.SerializeObject(entityJson.PaymentSummary);
Comments
0 comments
Please sign in to leave a comment.