none
Can't get simple WCF Web Client to work RRS feed

  • Question

  • I have created a WCF Web Service using a VS2012. It is running on my IIS on my machine. I can call the WCF service routine from VS2012 application fine.   I cannot successfully call the methods on the web service in my UWP application (win86)

    The service uses a BasicHttpBinding as follows:

     BasicHttpBinding binding = new BasicHttpBinding();

    EndpointAddress address = new EndpointAddress(new Uri(_webUrl));

     _myWebservice = new ScannerCollectionWCFClient(binding, address);

    I then call the method provided like this:

    _myWebservice.Test();   //This returns a string

    I get an error that indicates that it cannot communication with the server.   I've tried this both Asynchronously using the generated proxy and using the code generated by SvcUtils (example above).   Neither works.  

    Assistance please..

    Thursday, May 12, 2016 9:43 PM

Answers

  • With help from Microsoft, the Windows Universal Platform requires setting Capabilities.   (Double click on Package.AppxManifest  and set Capabilities on for "Private Networks".)   I was then able to communicate with a network web service.   I still had problems with a local web service but am at least able to move forward.... Thanks to MS for the answer.


    • Marked as answer by GPeele Friday, May 27, 2016 2:02 PM
    • Edited by GPeele Friday, May 27, 2016 2:02 PM
    Friday, May 27, 2016 2:02 PM

All replies

  • Hello,

    I can call the WCF service with the basicHttpBinding from the UWP app successfully by using the Add Service Reference. Please try to check whether using the Add Service Reference works for you or not.
    In UWP app we need to call the WCF service method asynchronously as following:

    _myWebservice.TestAsync(); 

    If the above information can not help, could you please show us the detailed error information that you have met when you try to consume the WCF service from the UWP app?

    Besides, it will be better if you can share us a simple reproduced project in here.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, May 13, 2016 6:05 AM
    Moderator
  • Thanks...as I stated in my original email, I used the proxy generated by creating a reference to the web service and calling the method asynchronously which means I called _myWebservice.TestAsync();  using the async/await methodology which produced the exact same results.

    This is part of a larger project.   If you like, I can create a sample project for testing purposes.   However, I'm not sure how to upload it for you to see.    The code is exactly like the other code I've used before.   I've done many WCF services in the non-universal platform and it seems as if the problem is unique to this environment.

    Friday, May 13, 2016 12:39 PM
  • I created a new project to test.  The project has a single button on the screen.  I get an error communicating with the web service.  The entire code in the project is as follows:

    public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
            }
            private string _webUrl = "http://localhost/ScannerCollectionWeb/ScannerCollectionWCF.svc";
            private async void btnTest_Click(object sender, RoutedEventArgs e)
            {
                BasicHttpBinding binding = new BasicHttpBinding();
                EndpointAddress address = new EndpointAddress(new Uri(_webUrl));
                var myWebservice = new InputOutput.ScannerCollectionWCFClient(binding, address);
                try
                {
                    string result = await myWebservice.TestAsync();
                    MessageDialog msgDialog = new MessageDialog(result, "Result");
                    await msgDialog.ShowAsync();
                }
                catch (Exception Ex)
                {
                    MessageDialog msgError = new MessageDialog(Ex.Message, "Error");
                    await msgError.ShowAsync();
                }
                finally
                {
                    await myWebservice.CloseAsync();
                }
            }
        }

    Friday, May 13, 2016 2:40 PM
  • Error information is as follows (Ex = exception):

    Ex.Message

    An error occurred while sending the request.

    Ex.InnerException

    The text associated with this error code could not be found.

    A connection with the server could not be established

    HResult

    0x80131500

    Stack Trace:

       at System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult result)
       at System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult result)
       at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)
       at System.ServiceModel.Channels.ServiceChannelProxy.TaskCreator.<>c__DisplayClass0.<CreateGenericTask>b__1(IAsyncResult asyncResult)
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
       at TestClient2.MainPage.<btnTest_Click>d__2.MoveNext()

    Friday, May 13, 2016 7:44 PM
  • Hi FPeele,

    Thanks for your posting!

    I implement the service by Add Service Reference and try two bindings for the service. All of them worked fine.

     BasicHttpBinding binding = new BasicHttpBinding();
                EndpointAddress address = new EndpointAddress(new Uri(_webUrl));
                //var myWebservice = new InputOutput.ScannerCollectionWCFClient(binding, address); //your configuration
                //myWebservice.Service1Client client = new myWebservice.Service1Client(myWebservice.Service1Client.EndpointConfiguration.BasicHttpBinding_IService1);// method 1
                myWebservice.Service1Client client = new myWebservice.Service1Client(binding, address); //method2

    However, I want to know how do you encapsulate the function of creating client in your "InputOutput.ScannerCollectionWCFClient".

    Since UWP can call the WCF service well, So I suggest you can check this points:

    1. Please mark sure your service and your UWP application in one machine, because your service is not published. If you want to call it in different PC, you need publish your service.

    2.Please try to call your service on the browser. If your browser didn't call your service, I think your should start your WCF service. Or make sure your WCF client is starting.

    The code below is my simple demo:

    1. WCF:

    2. UWP:

    Please try it. Any updates or results please let me know. 

    Regards,

    Will


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, May 17, 2016 7:23 AM
  • My code is exactly like yours and the service reference is correct as well.   There must be something wrong on my machine.    The service is working great when called with VS 2012.   It is just the Universal Environment that does not work.   It also shows up fine in the browser.   Gregg


    • Edited by GPeele Tuesday, May 24, 2016 6:44 PM
    Tuesday, May 24, 2016 6:43 PM
  • This works from VS 2012:

    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private string _webUrl = "http://127.0.0.1/ScannerCollectionWeb/ScannerCollectionWCF.svc";

            private void btnTest_Click(object sender, EventArgs e)
            {

                BasicHttpBinding binding = new BasicHttpBinding();
                EndpointAddress address = new EndpointAddress(new Uri(_webUrl));
                InputOutput.ScannerCollectionWCFClient myWebservice = new ScannerCollectionWCFClient(binding, address);

                try
                {
                    myWebservice.TestCompleted += new EventHandler<TestCompletedEventArgs>(TestCallback);
                    myWebservice.TestAsync();

                }
                catch (Exception Ex)
                {
                    MessageBox.Show(Ex.Message + Ex.InnerException, "Error");
                }
                finally
                {
                    myWebservice.Close();
                }
            }

            static void TestCallback(object sender, TestCompletedEventArgs e)
            {
                MessageBox.Show(e.Result);
            }

        }


    • Edited by GPeele Tuesday, May 24, 2016 7:25 PM
    Tuesday, May 24, 2016 7:24 PM
  • This works using a form application in VS 2015:

    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private string _webUrl = "http://127.0.0.1/ScannerCollectionWeb/ScannerCollectionWCF.svc";

            private async void btnTest_Click(object sender, EventArgs e)
            {
                BasicHttpBinding binding = new BasicHttpBinding();
                EndpointAddress address = new EndpointAddress(new Uri(_webUrl));
                InputOutput.ScannerCollectionWCFClient myWebservice = new ScannerCollectionWCFClient(binding, address);

                string result;

                try
                {
                    result = await myWebservice.TestAsync();
                    MessageBox.Show(result);
                }
                catch (Exception Ex)
                {
                    MessageBox.Show(Ex.Message + Ex.InnerException, "Error");
                }
                finally
                {
                    myWebservice.Close();
                }
            }
        }


    • Edited by GPeele Tuesday, May 24, 2016 7:37 PM
    Tuesday, May 24, 2016 7:37 PM
  • Tried to get WSHttp binding to work....But after getting my forms application to work,  I discover that Windows Universal does not support this binding...

    Out of luck...

    Wednesday, May 25, 2016 2:41 PM
  • With help from Microsoft, the Windows Universal Platform requires setting Capabilities.   (Double click on Package.AppxManifest  and set Capabilities on for "Private Networks".)   I was then able to communicate with a network web service.   I still had problems with a local web service but am at least able to move forward.... Thanks to MS for the answer.


    • Marked as answer by GPeele Friday, May 27, 2016 2:02 PM
    • Edited by GPeele Friday, May 27, 2016 2:02 PM
    Friday, May 27, 2016 2:02 PM