I have a custom endpoint on which I am doing some additional validations. I registered "RegisterTypedRequestFilter" to validate the condition using a SQL query. When the query is going to run I am getting an error Connection property has not been initialized because the SQLConnection property in the Manager object is nulled other than that all properties contain values.
I am doing something like below:
- Code: Select all
public void Configure(Plugin Plugin, ServiceStackHost AppHost, Container Container, Manager JiwaApplicationManager)
{
AppHost.Routes.Add(typeof(CustomGETRequest), "/Custom", "GET", Custom endpoint.", "");
AppHost.RegisterTypedRequestFilter<CustomGETRequest>((req, res, dto) => { CustomGETRequestFilter(req, res, dto, Plugin.Manager); });
}
public void CustomGETRequestFilter(IRequest req, IResponse res, CustomGETRequest dto, Manager manager)
{
string apikey = req.QueryString["Apikey"];
if (string.IsNullOrEmpty(apikey))
throw new UnauthorizedAccessException();
var queryResult = ExecuteQuery(manager.Database.SQLConnection, dto.Id);
}
public string ExecuteQuery(SqlConnection dbConnection, string id)
{
string debtorId = string.Empty;
string query = @"Some query";
using (SqlCommand sqlCommand = new SqlCommand(query, dbConnection))
{
try
{
sqlCommand.Parameters.AddWithValue("@Id", id);
object queryResult = sqlCommand.ExecuteScalar();
if (queryResult != null)
{
debtorId = Convert.ToString(queryResult);
}
}
catch (Exception ex)
{
throw ex;
}
}
return debtorId;
}