【发布时间】: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。我需要从我的
netstandardUI Xamarin.Forms 库访问该服务。现在我什至会接受自己编写代理,但我没有找到任何可以使用的示例。
标签: wcf xamarin .net-standard slsvcutil