Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method not found on runtime

I have an ASP.Net c# project trying to access methods in a class in another project. It's working for first half of methods in the class but not for the other half of the methods in the class which I recently added. They compile, but they throw a method not found exception at run-time.

Does anyone have any ideas I could try? I've tried:

  1. recreating the .sln file
  2. Subbing in another class library project, which I know works. It appears that the error is in my main project that calls the method in the other project.
like image 861
williamsandonz Avatar asked Sep 28 '11 05:09

williamsandonz


5 Answers

"Method not found" is a very specific error, which means a method it expected (i.e. was there at compile time) simply is not present. This usually means that the files you are deploying are different to what you think they are - specifically, I would wager that you are deploying the old version of the library (which lacks your additions).

Check the dlls deployed to the web-server against what you think they should be.

like image 125
Marc Gravell Avatar answered Nov 09 '22 10:11

Marc Gravell


I had the same issue. In my case, it was caused by the addition of an optional argument. So first you would have:

referencingAssembly:

referencedAssembly.DoStuff(firstArgument, secondArgument)

referencedAssembly:

public void DoStuff(string firstArgument, string secondArgument)
{
   //stuff to do
}

Then you add an optional parameter to the method, but you don't provide that argument while calling it.

referencingAssembly:

referencedAssembly.DoStuff(firstArgument, secondArgument)//unchanged

referencedAssembly:

public void DoStuff(string firstArgument, string secondArgument, string thirdArgument = "default value")
{
   //stuff to do
}

Locally, this will build and run fine since the newly build referencingAssembly.dll will have a reference to the DoStuff(string, string, string) method. But when you only deploy the changed referencedAssembly (thinking: the added argument was optional, and the referncingAssembly still works), the old version of referencingAssembly will throw a MethodNotFound since it seeks a method with the signature DoStuff(string, string), which is no longer present in the referencedAssembly since we added the extra (optional) argument.


A possible solution could be an overload:

referencedAssembly:

public void DoStuff(string firstArgument, string secondArgument)//original method
{
   DoStuff(firstArgument, secondArgument, "default value")
}
public void DoStuff(string firstArgument, string secondArgument, string thirdArgument)//new overload of the method
{
//stuff to do
}

Or deploying the newly build referencingAssembly (which will refer to the method with signature DoStuff(string, string, string)).

like image 43
nozem Avatar answered Nov 09 '22 10:11

nozem


Two projects in your solution : project A and project B. Both projects use the nuget package "DoStuff", but different versions of the DoStuff package:

  • project A references version 1.1 of DoStuff.
  • project B references version 1.0 of DoStuff.
  • project B references project A.

version 1.1 has a new method, that is used in project A. when project B uses project A, you'll get this MethodNotFoundException, because project B's version of DoStuff doesn't know what project A is talking about.

To prevent this, we have a unit test in place that fails if projects are using different versions of the same nuget package. A relatively simple doorstop that has saved us a couple of headaches in the past years.

like image 34
increddibelly Avatar answered Nov 09 '22 10:11

increddibelly


I have faced this kind of problem but was able to solve it. I emptied my bin and debug folder and tried building the project again. it worked, at least for me. Or try to clean the solution and try rebuilding it. But of course, posting a part of your code could be more helpful.

like image 35
Sandy Avatar answered Nov 09 '22 10:11

Sandy


had the same problem, in my case setting the optimizeCompilations to false in the webconfig solved the problem

like image 9
Sebastien H. Avatar answered Nov 09 '22 10:11

Sebastien H.