1

Have a Windows 10 Mobile app that needs to make calls to a WCF service. This is an existing service, running on another server (c#/IIS) and I can connect to the service just fine and make calls from my Xamarin.Android app, using the following code:

    public PacTracMobileServerClient GetClient()
    {
        var config = new Config.AppConfig();
        var serverUrl = config.GetODSServerAddress;

        var binding = new CustomBinding();
        //TODO: Make these timeouts configurable
        binding.SendTimeout = TimeSpan.FromMinutes(2);
        binding.ReceiveTimeout = TimeSpan.FromMinutes(2);

        var tme = new TextMessageEncodingBindingElement(MessageVersion.Soap11, Encoding.UTF8);
        binding.Elements.AddRange(tme);

        if (serverUrl.StartsWith("https", StringComparison.CurrentCultureIgnoreCase))
        {
            //HTTPS binding
            var https = new HttpsTransportBindingElement
            {
                MaxReceivedMessageSize = int.MaxValue,
                MaxBufferSize = int.MaxValue,
                RequireClientCertificate = false,
            };
            binding.Elements.Add(https);

            //Trust all certificates
            //ServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
        }
        else
        {
            //HTTP binding
            var http = new HttpTransportBindingElement
            {
                MaxReceivedMessageSize = int.MaxValue,
                MaxBufferSize = int.MaxValue
            };
            binding.Elements.Add(http);
        }

        var endPoint = new EndpointAddress(new Uri(serverUrl));

        return new PacTracMobileServerClient(binding, endPoint);
    }

Again, the above is working in Xamarin.Android (although I will be happy to improve on it if there are suggestions). Since my Xamarin.Android implementation was .Net, I was hoping it just working with this Windows UWP app. Everything seems to be fine except for the line that was commented out for trusting all certificates as it seems ServicePointManager is not supported in UWP?

All that said, I am getting the following error when I try to make a call using this client in my UWP app

net_http_client_execution_error

So, two questions.

  1. What is wrong with my code above and how can I change it to work with my UWP app so I can call the WCF service?

  2. I am wondering if my issue is because of the certificates as the only difference between Android (working) and UWP (not working) is the line I had to comment out for the ServicePointManager. How can I implement something similar for UWP? Basically, this app is only used internally and I don't want or need to worry about certificates when calling the service.

Note: I did make sure to enable capabilities for Internet (Client & Server), Internet (Client) and Private Networks (Client & Server) in my package manifest. Also, I did verify I am connected to the network and can access the service from the web browser on the mobile device.

Thanks!

Michael Bedford
  • 1,742
  • 20
  • 48
  • 1
    My first thought was that the service might be running on localhost which might be disallowed from a UWP application, if so take a look at [UWP Enable local network loopback](http://stackoverflow.com/questions/33259763/uwp-enable-local-network-loopback). You might be able to address #2 with looking at this solution to [Accepting invalid SSL certificates using WinRT](http://stackoverflow.com/a/28762877/665106). – ShelbyZ Dec 13 '16 at 04:22
  • Thank you for both of these, will try them both. For further info, i am deploying to mobile device, via usb, from my development environment and trying from mobile device while running in debug and mobile device is connected to my network via wifi, which has access to the server. I would think this would work and be allowed. – Michael Bedford Dec 13 '16 at 04:59
  • @ShelbyZ I tried both, sort of. First, I dont have a loopback issue. Second, I confirmed my problem is likely due to the certificate issue. I read your second link which looks like what i need to ignore certificate issues but it uses httpbaseprotocolfilter which you then pass in to a httpclient but as you can see from my code snippet above, I use the wcf client that was implemented by adding a service reference which does not use httpclient. How can I still implement these filters in my wcf code? – Michael Bedford Dec 14 '16 at 04:42
  • Yeah, I suppose I missed that fact when originally looking at the issue. You might try looking into [Consuming a SOAP service using HttpClient](https://long2know.com/2016/07/consuming-a-soap-service-using-httpclient/) which I would agree isn't the best solution, but you would be able to add the protocol filter for the certificate issue. The only other option might be looking at the certificate used by the service to see if it is indeed invalid (expired, etc) and fix that issue. – ShelbyZ Dec 15 '16 at 17:35

0 Answers0