简体   繁体   English

从Matlab部署dll的C#.net Web应用程序

[英]C# .net web application with dll deployed from matlab

I searched the similar problem on the website which seems a 32/64 bit thing. 我在网站上搜索了类似的问题,这似乎是32/64位的问题。 But I still can't figure it out even by the method provided such as toggling between 86/64 target platform 但是,即使通过提供的方法(例如在86/64目标平台之间切换),我仍然无法弄清楚

The software used here are Visual Studio 2010 and Matlab 2015a. 此处使用的软件是Visual Studio 2010和Matlab 2015a。 I am testing on a very simple Matlab code in a single .m file which is supposed to plot a sine curve in a figure. 我正在单个.m文件中的一个非常简单的Matlab代码上进行测试,该文件应该在图中绘制正弦曲线。 There is no input or output for this program: 该程序没有输入或输出:

function [] = testPlot()
x = 0:pi/100:2*pi;
y = sin(x);
%Create a line plot of the data.

figure % opens new figure window
plot(x,y)

It is deployed to dll named testPlotN using matlab NE builder (I guess the dll should be 64bit), which is planned to use in ac# web project. 使用Matlab NE Builder将其部署到名为testPlotN的dll中(我猜该dll应该为64bit),计划在ac#Web项目中使用。 I can successfully test it on ac# console application (no matter the target platform is x86 or x64 seemingly) 我可以在ac#控制台应用程序上成功测试它(无论目标平台看似是x86还是x64)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MathWorks.MATLAB.NET.Arrays;
using MathWorks.MATLAB.NET.Utility;
using testPlotN; //dll

namespace ConsoleApplication1

{
    class Program
    {
        static void Main(string[] args)
        {
            testP obj = new testP();//class from dll
            obj.testPlot();//matlab function
            Console.ReadKey();
        }
    }
}

However when I test it in ac# web application, with the main file WebForm1.aspx 但是,当我在ac#Web应用程序中使用主文件WebForm1.aspx对其进行测试时

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        hello my name is<asp:TextBox ID="TextBox1" runat="server" 
            ontextchanged="TextBox1_TextChanged"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="OK" />
        <br />
        <br />
        <br />
        <asp:Label ID="lbloutput" runat="server"></asp:Label>
        <br />

    </div>
    </form>

</body>
</html>

And on click call for the button is in the file WebForm1.aspx.cs 在单击时,按钮调用位于文件WebForm1.aspx.cs中

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading.Tasks;
using MathWorks.MATLAB.NET.Arrays;
using MathWorks.MATLAB.NET.Utility;
using testPlotN;

namespace WebApplication2
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {       
            testP obj = new testP();
            obj.testPlot();
        }
    }
}

When I use choose any cpu by default as platform target when building, it shows the page perfectly without any problem 当我在构建时使用默认情况下选择任何cpu作为平台目标时,它将完美显示页面,而没有任何问题

1 1个

But when I click the button, the error came out 但是当我点击按钮时,错误出现了

Server Error in '/' Application.
--------------------------------------------------------------------------------


 An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B) 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

Source Error: 






Line 36:             testP obj = new testP();
Line 37:             obj.testPlot();
Line 38:         }

Source File: C:\Users\M\Documents\Visual Studio 2010\Projects\WebApplication2\WebApplication2\WebForm1.aspx.cs    Line: 36 

Stack Trace: 





[BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)]
   MathWorks.MATLAB.NET.Arrays.MWArray.mclmcrInitialize2(Int32 primaryMode) +0
   MathWorks.MATLAB.NET.Arrays.MWArray..cctor() +553

[TypeInitializationException: The type initializer for 'MathWorks.MATLAB.NET.Arrays.MWArray' threw an exception.]
   MathWorks.MATLAB.NET.Utility.MWMCR..cctor() +1864

[TypeInitializationException: The type initializer for 'MathWorks.MATLAB.NET.Utility.MWMCR' threw an exception.]
   testPlotN.testP..cctor() +97

[TypeInitializationException: The type initializer for 'testPlotN.testP' threw an exception.]
   testPlotN.testP..ctor() +0
   WebApplication2.WebForm1.Button1_Click(Object sender, EventArgs e) in C:\Users\M\Documents\Visual Studio 2010\Projects\WebApplication2\WebApplication2\WebForm1.aspx.cs:36
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9659822
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +108
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1639

While I turned to x64 target platform, the first page can not even show up! 当我转向x64目标平台时,第一页甚至无法显示! And the error message appeared immediately 并且错误消息立即出现

Server Error in '/' Application.
--------------------------------------------------------------------------------


 Could not load file or assembly 'WebApplication2' or one of its dependencies. An attempt was made to load a program with an incorrect format. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.BadImageFormatException: Could not load file or assembly 'WebApplication2' or one of its dependencies. An attempt was made to load a program with an incorrect format.

Source Error: 




An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.


Assembly Load Trace: The following information can be helpful to determine why the assembly 'WebApplication2' could not be loaded.





=== Pre-bind state information ===
LOG: DisplayName = WebApplication2
 (Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: WebApplication2 | Domain ID: 5
WRN: A partial bind occurs when only part of the assembly display name is provided.
WRN: This might result in the binder loading an incorrect assembly.
WRN: It is recommended to provide a fully specified textual identity for the assembly,
WRN: that consists of the simple name, version, culture, and public key token.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.
LOG: Appbase = file:///C:/Users/M/Documents/Visual Studio 2010/Projects/WebApplication2/WebApplication2/
LOG: Initial PrivatePath = C:\Users\M\Documents\Visual Studio 2010\Projects\WebApplication2\WebApplication2\bin
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Users\M\Documents\Visual Studio 2010\Projects\WebApplication2\WebApplication2\web.config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Users/M/AppData/Local/Temp/Temporary ASP.NET Files/root/3bb260af/e3ca09a0/WebApplication2.DLL.
LOG: Attempting download of new URL file:///C:/Users/M/AppData/Local/Temp/Temporary ASP.NET Files/root/3bb260af/e3ca09a0/WebApplication2/WebApplication2.DLL.
LOG: Attempting download of new URL file:///C:/Users/M/Documents/Visual Studio 2010/Projects/WebApplication2/WebApplication2/bin/WebApplication2.DLL.
ERR: Failed to complete setup of assembly (hr = 0x8007000b). Probing terminated.


Stack Trace: 





[BadImageFormatException: Could not load file or assembly 'WebApplication2' or one of its dependencies. An attempt was made to load a program with an incorrect format.]
   System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +0
   System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +36
   System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +152
   System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection) +77
   System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +21
   System.Reflection.Assembly.Load(String assemblyString) +28
   System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +38

[ConfigurationErrorsException: Could not load file or assembly 'WebApplication2' or one of its dependencies. An attempt was made to load a program with an incorrect format.]
   System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +738
   System.Web.Configuration.CompilationSection.LoadAllAssembliesFromAppDomainBinDirectory() +217
   System.Web.Configuration.CompilationSection.LoadAssembly(AssemblyInfo ai) +130
   System.Web.Compilation.BuildManager.GetReferencedAssemblies(CompilationSection compConfig) +170
   System.Web.Compilation.BuildManager.GetPreStartInitMethodsFromReferencedAssemblies() +92
   System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +290
   System.Web.Compilation.BuildManager.ExecutePreAppStart() +157
   System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +531

[HttpException (0x80004005): Could not load file or assembly 'WebApplication2' or one of its dependencies. An attempt was made to load a program with an incorrect format.]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9920756
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101
   System.Web.HttpRuntime.ProcessRequestInternal(HttpWorkerRequest wr) +456



--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.6.81.0 

I have spent two days to debug this. 我花了两天时间来调试它。 But I still can't figure it out even by the method provided such as toggling between 86/64 target platform and setting of iis to enable 32 bit application. 但是,即使通过所提供的方法(例如在86/64目标平台之间切换以及设置iis以启用32位应用程序),我仍然无法弄清楚。

Any clue would be much appreciated! 任何线索将不胜感激!

the problem was resolved in my case by enabling Use the 64 bit version of IIS Express for websites and projects 通过启用对网站和项目使用IIS Express的64位版本,就我而言,此问题已解决

click on tools --> Options --> Projects and Solutions --> Web Projects 单击工具->选项->项目和解决方案-> Web项目

Click on the check box to enable Use the 64 bit version of IIS Express for websites and projects 单击复选框以启用对网站和项目使用IIS Express的64位版本

This is likely to do with the version of .NET that you are using and what the DLL was built in. Try adding the following to the configuration file (Web.Config): 这可能与您所使用的.NET版本以及DLL的内置版本有关。请尝试将以下内容添加到配置文件(Web.Config):

<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0"/>
    <supportedRuntime version="v2.0.50727"/>
  </startup>
</configuration>

Also, the "Managed Pipeline" should be set to Classic in IIS 另外,“托管管道”应在IIS中设置为“经典”

I had this same problem with my MatLab to .NET code. 我的MatLab到.NET代码也遇到了同样的问题。 Here are two things to look at: 这里有两件事要看:

  1. In Visual Studio, under Project Properties --> Build, set the Platform Target to x64. 在Visual Studio中,在“项目属性->生成”下,将“平台目标”设置为x64。 This will get it to work for testing in IIS Express. 这样可以使其在IIS Express中进行测试。

  2. When you deploy, make sure your application pool is set to 64bit only by changing Enable 32-Bit Applications to False. 部署时,仅通过将“启用32位应用程序”更改为“假”来确保将应用程序池设置为64位。

This worked for me when I had this problem. 当我遇到这个问题时,这对我有用。

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

相关问题 C#.NET DLL文件,并在.NET Web应用程序中使用它们 - C# .NET DLL files and using them in a .NET Web Application 如何将DLL引用添加到ASP.NET C#Web应用程序(该dll是基础c库的包装) - How to add a DLL reference to a ASP.NET C# web application (the dll is a wrapper for underlying c libraries) 从 C# .NET 应用程序调用 Delphi DLL - Calling a Delphi DLL from a C# .NET application 从一台计算机而不是另一台计算机部署时,C#Web应用程序在Azure中导致404错误 - C# web application causes 404 error in Azure when deployed from one computer, but not from another 将ac#asp.net Web应用程序与C ++ dll链接时,该dll代码是否在服务器或客户端上运行? - When linking a c# asp.net web application with a C++ dll is the dll code running on the server or the client? 强制Web应用程序使用某些dll版本C#asp.net - Force web application to use certain dll version C# asp.net 在C#.NET中使用matlab dll会引发异常 - matlab dll throws exception when used in C# .NET 从 ac# .net Web 应用程序生成 PDF/Excel 报告 - Generating PDF/Excel reports from a c# .net web application 从 C# 应用程序调用 ASP.net Web 服务 - Calling ASP.net Web Service from C# Application 如何从.NET / C#Web应用程序调用Google API - How to call Google API from .NET/C# Web Application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM