[英]How can I consume wcf service for login in Native apps with Portable class library (PCL) in xamarin studio
[英]Not able to Consume wcf service POST method in Portable class library for xamarin android
我正在使用Visual Studio中的可移植類庫中的wcf webservices(POST方法)for xamarin android.App在POST方法中崩潰.Below是我的代碼
在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 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; }
}
在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;
}
}
}
郵遞員回應:
當我調試時,我在結果變量中得到此響應。
當我指向postAsync Function時,它顯示Expression無效
您不應該嘗試在MainActivity的OnCreate函數中執行任何重要的工作,例如調用Web服務。
當然,你不應該讓它異步並在其中進行異步調用。 請查看Xamarin的例子。
通常,可以在OnCreate中進行一些初始化,然后為Application類(可以在PCL項目中定義)調用LoadApplication。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.