Endpoint Returning FileNotFound for Rest API Plugin  Topic is solved

Discussions relating to plugin development, and the Jiwa API.

Endpoint Returning FileNotFound for Rest API Plugin

Postby iamgurney » Fri Jun 02, 2023 10:33 pm

I am following REST API Custom Route example plugin to create custom routes on our custom plugin but when I am calling a route using Postman I am getting error FileNotFoundException

{
"ResponseStatus": {
"ErrorCode": "FileNotFoundException",
"Message": "Could not load file or assembly 'REST API, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.",
"StackTrace": "[WarehouseDto: 6/2/2023 11:29:58 AM]:\n[REQUEST: {}]\nSystem.IO.FileNotFoundException: Could not load file or assembly 'REST API, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.\r\nFile name: 'REST API, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'\r\n at CustomEndpoints.<Post>d__0.MoveNext()\r\n at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.Start[TStateMachine](TStateMachine& stateMachine)\r\n at CustomEndpoints.Post(WarehouseDto WarehouseDto)\r\n at lambda_method(Closure , Object , Object )\r\n at ServiceStack.Host.ServiceRunner`1.<ExecuteAsync>d__13.MoveNext()\r\n\r\n=== Pre-bind state information ===\r\nLOG: DisplayName = REST API, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null\n (Fully-specified)\r\nLOG: Appbase = file:///C:/Program Files (x86)/Jiwa Financials/Jiwa 7/\r\nLOG: Initial PrivatePath = NULL\r\nCalling assembly : (Unknown).\r\n===\r\nLOG: This bind starts in default load context.\r\nLOG: Using application configuration file: C:\\Program Files (x86)\\Jiwa Financials\\Jiwa 7\\JiwaAPISelfHostedService.exe.Config\r\nLOG: Using host configuration file: \r\nLOG: Using machine configuration file from C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\config\\machine.config.\r\nLOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).\r\nLOG: The same bind was seen before, and was failed with hr = 0x80070002.\r\n",
"Errors": []
}
}
iamgurney
I'm new here
I'm new here
 
Posts: 6
Joined: Tue May 30, 2023 7:15 am

Re: Endpoint Returning FileNotFound for Rest API Plugin  Topic is solved

Postby SBarnes » Sat Jun 03, 2023 12:45 pm

You would really need to post the actual plugin to be able to fully diagnose what is causing this but two things I can suggest are

1. Make sure the execution order value on you plugin is set to a higher value than the REST api plugin

2. You may need to add an assembly resolver to your plugin if you haven't already got one the code below tells you what to do, note this assembly resolver has logging code to let you know what its doing which you could remove.

In the configure method add the following code

Code: Select all
AppDomain.CurrentDomain.AssemblyResolve += delegate(object sender, System.ResolveEventArgs args) { return AssemblyResolve(sender, args, Plugin); };   
Plugin.PluginReferenceCollection.Read();


in the plugin itself add the following code

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)
      {

         // 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);

         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.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)
            {

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

         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;
         

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

Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1619
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175


Return to Technical and or Programming

Who is online

Users browsing this forum: No registered users and 15 guests

cron