如何解决对象与引用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 举报,一经查实,本站将立刻删除。