为 netstandard Xamarin.Forms 项目生成 WCF 客户端代理

Generate a WCF client proxy for a netstandard Xamarin.Forms project

我创建了一个 Xamarin.Forms 项目,其中包含 netstandard2.0 目标库而不是共享库或 PCL 库。到目前为止,这编译和工作。我正在使用 Visual Studio 2017 社区的更新版本。

我还创建了一个将托管在 windows 本身(不是 IIS)上的 WCF 服务。我已将 app.config 配置为提供元数据交换端点:

<?xml version="1.0"?>
<configuration>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
    </startup>
    <system.serviceModel>
        <services>
            <service behaviorConfiguration="MM.Server.ServiceServerBehavior" name="MM.Server.ServiceServer">
                <endpoint address="net.tcp://localhost:8730/MMServer/" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IServiceServer" bindingName="NetTcpBinding_IServiceServer_EndPoint" contract="MM.Contracts.IServiceServer">
                    <identity>
                        <dns value="localhost"/>
                    </identity>
                </endpoint>
                <endpoint address="http://localhost:8731/MMServer/mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
                <host>
                    <baseAddresses>
                        <add baseAddress="net.tcp://localhost:8730/MMServer/"/>
                    </baseAddresses>
                </host>
            </service>
        </services>
        <behaviors>
            <serviceBehaviors>
                <behavior name="MM.Server.ServiceServerBehavior">
                    <serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:8731/MMServer/mex" />
                    <serviceDebug includeExceptionDetailInFaults="true"/>
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <bindings>
            <netTcpBinding>
                <binding name="NetTcpBinding_IServiceServer" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions" hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxConnections="10" maxReceivedMessageSize="2147483647">
                    <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="16384"/>
                    <reliableSession ordered="true" inactivityTimeout="01:00:00" enabled="false"/>
                    <security mode="None"><!-- TODO: Add more security later -->
                        <transport clientCredentialType="None" protectionLevel="None"/>
                    </security>
                </binding>
            </netTcpBinding>
        </bindings>
    </system.serviceModel>
</configuration>

这是一个非常简单的配置,我暂时不关心安全问题。服务运行成功。

现在我不能简单地向我的 Xamarin.Forms 项目添加服务引用,因为 netstandard 不提供 System.ServiceModel

我发现 I can use the SLsvcUtil.exe 从 Silverlight SDK 为我的 WCF 服务生成客户端代理,该代理与 Xamarin.Forms 目标 netstandard 兼容,但是我无法获得它 运行。

当我的 WCF 服务是 运行 时,无论我如何尝试使用 SLsvcUtil.exe,我总是得到错误:

Error: An error occurred in the tool.
Error: Object reference not set to an instance of an object.

这是我用来执行的批处理 SLsvcUtil.exe:

set "namespace=*,MM.Services"
set "mexPath=http://localhost:8731/MM/mex"
set "svcutil=C:\Program Files (x86)\Microsoft SDKs\Silverlight\v5.0\Tools\SLsvcUtil.exe"
set "outputDir=C:\vsproj\Xamarin\MM\MM.App"
"%svcutil%" %mexPath% /directory:"%outputDir%" /namespace:"%namespace%"
pause

http://localhost:8731/MM/mex returns 完整的 WSDL,成功。

如何为我的 Xamarin.Forms 应用程序生成一个可用的、面向 netstandard2.0 的客户端代理?我愿意接受任何导致相同预期结果的替代方案。

查看 .NET Core 的官方 Microsoft WCF 客户端。我认为这也适用于 Xamarin,因为它是为 .NET Core 设计的。

GitHub .NET Core WCF Client Libraries

Web Service Reference guide

Microsoft WCF Web Service Reference Provider