Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

consuming WCF Rest Service in sample application using Xamarin.Forms PCL project

$
0
0

Hi,

We are consuming WCF Rest Service in sample application using Xamarin.Forms PCL project . The application is failing at run time and I am getting "System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation."

**
Kindly clarify if this is the best approach for consuming WCF Rest Service in Xamarin.forms .**

Because we are using the predefined WCF service in application and generating WCF Proxy client for predefined one.But we don't know where the client deploy the WCF service in known place. So how to fit this approach for unknown Webservice deployment in client .

Kindly clarify this.

Description of Problem:

1.I created a Xamarin.Forms PCL project which gave me a solution with PCL project for Android,iOS and Windows projects.

Target Properties of the PCL for the project
.NET Framework 4.5
Xamarin.iOS
Xamarin.Android
Windows 8

2.Created WCF Rest service with simple method which return"Hello World".
3.WCF service hosted in local machine(IIS server)in my case.
4.Consuming WCF Service method in client application.
For this , I have generated WCF Proxy client with SLSvcUtil.exe tool from the Silverlight SDK.

slsvcutil http://ipaddress/mobileservice/service.svc?WSDL /out:reference.cs

ipaddress- Local machine address which points to the WCF service deployment.

Adding the resulting reference.cs file to my project.

Adding the following code to access the service:

try
{
SampleWCFClient client = new SampleWCFClient(
new BasicHttpBinding(),
new EndpointAddress("http://192.168.0.225/SampleWCF/SampleWCF.svc"));
// Call the proxy - this should use the async versions
client.WorldCompleted += client_WorldCompleted;
client.WorldAsync();

        }
        catch (Exception ex)
        {
            String tedxt = ex.Message.ToString();
            String asetrgtedxt = ex.InnerException.ToString();
        }

void client_WorldCompleted(object sender, WorldCompletedEventArgs e)
{
Device.BeginInvokeOnMainThread(async () => {
string error = null;
if (e.Error != null)
error = e.Error.Message;
else if (e.Cancelled)
error = "Cancelled";
if (!string.IsNullOrEmpty(error))
{
await DisplayAlert("Error", error, "OK", "Cancel");
}
else
{

  //return Hello World if success
   string Helloworld = e.Result;
 }
});

}

The application is failing at run time and I am getting "System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.


Viewing all articles
Browse latest Browse all 58056

Trending Articles