Tôi đã tạo dự án Xamarin.Forms với thư viện nhắm mục tiêu netstandard2.0
thay vì thư viện được chia sẻ hoặc PCL. Cho đến nay điều này biên dịch và hoạt động. Tôi đang sử dụng phiên bản up2date của Cộng đồng Visual Studio 2017.Tạo proxy máy khách WCF cho dự án Xamarin.Forms netstandard
Tôi cũng đã tạo một dịch vụ WCF sẽ được lưu trữ trên chính cửa sổ (không phải IIS). Tôi đã định cấu hình app.config của mình để cung cấp Điểm cuối trao đổi siêu dữ liệu:
<?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>
Đây là cấu hình rất đơn giản và hiện tại tôi không quan tâm đến bảo mật. Dịch vụ này chạy thành công.
Bây giờ tôi không thể chỉ thêm một tham chiếu dịch vụ vào dự án Xamarin.Forms của tôi, vì netstandard
không cung cấp System.ServiceModel
.
Tôi đã phát hiện ra rằng I can use the SLsvcUtil.exe từ Silverlight SDK để tạo proxy máy khách cho dịch vụ WCF tương thích với Xamarin.Forms nhắm mục tiêu netstandard
thay vào đó, tuy nhiên tôi không thể chạy nó.
Không có vấn đề làm thế nào tôi cố gắng sử dụng SLsvcUtil.exe trong khi dịch vụ WCF của tôi đang chạy, tôi luôn nhận được lỗi:
Error: An error occurred in the tool.
Error: Object reference not set to an instance of an object.
Dưới đây là hàng loạt của tôi mà tôi sử dụng để thực hiện 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 trả về WSDL đầy đủ, thành công.
Tôi làm cách nào để có được proxy máy khách được tạo hoạt động cho ứng dụng Xamarin.Forms đang nhắm mục tiêu netstandard2.0
? Tôi mở cho bất kỳ thay thế nào dẫn đến cùng một kết quả mong muốn.
Bất kỳ lý do đặc biệt mà bạn chọn WCF qua sử dụng một Web API RESTful? – Nkosi
@Nkosi Tôi rất có kinh nghiệm trong WCF và cũng muốn sử dụng EntityFramework phía sau nó. Bạn có biết bất kỳ giải pháp thay thế tốt nào có thể dễ dàng điều chỉnh theo sở thích của tôi không? – modiX
Hãy xem qua hướng dẫn này và xem liệu nó có phù hợp với nhu cầu của bạn không https://developer.xamarin.com/guides/cross-platform/application_fundamentals/web_services/walkthrough_working_with_WCF/ dường như theo cùng một lộ trình tương tự với những gì bạn đang làm . – Nkosi