【问题标题】:Not able to Consume wcf service POST method in Portable class library for xamarin android无法在 xamarin android 的便携式类库中使用 wcf 服务 POST 方法
【发布时间】:2016-03-17 10:17:20
【问题描述】:

我在 Visual Studio 的便携式类库中为 xamarin android 使用 wcf webservices(POST 方法)。应用程序在 POST 方法中崩溃。下面是我的代码

在 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 应用程序中

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 函数时,它显示表达式无效

【问题讨论】:

  • “崩溃”是什么意思?您在调试器中遇到异常吗?请添加调试器的输出 - 错误消息、堆栈等。

标签: c# android wcf xamarin


【解决方案1】:

您不应该尝试在 MainActivity 的 OnCreate 函数中执行任何重要的工作,例如调用您的 Web 服务。

当然,你不应该让它异步并在其中进行异步调用。请查看 Xamarin 的示例。

通常,在 OnCreate 中您可以进行一些初始化,然后为您的 Application 类调用 LoadApplication(您可以在 PCL 项目中定义)。

【讨论】:

  • 如果您能向我展示一些有关在 xamarin android 中使用 wcf 服务的示例,将会非常有帮助
  • 在你使用 wcf 服务之前,你应该得到工作的应用程序。至于现在,您没有正确加载您的应用程序。稍后我会尝试找到一些示例。
  • 它的工作服务,实际上是我故意向stackOverflow发布了错误的URL。上面我已经发布了邮递员响应和调试响应。请检查
  • @dafodil,您当前的问题不是服务,而是整个应用程序。尝试评论您的服务调用并检查您的应用是否仍然崩溃。如果不是,请取消注释调用并从崩溃的那一刻起从调试器发布调用堆栈。但我认为即使没有调用服务它也会崩溃。然后尝试从 Xamarin 网站阅读“入门”示例,并获取工作 Xamarin.Andriid 应用程序的示例。之后添加您的服务消费。
  • 我已经更正了,现在应用程序没有崩溃。首先我通过删除服务方法进行检查,然后通过添加服务调用方法,应用程序没有崩溃。但我没有得到响应。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多