【问题标题】:WCF, EF and UWPWCF、EF 和 UWP
【发布时间】:2017-05-28 17:42:51
【问题描述】:

我目前正在编写 UWP 应用程序。我正在使用 WCF 服务与包含 EF 的业务层对话以与 SQL Server 数据库对话。这一切都处于非常早期的阶段,因为我以前从未使用过 UWP。

我让 UWP 使用 WCF 从数据库返回数据。当我返回一个字符串时,这工作正常。但是,现在我想返回对象,这在 UWP 只接收引用的意义上不再起作用。不返回任何对象。当我在调试模式下运行 WCF 时,返回对象,我可以看到所有包含数据的字段等。

你们中的一个天才可以帮助我吗,因为我被困住了,不知道该怎么办。

这是我的代码。我在业务层的课

namespace Business.Models
{
  [DataContract(IsReference = true)]
  public class SystemUser
  {
    public tblSystemUser User { get; set; }

    // public tblRole Role { get; set; }

  }
}

WCF 代码:

namespace ActiveCareWCF
{
  [ServiceContract]
  public interface IUser
  {

    [OperationContract]
    Task<SystemUser> DoLogin(string userName);
  }


 public class Service : IUser
 {
    public async Task<SystemUser> DoLogin(string userName)
    {
        SystemUser systemUser = new SystemUser();
        userName = @"username";

        try
        {
            ServiceCalls serviceCalls = new ServiceCalls();

            systemUser = serviceCalls.AuthorizeUser(userName);

            return systemUser --- **this is returning an object**;
        }
        catch (Exception ex)
        {

            throw ex;
        }

    }

}

SVC 文件:

 public class ActiveCareService : IUser
{
    public async Task<SystemUser> DoLogin(string userName)
    {
        Service service = new Service();

        var user = service.DoLogin(userName);

        return user.Result;
    }

}

最后是 UWP 中的调用。

 private async void Login_Click(object sender, RoutedEventArgs e)
    {
        ActiveCareService.UserClient client = new ActiveCareService.UserClient();

        var userFromService = client.DoLoginAsync(@"username").Result;
        await client.CloseAsync();

        var dialog = new MessageDialog("Logged in as " + userFromService.FirstName); **This just returns a string of the type of object it is, not the actual object. This is what I'm struggling with.**
        await dialog.ShowAsync();

        Frame.Navigate(typeof(YourSites));
    }

提前致谢。

【问题讨论】:

  • 在 UWP 应用中更新服务客户端?
  • 嗨,戴夫,我已更新、删除并重新添加。 UWP 中返回的所有调用都是 {ActiveCareApp.ActiveCareService.SystemUser}。
  • 这里有很多问题。 1)你真的需要WCF吗?不是直接错误,而是有点沉重和过时。替代方案是 a.o、WebAPI 和 ServiceStack。
  • 3) 你的async void Login_Click() 不应该使用.Result。请改用await
  • 这是 UWP 问题还是服务配置问题?如果您直接调用端点,我会通过查看原始 HTTP 中的内容来判断。

标签: c# entity-framework wcf uwp


【解决方案1】:

我也在做同样的事情,现在我的服务总是返回 JSON 字符串,这意味着必须在服务中序列化并在 uwp 处反序列化。

您必须在服务和 UWP 中都有序列化类的模型。效果很好。

在我的服务中,我有一个完整的模型,在 UWP 应用程序中,我创建了一个符合我需要的简化模型。

希望这能有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多