c# - Not able to Consume wcf service POST method in Portable class library for xamarin android -


i consuming wcf webservices(post method) in portable class library in visual studio xamarin android.app getting crash in post method.below code

in pcl

class1.cs

namespace xamarinservicecall {     public sealed class class1 : irestservice     {          public async task<string> getdata(usermodel model)         {             using (var client = new httpclient())             {                 var content = new stringcontent(jsonconvert.serializeobject(model), encoding.utf8, "application/json");                  var result = await client.postasync("http://xamarin-rest-service/loginservice/validatelogin", content);                 return await result.content.readasstringasync();              }         }     } } 

irestservice.cs

namespace xamarinservicecall {     public interface irestservice     {         task<string> getdata(usermodel model);     } } 

usermodel.cs

public class usermodel     {          public string loginfrom { get; set; }          public string domainname { get; set; }         public string username { get; set; }         public string password { get; set; }          //i response of below parameters         public int domaintype { get; set; }         public string firstname { get; set; }          public int status { get; set; }         public int userid { get; set; }       } 

in xamarin android app

mainactivity.cs

namespace servicesample {     [activity(label = "layoutsample", mainlauncher = true, icon = "@drawable/icon")]     public class mainactivity : activity     {             protected async override void oncreate(bundle bundle)         {             base.oncreate(bundle);              // set our view "main" layout resource             setcontentview(resource.layout.main);              // our button layout resource,             // , attach event             button button = findviewbyid<button>(resource.id.mybutton);              xamarinservicecall.class1 serviceclass = new xamarinservicecall.class1();            var model = new xamarinservicecall.usermodel { domainname = "domainname" ,username = "username" , password = "password" ,loginfrom = "app" };              var result = await serviceclass.getdata(model);             button.text = "get call says: " + result;          }     } } 

postman response:

enter image description here

when debug response in result variable.

enter image description here

when point postasync function shows me expression not valid

enter image description here

you should not try perform significant work, calling web service, inside oncreate function of mainactivity.

and of course, should not make async , make asynchronous calls inside it. please check xamarin's examples.

generally, in oncreate can make initialization , call loadapplication application class (which can define in pcl project).


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -