简体   繁体   English

C#Web Api / MVC.net未正确加载程序集

[英]C# Web Api/ MVC.net not loading assemblies properly

I'm new to C# and .net in general, and I need to use it to work with the SDK for a major piece of software we use. 我是C#和.net的新手,我需要使用它来使用SDK来处理我们使用的主要软件。

I can add the assemblies they tell me to add in a console application and everything works fine. 我可以添加他们告诉我在控制台应用程序中添加的程序集,一切正常。 When I try to add them to any web application (either MVC or WebAPI), I keep getting errors saying other assemblies are missing. 当我尝试将它们添加到任何Web应用程序(MVC或WebAPI)时,我不断收到错误消息,说明缺少其他程序集。 Eventually I just added every assembly/reference (not sure the correct term for this, pretty much just .dll files) in the sdk folder and now it is working. 最后我刚刚在sdk文件夹中添加了每个程序集/引用(不​​确定正确的术语,几乎只是.dll文件),现在它正在工作。 This definitely cannot be the correct way to do this. 这绝对不是正确的方法。

Is there something I am missing that allows assemblies/references to load other assemblies/references, or is something else the cause of my issue? 是否有我遗漏的东西允许程序集/引用加载其他程序集/引用,或者是我的问题的其他原因?

UPDATE: 更新:

The exceptions are usually something like this: 例外通常是这样的:

Could not load file or assembly 'Server, Version=1.5.1.0, Culture=neutral, PublicKeyToken=d11ef57bba4acf91' or one of its dependencies. The system cannot find the file specified. 

And it does not occur during build, only when the function is actually being called from the web application( in my case, since it's an api, when I visit the URL that returns my JSON data) 并且它不会在构建期间发生,只有当实际从Web应用程序调用该函数时(在我的情况下,因为它是api,当我访问返回我的JSON数据的URL时)

You can use a tool called "IL Disassembler" that is installed along with Visual Studio to see what other dependencies the SDK's Assembly depends on. 您可以使用与Visual Studio一起安装的名为“IL Disassembler”的工具来查看SDK程序集所依赖的其他依赖项。

The install location may vary but mine is here... "C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v10.0A\\bin\\NETFX 4.6 Tools\\ildasm.exe" 安装位置可能会有所不同,但我的在这里......“C:\\ Program Files(x86)\\ Microsoft SDKs \\ Windows \\ v10.0A \\ bin \\ NETFX 4.6 Tools \\ ildasm.exe”

Drag and drop the SDK's assembly that you are using onto the tool and click the manifest field. 将您正在使用的SDK程序集拖放到该工具上,然后单击清单字段。 A dialog will open with the other assemblies that you'll need to include. 将打开一个对话框,其中包含您需要包含的其他程序集。

在此输入图像描述

If your console application works but the web application does not then the exception should specify which assembly is missing. 如果您的控制台应用程序工作但Web应用程序没有,那么异常应该指定缺少哪个程序集。

Compare you console output directory with the output directory of you web build. 将控制台输出目录与Web构建的输出目录进行比较。 Make sure the dll that is required is being deployed to the web directory you are running. 确保将所需的dll部署到正在运行的Web目录中。

In visual studio with your web project find the assembly under references and select it. 在带有Web项目的visual studio中,查找引用下的程序集并选择它。 Make sure that the property "Copy Local" is set to TRUE. 确保“Copy Local”属性设置为TRUE。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM