在 Xamarin.Forms 和 App.config 中使用 WCF

Consuming WCF in Xamarin.Forms with App.config

是否可以在 Xamarin.Forms 中使用 App.config 使用 WCF 服务?在 Xamarin.Forms 中,我有 PCL 库和三个平台项目:iOS、Android 和 Windows Phone.

通常(例如在 WPF 中)我有 App.config 个包含以下内容的文件:

<system.serviceModel>
    <client>
        <endpoint
          address="net.tcp://localhost:8002/FooService"
          binding="netTcpBinding"
          contract="MyApp.Contracts.IFooService" />
    </client>
</system.serviceModel>

GameProxy class,像这样:

public class GameProxy : ClientBase<IFooService>, IFooService
{
    public async Task<BarResponse> BarRequest(int id)
    {
        return await Channel.BarRequest(id);
    }
}

是否可以在 Xamarin.Forms 中以这种方式进行?

Xamarin 中没有 App.config 文件。您只需要像这样在代码中设置值:

private static BasicHttpBinding CreateBasicHttp()
{
    BasicHttpBinding binding = new BasicHttpBinding
        {
            Name = "basicHttpBinding",
            MaxBufferSize = 2147483647,
            MaxReceivedMessageSize = 2147483647
        };
    TimeSpan timeout = new TimeSpan(0, 0, 30);
    binding.SendTimeout = timeout;
    binding.OpenTimeout = timeout;
    binding.ReceiveTimeout = timeout;
    return binding;
}

Xamarin 文档有一个 complete WCF walkthrough