简体   繁体   English

在运行时找不到方法

[英]Method not found on runtime

I have an ASP.Net c# project trying to access methods in a class in another project.我有一个 ASP.Net c# 项目试图访问另一个项目中的类中的方法。 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重新创建.sln文件
  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.看来错误出在我的主项目中,它调用了另一个项目中的方法。

"Method not found" is a very specific error, which means a method it expected (ie 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.检查部署到 Web 服务器的 dll 与您认为它们应该是什么。

I had the same issue.我遇到过同样的问题。 In my case, it was caused by the addition of an optional argument .就我而言,它是由添加一个可选参数引起的。 So first you would have:所以首先你会有:

referencingAssembly:参考Assembly:

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:参考Assembly:

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.在本地,这将构建并运行良好,因为新构建的引用Assembly.dll 将引用 DoStuff(string, string, string) 方法。 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.但是当您只部署更改后的 referencedAssembly(思考:添加的参数是可选的,并且referncingAssembly 仍然有效)时,旧版本的referencingAssembly 将抛出一个MethodNotFound,因为它寻找具有签名DoStuff(string, string) 的方法,即由于我们添加了额外的(可选)参数,因此不再存在于 referencedAssembly 中。


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)).或者部署新构建的referencingAssembly(将引用具有签名DoStuff(string, string, string)的方法)。

有同样的问题,在我的情况下,在 webconfig 中将 optimizeCompilations 设置为 false 解决了这个问题

Two projects in your solution : project A and project B. Both projects use the nuget package "DoStuff", but different versions of the DoStuff package:您的解决方案中有两个项目:项目 A 和项目 B。两个项目都使用 nuget 包“DoStuff”,但 DoStuff 包的版本不同:

  • project A references version 1.1 of DoStuff.项目 A 引用了 DoStuff 的 1.1 版。
  • project B references version 1.0 of DoStuff.项目 B 引用了 DoStuff 1.0 版。
  • project B references project A.项目 B 引用项目 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. 1.1版本有一个新方法,在项目A中使用。当项目B使用项目A时,你会得到这个MethodNotFoundException,因为项目B的DoStuff版本不知道项目A在说什么。

To prevent this, we have a unit test in place that fails if projects are using different versions of the same nuget package.为了防止这种情况,我们有一个单元测试,如果项目使用同一 nuget 包的不同版本,则该单元测试会失败。 A relatively simple doorstop that has saved us a couple of headaches in the past years.一个相对简单的门挡,在过去几年里为我们省去了一些麻烦。

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.我清空了 bin 和 debug 文件夹,然后再次尝试构建项目。 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.但是,当然,发布一部分代码可能会更有帮助。

I had a very similar problem and found that an older version of the assembly had been installed to the GAC.我有一个非常相似的问题,发现旧版本的程序集已安装到 GAC。 After I uninstalled that version, the project compiled and ran correctly.卸载该版本后,项目编译并正确运行。 So check the GAC ( C:\\Windows\\Assembly ) to be sure it's not listed there.因此,请检查 GAC ( C:\\Windows\\Assembly ) 以确保它没有列在那里。

I encountered this same exception too, though my problem was different, I had a method in one of my library originally looking like this:我也遇到了同样的异常,虽然我的问题有所不同,但我的一个库中有一个最初看起来像这样的方法:

public void WriteMessage(string msg) 
{
    ...
}

and there were some new request for modification and it turned into this:并且有一些新的修改请求,它变成了这样:

public void WriteMessage(string msg, int code = 100)
{
    ...
}

after that, I updated the project that is using this library with newly compiled binary, it started to throw this exception.之后,我用新编译的二进制文件更新了使用这个库的项目,它开始抛出这个异常。

After many attempts, nothing worked, even project clean, rebuild, removing and re-adding the reference, then I tried to modify the project's method call from:经过多次尝试,没有任何效果,甚至项目清理,重建,删除和重新添加引用,然后我尝试修改项目的方法调用:

...
library.WriteMessage('hello!');
...

to:到:

...
library.WriteMessage('hello!', 100);
...

and then compiled the project, it solved the problem, after that I changed it back to:然后编译项目,问题解决了,之后我改回:

...
library.WriteMessage('hello!');
...

and now it magically fixed everything, maybe there were some kind of cached metadata left somewhere that isn't being updated, and by changing the way the method is called somehow clearly remind it that the method's signature is different, yet not so different.现在它神奇地修复了一切,也许有某种缓存的元数据留在某处没有被更新,并且通过改变方法的调用方式以某种方式清楚地提醒它该方法的签名是不同的,但不是那么不同。

Hope this can help someone facing the same problem that I experienced.希望这可以帮助面临与我遇到的相同问题的人。

Look, this must be the weirdest cause ever, but after a whole morning trying everything, rebuilding the project from scratch, etc, I noticed that the single only thing that would avoid the problem was if my library had a different assembly name.看,这一定是有史以来最奇怪的原因,但是经过一整个上午的尝试,从头开始重建项目等,我注意到唯一可以避免问题的方法是我的库是否具有不同的程序集名称。

I had a ghastly suggestion from the back of my mind that I had installed previous dll versions in the GAC, but I already checked that and besides, it was never my intention.我脑子里有一个可怕的建议,我在 GAC 中安装了以前的 dll 版本,但我已经检查过了,此外,这从来不是我的本意。 But following that trail I found out this dll (and all the others from the project !) where installed in C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\Common7\\IDE !!!!!!但是按照这条线索,我发现这个 dll(以及项目中的所有其他文件!)安装在 C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\Common7\\IDE 中!!!!!!

Aaaargh !啊! Guess I'm the only one that somehow messed this up, but I have no clue why, I just thought I could spare someone else this frustation in the remote possibility that someone is so unlucky !!猜猜我是唯一一个以某种方式搞砸了这件事的人,但我不知道为什么,我只是想我可以在某人如此不幸的可能性很小的情况下让其他人免于这种沮丧!

I encountered the same issue when i was running an application with nuget package managed dlls.当我使用 nuget 包管理的 dll 运行应用程序时,我遇到了同样的问题。 It turns out when I try to update the dlls from nuget package manager, it did not update my test layer.事实证明,当我尝试从 nuget 包管理器更新 dll 时,它没有更新我的测试层。 I suggest you to check the version of dlls you are referring to.我建议您检查您所指的 dll 版本。 Go to ObjectBrower in VS and check on the dlls and check the referring location: make sure you are referring to the latest dlls.转到 VS 中的 ObjectBrower 并检查 dll 并检查引用位置:确保您引用的是最新的 dll。

I had the same problem, then I have changed the method name into a different name, and changed the references .我遇到了同样的问题,然后我将方法名称更改为不同的名称,并更改了引用。 then it works great.那么它工作得很好。 I dont know what was the problem but it looks like there is a bug when changing a method name in some circumstances is not applied to the whole system我不知道是什么问题,但在某些情况下更改方法名称时似乎存在错误不适用于整个系统

My case was a little variation of the other ones here ;我的情况与这里的其他情况略有不同; but it might help someone.但它可能会帮助某人。 I just added an optional argument in a library and got that message from the GUI using the library.我刚刚在库中添加了一个可选参数,并使用该库从 GUI 中获取了该消息。

The problem was caused by the GUI and one of its libraries referencing the same library but one was not up to date.该问题是由 GUI 及其引用同一库的库之一引起的,但其中一个库不是最新的。

I have我有

  • GUI referencing LibA and LibZ GUI 引用 LibA 和 LibZ
  • LibA referencing LibZ LibA 引用 LibZ

So I just removed all "bin" and "obj" folders of both LibA and GUI, made sure to have the updated DLL of LibZ in both, and everything worked again.所以我只是删除了 LibA 和 GUI 的所有“bin”和“obj”文件夹,确保在两者中都有 LibZ 的更新 DLL,然后一切又正常了。

如果您将另一个项目引用为.exe ,则必须将其重建为.dll ,为此您必须将引用的项目设置为class library到项目设置中,然后构建它并将 dll 引用到项目中,方法是失踪。

The error also occurred when I had an Action as parameter.当我将 Action 作为参数时,也会发生该错误。 I passed a method without wrapping it in a new Action(..).我传递了一个方法而没有将它包装在一个新的 Action(..) 中。

