【问题标题】:TargetInvocationException when retrieving list from SOAP web service in Azure从 Azure 中的 SOAP Web 服务检索列表时出现 TargetInvocationException
【发布时间】:2015-04-29 23:34:01
【问题描述】:

我有一个 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 手机应用程序中会引发错误。错误的“消息”只是一个空引用错误,堆栈跟踪是:

在 MyApp.MyService.Service1SoapClient.EndGetAllMyTypes(IAsyncResult 结果)在 MyApp.MyService.Service1SoapClient.OnEndGetAllMyTypes(IAsyncResult 结果)在 System.ServiceModel.ClientBase`1.OnAsyncCallCompleted(IAsyncResult 结果)

【问题讨论】:

  • 请发布异常消息,特别是内部异常。
  • 内部异常只是一个NullReferenceException

标签: c# web-services azure soap xamarin


【解决方案1】:

因此,您需要将HttpGetHttpPost 添加到Web 服务的Web.config 中的协议中。

<system.web>
    <webServices>
        <protocols>
            <add name="HttpSoap"/>
            <add name="HttpPost"/>
            <add name="HttpGet"/>
            <add name="Documentation"/>
        </protocols>
    </webServices>
</system.web>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-24
    • 2012-05-05
    • 1970-01-01
    • 2023-03-21
    • 2017-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多