[英]Not able to Consume wcf service POST method in Portable class library for xamarin android
I am consuming wcf webservices(POST method) in Portable class library in visual studio for xamarin android.App is getting crash in POST method.Below is my code 我正在使用Visual Studio中的可移植类库中的wcf webservices(POST方法)for xamarin android.App在POST方法中崩溃.Below是我的代码
IN PCL 在PCL
Class1.cs 将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 IRestService.cs
namespace XamarinServiceCall
{
public interface IRestService
{
Task<string> GetData(UserModel model);
}
}
UserModel.cs 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 get 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 在Xamarin Android APp中
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 from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
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: 邮递员回应:
When i debug i get this response in result variable. 当我调试时,我在结果变量中得到此响应。
When i point to postAsync Function it shows me Expression is not valid 当我指向postAsync Function时,它显示Expression无效
You should not try to perform any significant work, like calling your web service, inside OnCreate function of MainActivity. 您不应该尝试在MainActivity的OnCreate函数中执行任何重要的工作,例如调用Web服务。
And of course, you should not make it async and make asynchronous calls inside it. 当然,你不应该让它异步并在其中进行异步调用。 Please check Xamarin's examples.
请查看Xamarin的例子。
Generally, in OnCreate you can make some initialization and then call LoadApplication for your Application class (which you can define in PCL project). 通常,可以在OnCreate中进行一些初始化,然后为Application类(可以在PCL项目中定义)调用LoadApplication。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.