无法在 xamarin android 的便携式类库中使用 wcf 服务 POST 方法 [英] 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 方法中崩溃.下面是我的代码
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
在 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
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; }
}
在 Xamarin Android 应用程序中
In 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;
}
}
}
邮递员回复:
当我调试时,我在结果变量中得到这个响应.
When i debug i get this response in result variable.
当我指向 postAsync 函数时,它显示我表达式无效
When i point to postAsync Function it shows me Expression is not valid
推荐答案
您不应该尝试在 MainActivity 的 OnCreate 函数中执行任何重要的工作,例如调用您的 Web 服务.
You should not try to perform any significant work, like calling your web service, inside OnCreate function of MainActivity.
当然,您不应该使它异步并在其中进行异步调用.请查看 Xamarin 的示例.
And of course, you should not make it async and make asynchronous calls inside it. Please check Xamarin's examples.
通常,您可以在 OnCreate 中进行一些初始化,然后为您的 Application 类(您可以在 PCL 项目中定义)调用 LoadApplication.
Generally, in OnCreate you can make some initialization and then call LoadApplication for your Application class (which you can define in PCL project).
这篇关于无法在 xamarin android 的便携式类库中使用 wcf 服务 POST 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!