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

mod*_*diX 8 wcf slsvcutil xamarin .net-standard

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

我还创建了一个WCF服务,它将托管在Windows本身(而不是IIS)上.我已将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>
Run Code Online (Sandbox Code Playgroud)

这是一个非常简单的配置,我现在不关心安全性.该服务成功运行.

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

我发现我可以使用 Silverlight SDK中的SLsvcUtil.exe为我的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.
Run Code Online (Sandbox Code Playgroud)

这是我用来执行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
Run Code Online (Sandbox Code Playgroud)

http:// localhost:8731/MM/mex成功返回完整的WSDL.

如何为我的目标Xamarin.Forms应用程序获取有效的客户端代理netstandard2.0?我愿意接受任何导致相同结果的替代方案.

Ale*_*esD 4

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

GitHub .NET Core WCF 客户端库

Web 服务参考指南

Microsoft WCF Web 服务参考提供程序