Page 1 of 1

Connection property has not been initialized On ReqFilter

PostPosted: Fri Mar 31, 2023 8:49 am
by sameermoin
Hi,

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;
    }

Re: Connection property has not been initialized On ReqFilte  Topic is solved

PostPosted: Fri Mar 31, 2023 10:48 am
by Mike.Sheen
sameermoin wrote: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.


It's probably because request filters are triggered before a Manager has been completely setup and connected to the database. Which version of the REST API plugin are you using? This is important as we recently changed all our use of Manager objects to no longer be pooled and instead created and disposed within each service request.

I'd use OrmLite in this specific case to read what you need from the database - it has less overhead than logging on with a Manager.

Re: Connection property has not been initialized On ReqFilte

PostPosted: Fri Mar 31, 2023 10:58 am
by sameermoin
Mike.Sheen wrote: Which version of the REST API plugin are you using?


I am using 7.2.1.32

I also tried below but on this it throwing file not found exception for REST API plugin:

Code: Select all
AuthUserSession session = (AuthUserSession)req.GetSession();         
         if (session == null)
            return;
         
         JiwaFinancials.Jiwa.JiwaApplication.Manager manager = null;
            RESTAPIPlugin.JiwaSessionDictionary.TryGetValue(session.Id, out manager);

            if (manager == null || manager.Database.APIKey_Type != "Debtor")
                return;

Re: Connection property has not been initialized On ReqFilte

PostPosted: Fri Mar 31, 2023 11:02 am
by Mike.Sheen
sameermoin wrote:I also tried below but on this it throwing file not found exception for REST API plugin


I don't see why it would be throwing a file not found exception.

I can provide you with an example using OrmLite to read from the database in a request filter.

If you want to solve your file not found exception, how about providing a stripped down, non-standard dependency free plugin which demonstrates your issue.

Re: Connection property has not been initialized On ReqFilte

PostPosted: Fri Mar 31, 2023 11:11 am
by sameermoin
Thanks mike for your help I just implemented ORM

Re: Connection property has not been initialized On ReqFilte

PostPosted: Fri Mar 31, 2023 9:34 pm
by sameermoin
Mike.Sheen wrote:If you want to solve your file not found exception, how about providing a stripped down, non-standard dependency free plugin which demonstrates your issue.


My issue is resolved but to show you the error I am attaching a demonstration plugin.

Screenshot 2023-03-31 153558.png
Screenshot 2023-03-31 153558.png (10.3 KiB) Viewed 5440 times


Screenshot 2023-03-31 154452.png


Plugin Demostration Plugin.xml
(25.15 KiB) Downloaded 69 times


if I removed the below part then it will work.

Code: Select all
AuthUserSession session = (AuthUserSession)req.GetSession();
        if (session == null)
            return;

        JiwaFinancials.Jiwa.JiwaApplication.Manager manager = null;
        RESTAPIPlugin.JiwaSessionDictionary.TryGetValue(session.Id, out manager);

        if (manager == null || manager.Database.APIKey_Type != "Debtor")
            return;

Re: Connection property has not been initialized On ReqFilte

PostPosted: Fri Mar 31, 2023 9:43 pm
by sameermoin
Plugin without code which causes filenotfoundexception:

Screenshot 2023-03-31 154158.png
Screenshot 2023-03-31 154158.png (10.46 KiB) Viewed 5440 times


Plugin Demostration Plugin.xml
(24.79 KiB) Downloaded 77 times

Re: Connection property has not been initialized On ReqFilte

PostPosted: Sat Apr 01, 2023 11:37 am
by SBarnes
I am guessing you are getting an exception that file not found is the REST api itself?

if this is the case then you need an assembly resolver, below is code that I have used related to a similar issue basically you need a system setting called JiwaPath that you can set to the Jiwa program directory.

The trick to part of this is the explicit read of PluginReferenceCollection as this is only read when the plugin actually compiles not on just at a reload at service start up.

This also has code to write to the application log to let you know what is going on.


Code: Select all
      public static string JiwaPath = "";
      
        public void Configure(JiwaFinancials.Jiwa.JiwaApplication.Plugin.Plugin Plugin, ServiceStack.ServiceStackHost AppHost, Funq.Container Container, JiwaApplication.Manager JiwaApplicationManager)
        {
//         System.Diagnostics.Debugger.Launch();
//         System.Diagnostics.Debugger.Break();
         
         //Forcing reading of the reference collection
         Plugin.PluginReferenceCollection.Read();
         LogToEventLog(Plugin, "Number of references " + Plugin.PluginReferenceCollection.Count.ToString(), System.Diagnostics.EventLogEntryType.Information);
         System.Reflection.AssemblyName aName = new System.Reflection.AssemblyName(System.Reflection.Assembly.GetExecutingAssembly().GetName().FullName);
      
         
         JiwaPath = JiwaApplicationManager.Database.ReadSysData(aName.Name, "JiwaPath", "").ToString();
         
      
         AppDomain.CurrentDomain.AssemblyResolve += delegate(object sender, System.ResolveEventArgs args) { return AssemblyResolve(sender, args, Plugin); };   
         
         
         
         
         
      }


and

Code: Select all
      public static void LogToEventLog(JiwaFinancials.Jiwa.JiwaApplication.Plugin.Plugin Plugin, string Message, System.Diagnostics.EventLogEntryType EventLogEntryType = System.Diagnostics.EventLogEntryType.Information)
      {

         try
         {

            string DatabaseName = Plugin.Manager.Database.DatabaseName;
            if (Message.Length > 32766)
            {
               Message = Message.Substring(0, 32766);
            }
            System.Diagnostics.EventLog Log = new System.Diagnostics.EventLog("Application");
            Log.Source = string.Format("Jiwa Plugin : {0}", "REST API " + DatabaseName);
            Log.WriteEntry(Message, EventLogEntryType);
            Log.Close();
         }
         catch (Exception ex)
         {
         }
      }
      
      
      
      public System.Reflection.Assembly AssemblyResolve(object sender, System.ResolveEventArgs args, JiwaFinancials.Jiwa.JiwaApplication.Plugin.Plugin Plugin)
      {
//         System.Diagnostics.Debugger.Launch();
//         System.Diagnostics.Debugger.Break();
         // If the requested assembly is a reference to another plugin, then we need to attempt to resolve the assembly ourselves
         LogToEventLog(Plugin, "Resolver reached with Plugin " +Plugin.Name, System.Diagnostics.EventLogEntryType.Information);
         LogToEventLog(Plugin, "Trying to resolve aseembly " + args.Name, System.Diagnostics.EventLogEntryType.Information);
         if(Plugin.PluginReferenceCollection.Count == 0)
         {
            Plugin.PluginReferenceCollection.Read();
         }
         LogToEventLog(Plugin, "Number of plugin references " + Plugin.PluginReferenceCollection.Count.ToString(), System.Diagnostics.EventLogEntryType.Information);
         foreach(JiwaApplication.Plugin.PluginReference pluginReference in Plugin.PluginReferenceCollection)
         {
            if (String.Format("{0}, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", pluginReference.Name) == args.Name)
            {
               LogToEventLog(Plugin, "Resolved assembly in references as  " + pluginReference.Plugin.RuntimeFileName , System.Diagnostics.EventLogEntryType.Information);
               return System.Reflection.Assembly.LoadFile(pluginReference.Plugin.RuntimeFileName);
            }
               
            
            if (String.Format("{0}, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", pluginReference.Name + ".dll") == args.Name)
            {
               LogToEventLog(Plugin, "Resolved assembly in references as  " + pluginReference.Plugin.RuntimeFileName , System.Diagnostics.EventLogEntryType.Information);
               return System.Reflection.Assembly.LoadFile(pluginReference.Plugin.RuntimeFileName);
            }
               
         }   
         
         
//         if(Plugin != null  && Plugin.EmbeddedReferenceCollection != null)
//         {
//            if(Plugin.EmbeddedReferenceCollection.Count == 0)
//            {
//               Plugin.EmbeddedReferenceCollection.Read();
//            }
//            LogToEventLog(Plugin, "Number of embedded references " + Plugin.EmbeddedReferenceCollection.Count.ToString(), System.Diagnostics.EventLogEntryType.Information);
//            foreach(JiwaApplication.Plugin.EmbeddedReference embeddedReference in Plugin.EmbeddedReferenceCollection)
//            {
//               LogToEventLog(Plugin, "Looking at embedded references " + embeddedReference.AssemblyName , System.Diagnostics.EventLogEntryType.Information);
//               if (String.Format("{0}, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", embeddedReference.AssemblyName) == args.Name)
//               {
//                  LogToEventLog(Plugin, "Resolved assembly in embedded references as  " + embeddedReference.AssemblyName , System.Diagnostics.EventLogEntryType.Information);
//                  return System.Reflection.Assembly.LoadFile(embeddedReference.AssemblyName);
//               }
//                  
//               
//               if (String.Format("{0}, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", embeddedReference.AssemblyName + ".dll") == args.Name)
//               {
//                  LogToEventLog(Plugin, "Resolved assembly in embedded references as  " + embeddedReference.AssemblyName , System.Diagnostics.EventLogEntryType.Information);
//                  return System.Reflection.Assembly.LoadFile(embeddedReference.AssemblyName);
//               }               
//            }            
//         }
//         

         
         
         if(Plugin != null && Plugin.Manager != null && Plugin.Manager.PluginCollection != null)
         {
            LogToEventLog(Plugin, "Now looking in plugin Collection which has " + Plugin.Manager.PluginCollection.Count.ToString() + " plugins", System.Diagnostics.EventLogEntryType.Information);
            foreach(JiwaApplication.Plugin.Plugin plugin in Plugin.Manager.PluginCollection)
            {
               //LogToEventLog(Plugin, "Now looking at plugin " + plugin.Name , System.Diagnostics.EventLogEntryType.Information);
               if (String.Format("{0}, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", plugin.Name) == args.Name)
               {
                  LogToEventLog(Plugin, "Resolved assembly in plugins as  " + plugin.RuntimeFileName , System.Diagnostics.EventLogEntryType.Information);
                  return System.Reflection.Assembly.LoadFile(plugin.RuntimeFileName);
               }
                  
               
               if (String.Format("{0}, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", plugin.Name + ".dll") == args.Name)
               {
                  LogToEventLog(Plugin, "Resolved assembly in plugins as  " + plugin.RuntimeFileName , System.Diagnostics.EventLogEntryType.Information);
                  return System.Reflection.Assembly.LoadFile(plugin.RuntimeFileName);
               }
                  
            }            
         }
         else
         {
            LogToEventLog(Plugin, "Could not process the plugin collection ", System.Diagnostics.EventLogEntryType.Error);
         }

         
         
         
         
            //string jiwaApplicationPath = @"C:\Program Files (x86)\Jiwa Financials\Jiwa 7";
         string jiwaApplicationPath = JiwaPath;
         LogToEventLog(Plugin, "Now looking in Jiwa Path " + JiwaPath , System.Diagnostics.EventLogEntryType.Information);   
            
            System.Reflection.Assembly resolvedAssembly = null;

            // NOTE: sometimes e.Name is the assembly FullName, sometimes it may be a path - depending on how the assembly is attempted to be loaded - the below will handle both cases
            string filename = args.Name.Split(new Char[] { ',' })[0].Replace(@"\\", @"\");
            string filenameAndPath = System.IO.Path.Combine(jiwaApplicationPath, filename) + ".dll";

            if (System.IO.File.Exists(filenameAndPath))
            {
            LogToEventLog(Plugin, "Resolved assembly in Jiwa Path as  " + filenameAndPath , System.Diagnostics.EventLogEntryType.Information);
                resolvedAssembly = System.Reflection.Assembly.LoadFrom(filenameAndPath);
            }

            return resolvedAssembly;
         
//         System.Diagnostics.Debugger.Launch();
//         System.Diagnostics.Debugger.Break();
         LogToEventLog(Plugin, "Trying to resolve " + args.Name + " failed.", System.Diagnostics.EventLogEntryType.Error);
         return null;
      }      
      
   }

Re: Connection property has not been initialized On ReqFilte

PostPosted: Sat Apr 01, 2023 12:45 pm
by SBarnes
Hi Sameer

I've updated your plugin to demonstrate exactly what the problem is basically I've added in the resolver you need but also some system settings, set both the read collection and scan plugins to false and stop and start the service twice as this only happens on a restart without compile and you will get the problem, set either of them to true and do the restart twice and the problem won't exist because the assembly resolver will work either because the plugin reference collection has been read explicitly or because it will scan all plugin locations.

Let me know if you have any further questions.