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

时间:2017-11-12 16:37:42

标签: wcf xamarin .net-standard slsvcutil

我创建了一个带有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>

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

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

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

无论我在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成功返回完整的WSDL。

如何为我的定位netstandard2.0的Xamarin.Forms应用获取有效的客户端代理?我打开任何可以产生相同结果的替代方案。

1 个答案:

答案 0 :(得分:4)

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

GitHub .NET Core WCF Client Libraries

Web Service Reference guide

Microsoft WCF Web Service Reference Provider