DLL had this: DLL 有这个:

public bool ShowMyForm(bool showConnectionWindow, Action onCloseNotify) {...}

Test app called the DLL like so:测试应用程序像这样调用 DLL:

private void onFormClose()
{
    MessageBox.Show("Form Closed");
}

private void ShowForm()
{
     mydll.ShowMyForm(true, onFormClose ); // bug here: wrap in Action
}

When I changed the call to当我将电话更改为

mydll.ShowMyForm(true, new Action(onFormClose) );

the exception disappeared.异常消失了。 The exception text is just misleading - the method is there, but the type of the parameter gave a runtime exception.异常文本只是误导 - 方法在那里,但参数的类型给出了运行时异常。 Too bad it is not picked up at compile time, though.太糟糕了,它在编译时没有被选中,但是。

In my case I deployed to a .NET 3.0 PC (Windows XP) while the compile target has been .NET 3.5.就我而言,我部署到 .NET 3.0 PC (Windows XP),而编译目标是 .NET 3.5。 I really wonder why there is not a more basic warning...我真的想知道为什么没有更基本的警告......

Problem has been usage of DataContract from System.Runtime.Serialisation.问题是 System.Runtime.Serialisation 中 DataContract 的使用。

I was faced the same issue, even i cleaned and rebuilded the solution not helped out.我遇到了同样的问题,即使我清理并重建了解决方案也无济于事。 when i published the application, old version of dll was in bin folder.当我发布应用程序时,旧版本的 dll 位于 bin 文件夹中。 So I changed the assembly version in assemblyinfo file.所以我在 assemblyinfo 文件中更改了程序集版本。 finally its working.最后它的工作。 New version is available in bin folder.新版本在 bin 文件夹中可用。

I had this problem when I had copied the solution to a different location on another machine.当我将解决方案复制到另一台机器上的不同位置时,我遇到了这个问题。 The Build folder just needed to be changed.只需要更改 Build 文件夹。 Don't ask me why but the solution was built in the build folder (we'll call this folder A) then an old copy was copied from folder B to folder C. At runtime it couldn't find my latest code because it was looking at an older version in folder C. In Properties for the solution in the Build tab, I changed the Output Path to folder B. It then built the latest version in folder B which it copied to folder C and it all worked again.不要问我为什么,但解决方案是在 build 文件夹中构建的(我们称此文件夹为 A),然后将旧副本从文件夹 B 复制到文件夹 C。在运行时它找不到我的最新代码,因为它是查看文件夹 C 中的旧版本。在“构建”选项卡中解决方案的“属性”中,我将输出路径更改为文件夹 B。然后它在文件夹 B 中构建了最新版本,并将其复制到文件夹 C 中,然后一切正常。

The bit I don't know is why we have folder C in the first place.我不知道的是为什么我们首先有文件夹 C。 I have a codedUI Testing solution and folder C is "C:\\Users\\\\Documents\\MyCodedUISolution\\TestResults\\_ 2017-04-20 11_31_29\\Out".我有一个 codedUI 测试解决方案,文件夹 C 是“C:\\Users\\\\Documents\\MyCodedUISolution\\TestResults\\_ 2017-04-20 11_31_29\\Out”。 What this is used for and why it's created, I don't know but if old code is copied here because you change the Output Path or you move the solution without changing the Output Path to what's needed then you're in trouble.这用于什么以及为什么创建它,我不知道,但如果旧代码被复制到这里,因为您更改了输出路径,或者您移动了解决方案而不将输出路径更改为所需的内容,那么您就有麻烦了。

In my case I had changed the return type of a method from IQueryable to IEnumerable.就我而言,我已将方法的返回类型从 IQueryable 更改为 IEnumerable。

I had recompiled and uploaded the DLL containing the method but not the DLL with code which calls the method.我重新编译并上传了包含该方法的 DLL,但没有上传包含调用该方法的代码的 DLL。 So the calling DLL was still expecting the method with the previous return type and couldn't find it.所以调用 DLL 仍然期待具有先前返回类型的方法并且找不到它。

I was not able to fix it with any of the suggested answers.我无法使用任何建议的答案修复它。 I could not find any old versions of the custom assembly.我找不到任何旧版本的自定义程序集。 I literally searched everywhere with the tool Everything.我真的用工具Everything到处搜索。 In debug mode it showed the right version of the assembly but it still could not find the method.在调试模式下,它显示了正确版本的程序集,但仍然找不到该方法。

Enabling Automatic Binding in the .csproj file fixed it for me.csproj文件中启用自动绑定为我修复了它

<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>

I was targeting a package of .NET Framework 4.5 in my project.我的目标是在我的项目中使用 .NET Framework 4.5 包。 The above setting is not needed if all packages in the project target .NET Framework 4.5.1 or higher.如果项目中的所有包都面向 .NET Framework 4.5.1 或更高版本,则不需要上述设置。

It may be a late response: But one possible remedy is to clean up Temporary ASP.NET files folder: Ie, C:\\Windows\\Microsoft.NET\\Framework\\vX.X.XXXXX\\Temporary ASP.NET Files这可能是一个迟到的响应:但一种可能的补救措施是清理 Temporary ASP.NET files 文件夹:即 C:\\Windows\\Microsoft.NET\\Framework\\vX.X.XXXXX\\Temporary ASP.NET Files

Deleting the folders related to your website, and rebuild the solution.删除与您的网站相关的文件夹,并重建解决方案。

In addition to Nirman's answer - don't forget clear temp files in C:\\Users\\UserName\\AppData\\Local\\Temp\\Temporary ASP.NET Files location.除了 Nirman 的回答 - 不要忘记清除C:\\Users\\UserName\\AppData\\Local\\Temp\\Temporary ASP.NET Files位置中的C:\\Users\\UserName\\AppData\\Local\\Temp\\Temporary ASP.NET Files

I changed class field type from enum to string and after that got error我将类字段类型从枚举更改为字符串,然后出现错误

System.MissingMethodException: Method not found: 'DatnesVeidi DatnesModel.get_DatnesVeids()' System.MissingMethodException:找不到方法:'DatnesVeidi DatnesModel.get_DatnesVeids()'

when rendering Razor view.渲染 Razor 视图时。 None of the previous suggestions helped, just clearing user local temp files.之前的建议都没有帮助,只是清除用户本地临时文件。

For others visiting this question in future:对于将来访问此问题的其他人:

I had this problem testing a replacement netstandard2.0 library that was being integrated with another project.我在测试与另一个项目集成的替换 netstandard2.0 库时遇到了这个问题。 A few other projects within the ecosystem also depended on this replacement library.生态系统中的其他一些项目也依赖于这个替代库。

When testing the new library in the system, the MissingMethodException occured.在系统中测试新库时,发生了MissingMethodException

In my case, we had changed implementations of a base class's abstract method in the new library but we hadn't rebuild the projects that were dependent on the new library.就我而言,我们已经更改了新库中基类抽象方法的实现,但我们没有重建依赖于新库的项目。

This must have caused the expected abstract implementation of the method to be missing at runtime.这一定会导致该方法的预期抽象实现在运行时丢失。

It could help to ensure that all dependencies are being rebuilt.它可以帮助确保所有依赖项都被重建。

I know this is a very old question but maybe this will help out somebody.我知道这是一个非常古老的问题,但也许这会对某人有所帮助。

I had the exactly same issue.我有完全相同的问题。 Fixing the problem was actually deleting all project files that I recently published from the web-server, cleaning and rebuilding the project and publishing it again.解决这个问题实际上是删除我最近从网络服务器发布的所有项目文件,清理和重建项目并再次发布它。

After doing that I got the exception 'The file has not been pre-compiled and cannot be requested'.这样做之后,我得到了异常“文件尚未预编译,无法请求”。 Turned out that I had a assembly missing.原来我缺少一个程序集。

The error message is definetly very misleading and it took me 3 days to fix that issue.错误消息无疑是非常具有误导性的,我花了 3 天时间来解决该问题。

Little late to the party.晚会有点晚。

Check the contents of the GAC.检查 GAC 的内容。

If your .dll is already in there then you need to remove it.如果您的 .dll 已经在那里,那么您需要将其删除。

Use the developer command prompt run as admin and type使用开发人员命令提示符以管理员身份运行并键入

gacutil -u {name of your dll} gacutil -u {你的dll名称}

I just had this issue:我刚刚遇到了这个问题:

Project X is a library used by other projects, it contains dll Y. Project A contains an older version of dll Y, and project A calls code in project X that is from dll Y. Project X 是其他项目使用的库,它包含 dll Y。 Project A 包含旧版本的 dll Y,项目 A 调用项目 X 中来自 dll Y 的代码。

The older version of the dll was used when calling code from that dll.从该 dll 调用代码时使用了旧版本的 dll。 Whereas other projects that did not have dll Y referenced, just used the up to date dll Y in project X.而其他没有引用 dll Y 的项目,只是在项目 X 中使用了最新的 dll Y。

Make sure you update the dll/nuget package in all projects that contain it.确保在包含它的所有项目中更新 dll/nuget 包。

I'm posting the answer in the hope of helping someone that got stucked like I was.我发布了答案,希望能帮助像我一样被困住的人。
I had the following error:我有以下错误:

Method not found: 'Microsoft.Scripting.Hosting.ScriptEngine IronPython.Hosting.Python.CreateEngine()

I tried may on the suggested solution, including (but not limited to) using gacutil.exe /u IronPython developer console ad admin, but nothing worked for me except the following:我尝试了建议的解决方案,包括(但不限于)使用gacutil.exe /u IronPython开发人员控制台广告管理员,但除了以下内容外,对我没有任何帮助:

1- Going to C:\Windows\Microsoft.NET\ 1- 转到 C:\Windows\Microsoft.NET\
2- Search for "IronPython" 2- 搜索“IronPython”
3- Delete every folder I found, in my case was: 3- 删除我找到的每个文件夹,在我的例子中是:
C:\Windows\Microsoft.NET\assembly\GAC_MSIL\IronPython
C:\Windows\Microsoft.NET\assembly\GAC_MSIL\IronPython.stdlib

In my case I manually updated Nuget packages without updating their version numbers.在我的例子中,我手动更新了 Nuget 包而不更新它们的版本号。 The packages were loaded from my local cache, even if I completely reinstalled the packages, because the version numbers were not updated.这些包是从我的本地缓存加载的,即使我完全重新安装了这些包,因为版本号没有更新。 Once I incremented the version numbers of my Nuget packages, the method could be found.一旦我增加了 Nuget 包的版本号,就可以找到该方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运行时找不到方法错误 - Method Not Found Error at runtime 在运行时未找到方法HttpContentExtensions.ReadAsAsync - Method HttpContentExtensions.ReadAsAsync not found runtime 获取异常找不到方法:&#39;System.Runtime.CompilerServices.CallSiteBinder - Getting exception Method not found: 'System.Runtime.CompilerServices.CallSiteBinder 没有使用反射的运行时C#“找不到方法”异常 - C# “Method not found” exception on runtime without usage of reflection 找不到运行时抛出方法-引用的程序集版本不匹配 - Runtime throws method not found - referenced assembly version mismatch 在运行时找不到引用的库 - Referenced Library not found at runtime 找不到方法:&#39;System.Type System.Runtime.InteropServices.Marshal.GetTypeFromCLSID(System.Guid)&#39; - Method not found: 'System.Type System.Runtime.InteropServices.Marshal.GetTypeFromCLSID(System.Guid)' 我收到错误 Method not found: 'Void System.Runtime.InteropServices.Marshal.StructureToPtr(,,0, IntPtr, Boolean)' - I'm getting error Method not found: 'Void System.Runtime.InteropServices.Marshal.StructureToPtr(!!0, IntPtr, Boolean)' 我如何 app.Invoke 具有多个参数的后门方法? 在运行时我得到 System.Exception:“没有找到这样的方法” - How do I app.Invoke backdoor method with multiple parameters? At runtime I'm getting System.Exception: “No such method found” 在运行时更改方法属性 - Changing method Attributes at runtime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM