MvvmCross IoC 和 WCF

MvvmCross IoC and WCF

我已经创建了一个应用程序,我需要在其中通过 Web 上的 WCF 数据服务访问数据。我已经设置了我的 Core、Droid 和 iOS 项目。我使用 svcutil 生成 类 等来访问 WCF 服务。由于便携式项目中缺少库,这仅适用于 Droid 应用程序。我的ViewModel如下

public class MapViewModel : MvxViewModel
{

    IMyAppService MyAppService;

    CompositeType data;

    public MapViewModel(IMyAppService MyAppService)
    {
        this.MyAppService = MyAppService;
    }

    public override async Task Initialize()
    {
        //TODO: Add starting logic here

        data = await MyAppService.SayHello("Jim");

        //return base.Initialize();
    }
}

在我的核心项目中,我也描述了接口:

public interface IMyAppService
{
    Task<CompositeType> SayHello(string Name);
}

到目前为止一切都很好。我在我的 Droid 项目中实现了接口:

public sealed class WhatNowService : IWhatNowService
{

    private static string ServiceUri = "WhatNow.svc";

    public WhatNowClient NewClient()
    {
        BasicHttpsBinding binding = DataModel.CreateBasicHttp();

        WhatNowClient client = new WhatNowClient(binding, new EndpointAddress(ServiceUri));
        client.ClientCredentials.UserName.UserName = Settings.Domain + "\" + Settings.UserName;
        client.ClientCredentials.UserName.Password = Settings.Password;

        return client;
    }


    public async Task<CompositeType> SayHello(string Name)
    {
        CompositeType ret = new CompositeType();
        ret.BoolValue = true;
        ret.StringValue = Name;

        try
        {
            WhatNowClient client = NewClient();
            ret = await Task<CompositeType>.Factory.FromAsync((asyncCallback, asyncState) => client.BeginGetDataUsingDataContract(ret, asyncCallback, asyncState),
               (asyncResult) => client.EndGetDataUsingDataContract(asyncResult), null);

        }
        catch (Exception ex)
        { }

        return ret;
    }
}

因为它以 "Service" 结尾,它应该被注册为惰性单例。所以这将我们带到了 svcutil 的输出。这是一个片段:

using System.Runtime.Serialization;
using System;


[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="CompositeType", Namespace="http://schemas.datacontract.org/2004/07/MyApp.WCF")]
[System.SerializableAttribute()]
public partial class CompositeType : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged
{

    [System.NonSerializedAttribute()]
    private System.Runtime.Serialization.ExtensionDataObject extensionDataField;

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private bool BoolValueField;

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private string StringValueField;

    public System.Runtime.Serialization.ExtensionDataObject ExtensionData
    {
        get
        {
            return this.extensionDataField;
        }
        set
        {
            this.extensionDataField = value;
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public bool BoolValue
    {
        get
        {
            return this.BoolValueField;
        }
        set
        {
            if ((this.BoolValueField.Equals(value) != true))
            {
                this.BoolValueField = value;
                this.RaisePropertyChanged("BoolValue");
            }
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public string StringValue
    {
        get
        {
            return this.StringValueField;
        }
        set
        {
            if ((object.ReferenceEquals(this.StringValueField, value) != true))
            {
                this.StringValueField = value;
                this.RaisePropertyChanged("StringValue");
            }
        }
    }

    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName)
    {
        System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
        if ((propertyChanged != null))
        {
            propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }
}

关键是它包含 "CompositeType" 的定义。如果我将这段代码放在 Core 项目中,所有类型都可用于所有项目,但它不会编译,因为 Portable 项目没有 System.Runtime.Serialization 等等。如果我把它放在 Droid 项目中,那么 Core 项目就不知道 CompositeType 是什么。最终,我认为 svcutil 输出需要在 Droid 项目中。有没有办法让 Core 项目能够看到 CompositeType 类型?有没有更好的方法可以做到这一点?似乎 .Net Core Library 可能比 Portable Library 更适合使用 WCF。这是未来 MvvmCross 版本的考虑吗?

谢谢, 吉姆

使用 .NET Standard 2.0 库。

.NET Standard 是 .NET 平台上可移植库的替代品,几乎所有的 .NET Framework API 都具有跨平台支持(一些开箱即用的 BCL 库是在 NuGet 上可用)。