加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 从Azure中的SOAP Web服务检索列表时发生TargetInvocationE

发布时间:2020-12-15 07:42:50 所属栏目:百科 来源:网络整理
导读:我有一个SOAP Web服务,我从中提取各种信息.大多数函数都正常工作,但我需要一些函数来返回List. WebMethod的定义如下: ListMyType myTypes = new ListMyTypes();[WebMethod]public ListMyType GetAllMyTypes(){ string sql = "SELECT * FROM MyType"; DataTa
我有一个SOAP Web服务,我从中提取各种信息.大多数函数都正常工作,但我需要一些函数来返回List.

WebMethod的定义如下:

List<MyType> myTypes = new List<MyTypes>();

[WebMethod]
public List<MyType> GetAllMyTypes()
{
    string sql = "SELECT * FROM MyType";
    DataTable dt = new DataTable();
    dt = Globals.GLS_DataQuery(sql);

    List<MyType> myType = new List<MyType>();
    foreach (DataRow row in dt.Rows)
    {
        MyType myType = new MyType()
        {
            ID = (int)row["Id"]
        };

        myTypes.Add(myType);
    }

    return myTypes;
}

Web服务在主项目中引用,并通过以下方式调用:

client.GetAllMyTypesCompleted += client_GetAllMyTypesCompleted;
client.GetAllMyTypesAsync();

client_GetAllMyTypesCompleted定义为:

private void client_GetAllMyTypesCompleted(object sender,GetAllMyTypesCompletedEventArgs e)
{
    var collection = e.Result;
}

在这里抛出TargetInvocationException,特别是关于Result.如果您自己运行Web服务,则返回正确的数据.供参考GLS_DataQuery定义为:

public static DataTable GLS_DataQuery(string sql)
{
    DataTable dt = new DataTable
    SqlCommand command = new SqlCommand(sql,connection);
    SqlDataAdapter adapter = new SqlDataAdapter(command);
    adapter.Fill(dt);

    return dt;
}

那我为什么看到这个错误呢?或者我应该如何返回此实例中的对象列表?

Web服务托管在Azure中可能具有相关性.

编辑:将调试器附加到在Azure中运行的Web服务的实例,可以看到它确实返回了正确的数据.该错误是在调用Web服务的Xamarin手机应用程序中引发的.错误的“消息”只是一个空引用错误,堆栈跟踪是:

at
MyApp.MyService.Service1SoapClient.EndGetAllMyTypes(IAsyncResult
result) at
MyApp.MyService.Service1SoapClient.OnEndGetAllMyTypes(IAsyncResult
result) at
System.ServiceModel.ClientBase`1.OnAsyncCallCompleted(IAsyncResult
result)

解决方法

事实证明,您需要将HttpGet和HttpPost添加到Web服务的Web.config中的协议.
<system.web>
    <webServices>
        <protocols>
            <add name="HttpSoap"/>
            <add name="HttpPost"/>
            <add name="HttpGet"/>
            <add name="Documentation"/>
        </protocols>
    </webServices>
</system.web>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读