对象与引用COM +元素中的目标类型不匹配

如何解决对象与引用COM +元素中的目标类型不匹配

我使用C#MVC WEB API进行编码。当我引用COM +元素时,它会弹出异常和异常。消息是“对象与目标类型不匹配”。以下控制器代码是发生错误的地方:

using Newtonsoft.Json;
using JUDEMAP;
using System;
using System.Collections.Generic;
using System.EnterpriseServices;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace WebApplication1.Controllers
{
    public class APPLE0030Controller : ApiController
    {
        [HttpPost]        
        public object QueryAll()
        {
            string stat = "";
            appleBase.SQuery dObjOut = new appleBase.SQuery();
            object sysObj = null;
            try
            {
                stat = "1";
                sysObj = Activator.CreateInstance(Type.GetTypeFromProgID("JUDEMAP.GOGOMAP1",true));
                stat = "2";              
                object[] objArgs = new object[] { };
                stat = "3";
                dObjOut = (appleBase.SQuery)sysObj.GetType().InvokeMember("QueryAll",System.Reflection.BindingFlags.InvokeMethod,null,objArgs,null);//→bug is here
                stat = "4";
                dObjOut.state = "Success";
                dObjOut.msg = "Work";
                return dObjOut;
            }
            catch (Exception ex)
            {
                dObjOut.state = "Fail" + stat;
                dObjOut.msg = "Not work," + ex.Message;
            }
            finally
            {
                if (sysObj == null)
                {
                    stat = "5";
                    ServicedComponent.DisposeObject((ServicedComponent)sysObj);
                }
            }
            stat = "6";


            return dObjOut;
        }
    }
}

错误在这里:

dObjOut = (appleBase.SQuery)sysObj.GetType().InvokeMember("QueryAll",null);

我已经签出了COM +元素: 1.COM +元素属性设置正确。 a)检查组件COM-Visible。 b)检查COM互操作的注册。 c)在没有密码的情况下对组件进行签名成功。 d)COM +项目的AssemblyInfo.cs已经在下面添加代码

[assembly: ApplicationName("APPO4600")]       //'*setting COM+ element name 
[assembly: ApplicationActivation(ActivationOption.Server)]
[assembly: ApplicationAccessControl(false)]

COM +项目代码如下: GOGOMAP1.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.EnterpriseServices;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace JUDEMAP
{
    [ProgId("JUDEMAP.GOGOMAP1")]
    [ClassInterface(ClassInterfaceType.None)]
    [Transaction(TransactionOption.NotSupported),Synchronization(SynchronizationOption.Required),JustInTimeActivation(true),EventTrackingEnabled(true)]
    public class GOGOMAP1 : ServicedComponent
    {

        public appleBase.SQuery QueryAll()
        {
            appleBase.SQuery SST = new appleBase.SQuery();

            string Sql;
            
            try
            {
                using (SqlConnection conn = new SqlConnection(GetConnStr("ATTA")))
                {
                    Sql = "SELECT * FROM ATTA..HTTMAP";
                    SqlCommand cmd = new SqlCommand(Sql,conn);
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                    using (SqlDataAdapter adap = new SqlDataAdapter(cmd))
                    {
                        DataTable dt = new DataTable();
                        adap.Fill(dt);
                        List <appleBase.sQueryTable> Tiout = new List<appleBase.sQueryTable>();
                        if (dt.Rows.Count > 0)
                        {
                            foreach (DataRow dr in dt.Rows)
                            {
                                appleBase.sQueryTable sQueryTable = new appleBase.sQueryTable();
                                sQueryTable.RENT1 = dr[0] == null ? "" : dr[0].ToString();
                                sQueryTable.RENT2 = dr[1] == null ? "" : dr[1].ToString();
                                sQueryTable.RENT3 = dr[2] == null ? "" : dr[2].ToString();
                                sQueryTable.RENT4 = dr[3] == null ? "" : dr[3].ToString();
                                sQueryTable.RENT5 = dr[4] == null ? "" : dr[4].ToString();
                                sQueryTable.ASPID = dr[5] == null ? "" : dr[5].ToString();
                                sQueryTable.CLASSRAIN = dr[6] == null ? "" : dr[6].ToString();
                                sQueryTable.CHINESERAIN = dr[7] == null ? "" : dr[7].ToString();
                                sQueryTable.ASPLINK = dr[8] == null ? "" : dr[8].ToString();
                                sQueryTable.TARGET = dr[9] == null ? "" : dr[9].ToString();
                                sQueryTable.MAKER = dr[10] == null ? "" : dr[10].ToString();
                                sQueryTable.MAKEDATE = dr[11] == null ? "" : dr[11].ToString();
......
                                
                            }
                        }
                        SST.data = Tiout;
                        SST.state = "Success";
                        SST.msg = "查詢成功";
                    }
                }
            }
            catch (Exception ex)
            {
                SST.state = "Fail";
                SST.msg = "查詢失敗," + ex.Message.ToString();
            }

            return SST;
        }

        



        public string GetConnStr(string DBName)
        {
            StreamReader StrmRd = new StreamReader(@"C:\DtLk\" + DBName + ".ini");
            string Line = "";
            string ConnStr = "";
            try
            {
                do
                {
                    Line = StrmRd.ReadLine();
                    if (Line != "")
                        ConnStr += Line;
                }
                while (Line != null);
                return ConnStr;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }

            finally
            {
                StrmRd.Close();
                StrmRd.Dispose();
                StrmRd = null;
            }
        }



    }
}

COM +项目的模型如下

using System;
using System.Collections.Generic;
using System.EnterpriseServices;
using System.Runtime.InteropServices;

namespace JUDEMAP{
    [ComVisible(true)]
    [ProgId("JUDEMAP.appleBase")]
    [ClassInterface(ClassInterfaceType.None)]

    public class appleBase
    {

        [Serializable]
        public class InsertDataPara
        {
            public iQueryConditions CurQueryCondition;
        }


        [Serializable]
        public class iQueryConditions
        {
            public string RENT1;
            public string RENT2;
            public string RENT3;
            public string RENT4;
            public string RENT5;
            public string ASPID;
            public string CLASSRAIN;
            public string CHINESERAIN;
            public string ASPLINK;
            public string TARGET;
            public string MAKER;
            public string MAKEDATE;
            public string CHGER;
            public string CHGDATE;
            public string NEWMAP;
            public string APPLYDATE;
            public string APPLYCNT;
            public string XINQUIRY;
            public string XADDREW;
            public string XEDIT;
            public string XDEL;
            public string XDOWNLOAD;
            public string MK_M;
            public string APPLY_CYCLE;
            public string IT_WR;
            public string IconType;
        }

        [Serializable]
        public class InsertDataOut
        {
            public string state;
            public string msg;
            public List<InsertDataPara> data;
        }

        [Serializable]
        public class UpdateDataPara
        {
            public uQueryConditions CurQueryCondition;
        }

        [Serializable]
        public class uQueryConditions
        {
            public string RENT1;
            public string RENT2;
            public string RENT3;
            public string RENT4;
            public string RENT5;
            public string ASPID;
            public string CLASSRAIN;
            public string CHINESERAIN;
            public string ASPLINK;
            public string TARGET;
            public string MAKER;
            public string MAKEDATE;
            public string CHGER;
            public string CHGDATE;
            public string NEWMAP;
            public string APPLYDATE;
            public string APPLYCNT;
            public string XINQUIRY;
            public string XADDREW;
            public string XEDIT;
            public string XDEL;
            public string XDOWNLOAD;
            public string MK_M;
            public string APPLY_CYCLE;
            public string IT_WR;
            public string IconType;
        }

        [Serializable]
        public class UpdateDataOut
        {
            public string state;
            public string msg;
            public List<UpdateDataPara> data;
        }

        [Serializable]
        public class DeleteDataPara
        {
            public iQueryConditions CurQueryCondition;
        }


        [Serializable]
        public class dQueryConditions
        {
            public string RENT1;
            public string RENT2;
            public string RENT3;
            public string RENT4;
            public string RENT5;

        }

        [Serializable]
        public class DeleteDataOut
        {
            public string state;
            public string msg;
            public List<InsertDataPara> data;
        }

        [Serializable]
        public class QueryAllPara
        {
            public sCARAuthInformation CARInfo;
            public sQueryConditions CurQueryCondition;
        }

        [Serializable]
        public class sCARAuthInformation
        {

        }
        [Serializable]
        public class sQueryConditions
        {
            public string RENT1;
            public string RENT2;
            public string RENT3;
            public string RENT4;
            public string RENT5;
        }

        [Serializable]
        public class SQuery
        {
            public string state { get; set; }
            public string msg { get; set; }
            public List<sQueryTable> data;
        }

        [Serializable]
        public class sQueryTable
        {
            public string RENT1 { get; set; }
            public string RENT2 { get; set; }
            public string RENT3 { get; set; }
            public string RENT4 { get; set; }
            public string RENT5 { get; set; }
            public string ASPID { get; set; }
            public string CLASSRAIN { get; set; }
            public string CHINESERAIN { get; set; }
            public string ASPLINK { get; set; }
            public string TARGET { get; set; }
            public string MAKER { get; set; }
            public string MAKEDATE { get; set; }
            public string CHGER { get; set; }
            public string CHGDATE { get; set; }
            public string NEWMAP { get; set; }
            public string APPLYDATE { get; set; }
            public string APPLYCNT { get; set; }
            public string XINQUIRY { get; set; }
            public string XADDREW { get; set; }
            public string XEDIT { get; set; }
            public string XDEL { get; set; }
            public string XDOWNLOAD { get; set; }
            public string MK_M { get; set; }
            public string APPLY_CYCLE { get; set; }
            public string IT_WR { get; set; }
            public string IconType { get; set; }
        }




    }
}

如何在C#MVC WEB API中成功引用COM +元素?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-