c# - 从 Azure 中的 SOAP Web 服务检索列表时出现 TargetInitationException

标签 c# web-services azure soap xamarin

我有一个 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;
}

正是在这里抛出了TargetInitationException,特别是关于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)

最佳答案

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

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

关于c# - 从 Azure 中的 SOAP Web 服务检索列表时出现 TargetInitationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29956461/

相关文章:

c# - 无法访问 Azure 中的文件系统

c# - 是否存在在循环中使用 `i <= 2` 代替 `i < 3` 会导致循环行为发生变化的情况?

c# - 无法将类型 'int?[]' 隐式转换为 'int[]

mysql - mysql如何处理并发访问?

azure - 如何在 kusto 中将字符串数据从列转换为日期

具有高级存储 (SSD) 的 Azure 应用服务计划

c# - 在 C# WPF 中恢复下载

c# - Windows 服务 - C#

web-services - 从 WCF 项目返回有意义的异常

PHP SoapClient SSL : Failed to load external entity (certificate not loaded)