对象与引用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 (将#修改为@)