简体   繁体   English

无法在Xamarin android的Portable类库中使用wcf服务POST方法

[英]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中

MainActivity.cs 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;

        }
    }
}

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.

相关问题 如何在Xamarin Studio中使用WCF服务在带有可移植类库(PCL)的本机应用程序中登录 - How can I consume wcf service for login in Native apps with Portable class library (PCL) in xamarin studio 如何在Xamarin.Forms可移植类库中使用WCF服务 - how to use WCF Service in Xamarin.Forms Portable class library 使用Xamarin在Portable类库中保护WCF绑定 - Secure WCF binding in a Portable class library with Xamarin 可以使用可移植类库通过通道工厂在Silverlight中使用WCF服务吗? - Possible to consume a WCF service in Silverlight via a channel factory, using a Portable Class Library? 如何在WCF操作协定方法中使用Web Service类库 - How to consume Web Service class library in WCF operation contract method WCF服务参考可移植类库内部 - WCF Service Reference Portable Class Library Internal 如何从Xamarin.Forms中的可移植类库项目调用位于Android项目内的方法? - How to invoke a method located inside the Android project from the Portable Class Library Project in Xamarin.Forms? 在Xamarin Forms可移植类库(PCL)中使用SOAP Web服务 - Consuming SOAP web service in Xamarin Forms Portable Class Library (PCL) Xamarin可移植类库问题 - Xamarin portable class library issue 在 Xamarin 跨平台应用程序中使用 WCF 服务 - Consume WCF service in Xamarin Cross Platform Application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM