繁体   English   中英

无法在Xamarin android的Portable类库中使用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方法中崩溃.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中

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 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM