Exception when using e.Result in Xamarin Forms but only when i return string vlaue not double

Hello everybody , i use ASMX WebService with Xamarin.Forms , and i have two Methods in Web service , one Method returns String Value and the Other one returns Double , when i try these two methods with firefox (or any other internet Browser) , both methods work fine , but when i call those two method by Xamarin.forms , the one which returns double works fine ,but the other one with string value raises exception Error

Unhandled Exception:

System.Reflection.TargetInvocationException: An exception occurred during the operation, making the result invalid. Check InnerException for exception details.

where are my Web service Methods

{
[WebMethod]
public string ReturnString()
{
return "String Value Returned";
}

    [WebMethod]
    public double ReturnDouble()
    {
        return 5.500;
    }

}

And here is my Xamarin.forms Code

{
public MainPage()
{
InitializeComponent();

        BtReturnString.Clicked += BtReturnString_Clicked;
        BtReturnDouble.Clicked += BtReturnDouble_Clicked;
    }

    private void BtReturnDouble_Clicked(object sender, EventArgs e)
    {
        ServiceReference1.WebService1SoapClient WS = new ServiceReference1.WebService1SoapClient();
        WS.ReturnDoubleAsync();
        WS.ReturnDoubleCompleted += WS_ReturnDoubleCompleted;
    }

    private void WS_ReturnDoubleCompleted(object sender, ServiceReference1.ReturnDoubleCompletedEventArgs e)
    {
        Device.BeginInvokeOnMainThread(() =>
        {
            Result1.Text = e.Result.ToString();
        });
    }

    private void BtReturnString_Clicked(object sender, EventArgs e)
    {
        ServiceReference1.WebService1SoapClient WS = new ServiceReference1.WebService1SoapClient();
        WS.ReturnStringAsync();
        WS.ReturnStringCompleted += WS_ReturnStringCompleted;
    }

    private void WS_ReturnStringCompleted(object sender, ServiceReference1.ReturnStringCompletedEventArgs e)
    {
        Device.BeginInvokeOnMainThread(() =>
        {
            Result1.Text = e.Result.ToString();
        });
    }

}

any help please ?????

Best Answer

  • AboghrisAboghris US ✭✭
    Accepted Answer
    @kentucker
    Thank u for you reply ,but i have changed to API REST ,i think it is more stable with xamarin.forms , and i am doing good so far. Thank u again :)

Answers

  • kentuckerkentucker USMember ✭✭✭✭✭

    Not that this helps with the issue with the web service call not working but you should set the completed event handler before you call the method. I also prefer to use await for calling the service it is much cleaner looking code

    for example

       private async void BtReturnDouble_Clicked(object sender, EventArgs e)
        {
             ServiceReference1.WebService1SoapClient WS = new ServiceReference1.WebService1SoapClient();
             Result1.Text = await WS.ReturnDoubleAsync().ToString();
        }
    
  • AboghrisAboghris USMember ✭✭
    Accepted Answer
    @kentucker
    Thank u for you reply ,but i have changed to API REST ,i think it is more stable with xamarin.forms , and i am doing good so far. Thank u again :)
Sign In or Register to comment.