8
votes

I've created a Xamarin.Forms project with a netstandard2.0 targeting library instead of a shared or PCL library. So far this compiles and works. I'm using an up2date version of Visual Studio 2017 Community.

I also have created a WCF service that gonna be hosted on windows itself (not IIS). I've configured my app.config to provide a Metadata Exchange Endpoint:

<?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>

This is a very simple configuration and I don't care about security for now. The service runs, successfully.

Now I cannot simply add a service reference to my Xamarin.Forms project, since netstandard does not provide System.ServiceModel.

I've found out that I can use the SLsvcUtil.exe from the Silverlight SDK to generate the client proxy for my WCF service that is compatible to Xamarin.Forms targeting netstandard instead, however I cannot get it running.

No matter how I try to use the SLsvcUtil.exe while my WCF service is running, I always get the error:

Error: An error occurred in the tool.
Error: Object reference not set to an instance of an object.

Here is my batch that I used to execute 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 returns the full WSDL, successfully.

How can I get a working generated client proxy for my Xamarin.Forms app that is targeting netstandard2.0? I'm open for any alternative that leads to the same desired result.

1
Any particular reason you choose WCF over using a RESTful Web API? - Nkosi
@Nkosi I'm very experienced in WCF and also want to use the EntityFramework behind it. Do you know any good alternative that is easy to adapt in respect of my preferences? - Martin Braun
Take a look at this walk through and see if it suits your needs developer.xamarin.com/guides/cross-platform/… it does seems to follow along similar path to what you were doing. - Nkosi
@Nkosi I know that article and it's not focusing Xamarin.Forms. I need to access the service from my netstandard UI Xamarin.Forms library. Right now I would even accept to write the proxy by myself, but I didn't find any sample I can use. - Martin Braun

1 Answers

4
votes

Take a look at the official Microsoft WCF client for .NET Core. I think this should work also with Xamarin since it is designed for .NET Core.

GitHub .NET Core WCF Client Libraries

Web Service Reference guide

Microsoft WCF Web Service Reference Provider