8

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.

Athari
  • 33,702
  • 16
  • 105
  • 146
Martin Braun
  • 10,906
  • 9
  • 64
  • 105
  • 1
    Any particular reason you choose WCF over using a RESTful Web API? – Nkosi Nov 15 '17 at 12:30
  • @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 Nov 15 '17 at 16:19
  • Take a look at this walk through and see if it suits your needs https://developer.xamarin.com/guides/cross-platform/application_fundamentals/web_services/walkthrough_working_with_WCF/ it does seems to follow along similar path to what you were doing. – Nkosi Nov 15 '17 at 16:24
  • @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 Nov 15 '17 at 21:12

1 Answers1

4

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

Aleš Doganoc
  • 11,568
  • 24
  • 40
  • Interesting. So if I reference the .NET Core WCF Client Library I can use the normal svcutil to generate my proxy? As far as I know .NET Core is enhanced in comparison to .NET Standard. I'm not sure I can reference a .NET Core library in .NET Standard. I will try it soon. – Martin Braun Nov 22 '17 at 15:43
  • Use the extension from the last link I posted to generate the proxy. It works also with .NET Standard I have done it. This is also the official way to generate clients. I have tried also with a svcutil client and it works if you manually provide the configuration (binding and endpoint address) in the constructor of the service client when you use it. Else you get a configuration files are not supported error. – Aleš Doganoc Nov 22 '17 at 20:06
  • The extension added a new entry in the connected services when right clicking dependencies called WCF Web Service Reference, I could discover my mex address of my running service. When I clicked on Finish it fails with the error: "The solution cannot be built, please try to rebuild the solution and fix any build problems.", although, my entire solution can be built. How can this be solved? Also: I've built the WCF libraries. Do I have to reference all DLLs in bin\ref\ now? Or from where I need to ref what libs? I only need net tcp communication. Thanks! – Martin Braun Nov 23 '17 at 00:31
  • If the extension does not work try the svcutil route. You can add the references by adding the packages from NuGet. The extension adds this packages: [System.ServiceModel.NetTcp](https://www.nuget.org/packages/System.ServiceModel.NetTcp/) [System.ServiceModel.Http](https://www.nuget.org/packages/System.ServiceModel.Http/) [System.ServiceModel.Duplex](https://www.nuget.org/packages/System.ServiceModel.Duplex/) [System.ServiceModel.Security](https://www.nuget.org/packages/System.ServiceModel.Security/) – Aleš Doganoc Nov 23 '17 at 07:53
  • Thanks I managed to get it compiled by generating a proxy with svcutil. Thanks for the NuGet packages. Unfortunately I get unhandled exceptions when I try to communicate with the server and it does not provide any further information, but I guess this can be considered a different problem. I mark your answer as right. (I'm sorry that you did not get the full 100 points, I don't know why StackOverflow doesn't give me more time to make a decision about who should get the full reward) – Martin Braun Nov 26 '17 at 22:29