public MemoryStream Encode(object result, ServiceContext context) { if (!context.Contains("jsonrpc") || !(bool)context["jsonrpc"]) { return(ServiceCodec.Instance.Encode(result, context)); } var response = new Dictionary <string, object> { { "jsonrpc", "2.0" } }; if (context.Contains("jsonrpc.id")) { response["id"] = context["jsonrpc.id"]; } if (context.ResponseHeaders.Count > 0) { response["headers"] = context.ResponseHeaders; } if (result is Exception) { var error = result as Exception; response["error"] = error.Message switch { "Parse error" => new Dictionary <string, object> { { "code", -32700 }, { "message", "Parse error" } }, "Invalid Request" => new Dictionary <string, object> { { "code", -32600 }, { "message", "Invalid Request" } }, "Method not found" => new Dictionary <string, object> { { "code", -32601 }, { "message", "Method not found" } }, "Invalid params" => new Dictionary <string, object> { { "code", -32602 }, { "message", "Invalid params" } }, _ => new Dictionary <string, object> { { "code", 0 }, { "message", error.Message }, { "data", error.StackTrace } }, }; } else { response["result"] = result; } var data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(response)); return(new MemoryStream(data, 0, data.Length, false, true)); }
protected override ServiceOutcome DoWork() { // Get the delivery ID either from configuration or from service context int deliveryID; if (ServiceContext.Contains("DeliveryID")) { deliveryID = (Int32)Instance.ServiceContext["DeliveryID"]; } else if (Instance.Configuration.Options.Contains("DeliveryID")) { deliveryID = Int32.Parse(Instance.Configuration.Options["DeliveryID"]); } else { throw new ConfigurationException("DeliveryID must be specified in configuration."); } // Get the delivery created by the retriever _delivery = Delivery.Get(deliveryID); // Iterate delivery files foreach (DeliveryFile file in _delivery.Files.Where(file => file.ReaderType == typeof(ExampleReader))) { using (DataManager.Current.OpenConnection()) { DataManager.Current.StartTransaction(); // Constructor gets reference to campaignManager so that the batch knows which rows // are dependent on the campaign manager first being comitted using (SqlBulkCopy batch = DataManager.CreateBulkCopy(SqlBulkCopyOptions.Default)) { // Dump every 30 rows (get this value from configuration) batch.BufferSize = 30; batch.DestinationTableName = "SOME_DB_TABLE"; // Delivery row type can be inherited to add custom transactions using (ExampleReader reader = file.CreateReader()) { while (reader.Read()) { batch.WriteToServer(ConvertToDataRow(file, (PpcExampleRow)reader.CurrentRow)); } } } file.HandledByServices.Add(this.Instance.InstanceID); file.Save(); DataManager.Current.CommitTransaction(); } } }
public Stream Encode(object result, ServiceContext context) { if (!context.Contains("jsonrpc") || !context["jsonrpc"]) { return(ServiceCodec.Instance.Encode(result, context)); } JObject response = new JObject { { "jsonrpc", "2.0" } }; if (context.Contains("jsonrpc.id")) { response["id"] = context["jsonrpc.id"]; } if (result is Exception) { var error = result as Exception; switch (error.Message) { case "Parse error": response["error"] = new JObject { { "code", -32700 }, { "message", "Parse error" } }; break; case "Invalid Request": response["error"] = new JObject { { "code", -32600 }, { "message", "Invalid Request" } }; break; case "Method not found": response["error"] = new JObject { { "code", -32601 }, { "message", "Method not found" } }; break; case "Invalid params": response["error"] = new JObject { { "code", -32602 }, { "message", "Invalid params" } }; break; default: response["error"] = new JObject { { "code", 0 }, { "message", error.Message }, { "data", error.StackTrace } }; break; } } else { response["result"] = new JValue(result); } var data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(response)); return(new MemoryStream(data, 0, data.Length, false, true)); }