【问题标题】:Generate a WCF client proxy for a netstandard Xamarin.Forms project为 netstandard Xamarin.Forms 项目生成 WCF 客户端代理
【发布时间】:2018-01-10 21:43:43
【问题描述】:

我创建了一个带有netstandard2.0 目标库而不是共享库或 PCL 库的 Xamarin.Forms 项目。到目前为止,这是编译和工作的。我正在使用 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

我发现 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 应用的有效生成客户端代理?我愿意接受任何导致相同预期结果的替代方案。

【问题讨论】:

  • 您选择 WCF 而不是使用 RESTful Web API 的任何特殊原因?
  • @Nkosi 我在 WCF 方面非常有经验,也想使用它背后的 EntityFramework。根据我的喜好,您知道任何易于适应的好选择吗?
  • 看看这个演练,看看它是否符合您的需求developer.xamarin.com/guides/cross-platform/… 它似乎遵循与您所做的类似的路径。
  • @Nkosi 我知道那篇文章,它没有关注 Xamarin.Forms。我需要从我的netstandard UI Xamarin.Forms 库访问该服务。现在我什至会接受自己编写代理,但我没有找到任何可以使用的示例。

标签: wcf xamarin .net-standard slsvcutil


【解决方案1】:

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

GitHub .NET Core WCF Client Libraries

Web Service Reference guide

Microsoft WCF Web Service Reference Provider

【讨论】:

  • 有趣。因此,如果我引用 .NET Core WCF 客户端库,我可以使用普通的 svcutil 来生成我的代理吗?据我所知,与 .NET Standard 相比,.NET Core 得到了增强。我不确定是否可以在 .NET Standard 中引用 .NET Core 库。我会尽快尝试。
  • 使用我发布的最后一个链接的扩展来生成代理。它也适用于 .NET Standard 我已经完成了。这也是官方生成客户端的方式。我也尝试过使用 svcutil 客户端,如果您在使用它时在服务客户端的构造函数中手动提供配置(绑定和端点地址),它就可以工作。否则你会得到一个配置文件不被支持的错误。
  • 该扩展在连接的服务中添加了一个新条目,当右键单击名为 WCF Web 服务参考的依赖项时,我可以发现我正在运行的服务的 mex 地址。当我单击“完成”时,它失败并显示错误:“无法构建解决方案,请尝试重新构建解决方案并修复任何构建问题。”虽然,我的整个解决方案都可以构建。如何解决?另外:我已经构建了 WCF 库。我现在必须引用 bin\ref\ 中的所有 DLL 吗?或者从哪里我需要参考什么库?我只需要 net tcp 通信。谢谢!
  • 如果扩展不起作用,请尝试 svcutil 路由。您可以通过从 NuGet 添加包来添加引用。扩展添加了这个包:System.ServiceModel.NetTcpSystem.ServiceModel.HttpSystem.ServiceModel.DuplexSystem.ServiceModel.Security
  • 谢谢,我设法通过使用 svcutil 生成代理来编译它。感谢 NuGet 包。不幸的是,当我尝试与服务器通信并且它没有提供任何进一步的信息时,我得到了未处理的异常,但我想这可以被认为是一个不同的问题。我将您的答案标记为正确。 (很抱歉你没有得到满分的100分,我不知道为什么StackOverflow没有给我更多的时间来决定谁应该得到满分)
猜你喜欢
  • 2012-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-11
  • 2010-10-22
  • 1970-01-01
相关资源
最近更新 更多