简体   繁体   中英

TargetInvocationException when retrieving list from SOAP web service in Azure

I have a SOAP web service which I am pulling various information from. Most functions are working correctly, but I need some functions to return a List .

The WebMethod is defined like:

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;
}

The web service is referenced in the main project and is called by:

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

client_GetAllMyTypesCompleted is defined as:

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

It is here that the TargetInvocationException is thrown, specifically regarding Result . If you run the web service by itself the correct data is returned. For reference GLS_DataQuery is defined as:

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;
}

So why am I seeing this error? Or how should I be returning a list of objects in this instance?

It might be of relevance that the web service is hosted in Azure.

EDIT: Attaching the debugger to the instance of the web service running in Azure sees that it does return the correct data. The error is thrown in a Xamarin phone app that is calling the web service. The "Message" of the error is simply a null reference error and the stack trace is:

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

So turns out you need to add HttpGet and HttpPost to the protocols in the Web.config of the web service.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM