Chapter 3 code

150 views
Skip to first unread message

Ryan Wittibschlager

unread,
Apr 5, 2014, 6:27:16 PM4/5/14
to restinp...@googlegroups.com
I am using the POX Client (.NET) and WCF POX Service (.NET) projects from the Ch. 3 source code. I had to make a few edits in the projects to compile successfully (just minor variable name and type edits). However I can't get the POX Client main program to run. I get the following exception thrown:

"An unhandled exception of type 'System.ServiceModel.EndpointNotFoundException' occurred in mscorlib.dll

Additional information: Could not connect to http://ryan-pc:7070/PlaceOrder. TCP error code 10061: No connection could be made because the target machine actively refused it. "

I've tried disabling my firewall to no avail. I am using VS2010 .NET 3.5 on a Windows 7 machine. Thanks.

Jim Webber

unread,
Apr 5, 2014, 7:14:05 PM4/5/14
to restinp...@googlegroups.com
Hi Ryan,

This is a bit fiddly to debug remotely. But firstly, try using http://127.0.0.1:7070/PlaceOrder instead of http://ryan-pc:7070/PlaceOrder just in case there's some DNS things going on.

Then let us know if it succeeded.

Jim
> --
> You received this message because you are subscribed to the Google Groups "restinpractice" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to restinpractic...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Ryan Wittibschlager

unread,
Apr 5, 2014, 9:00:15 PM4/5/14
to restinp...@googlegroups.com
Thanks for the quick reply, I still receive a similar error message though:

"An unhandled exception of type 'System.ServiceModel.EndpointNotFoundException' occurred in mscorlib.dll

Additional information: Could not connect to http://127.0.0.1:7070/PlaceOrder. TCP error code 10061: No connection could be made because the target machine actively refused it 127.0.0.1:7070. "

Jim Webber

unread,
Apr 6, 2014, 2:20:16 AM4/6/14
to restinp...@googlegroups.com
Hi Ryan,

OK, next check.

Are you actually running a Web server process on port 7070?

That is, have you run the server part of your code before you start the client?

Jim

On 6 Apr 2014, at 12:59, Ryan Wittibschlager <ryanpau...@gmail.com> wrote:

> Thanks for the quick reply, I still receive a similar error message though:
>
> "An unhandled exception of type 'System.ServiceModel.EndpointNotFoundException' occurred in mscorlib.dll
>
> Additional information: Could not connect to http://127.0.0.1:7070/PlaceOrder. TCP error code 10061: No connection could be made because the target machine actively refused it 127.0.0.1:7070. "

Ryan Wittibschlager

unread,
Apr 6, 2014, 10:11:54 AM4/6/14
to restinp...@googlegroups.com
I did not start any Server process. Here is the code that is executed:

            string baseAddress = "http://127.0.0.1:7070/";

            var factory = new ChannelFactory<IOrderingService>(new WebHttpBinding(), new EndpointAddress(baseAddress));

            factory.Endpoint.Behaviors.Add(new WebHttpBehavior());

            IOrderingService client = factory.CreateChannel();

            var order = new Order { CustomerId = "1234", Items = new List<Item> { new Item() { ItemId = "abc", Quantity = 101 }, new Item() { ItemId = "def", Quantity = 202 } } }; 

For whatever reason I was thinking something with the factory was starting a server process. The next line of code:

            OrderConfirmation response = client.PlaceOrder(order);

is where it throws the exception.
I am very new to this stuff so I'm not sure exactly what is going on (I've tried looking up the ChannelFactory class, it was a bit confusing). If all I need to do is run a web server process on my machine then let me know how to go about doing this.

Also, when the code executes, right before it throws an exception I executed a 'netstat -anb' command to see if it was listening on the 7070 port. This is the response:


Active Connections

  Proto  Local Address          Foreign Address        State
  TCP    0.0.0.0:80             0.0.0.0:0              LISTENING
...
...
 [System]
  TCP    127.0.0.1:52819        127.0.0.1:7070         SYN_SENT

Thanks.

Jim Webber

unread,
Apr 7, 2014, 12:13:32 AM4/7/14
to restinp...@googlegroups.com
Hi Ryan,

> I did not start any Server process. Here is the code that is executed:

Then that's the reason the exception is telling you there's nothing listening. You'll need to spin up the server so that your client code can connect to it.

> For whatever reason I was thinking something with the factory was starting a server process. The next line of code:
>
> OrderConfirmation response = client.PlaceOrder(order);
>
> is where it throws the exception.

No, that's just the client calling (or trying to call) across the wire to the server.

Think about it like this: if the ordering service was a real e-commerce API, then a remote customer would have no business in starting and stopping it: that's strictly the concern of the service owner. It's just that in this case you play both roles :-)

Assuming you want to use WCF, Page 72 in the book gives you an example (using PUT) of how most of the server code should look. You just need to wrap it in some config and deploy to a Web server, or create a main method like: http://msdn.microsoft.com/en-us/library/ms731758(v=vs.110).aspx

Hope that helps,

Jim

Ryan Wittibschlager

unread,
Apr 11, 2014, 10:17:37 PM4/11/14
to restinp...@googlegroups.com
Jim,

Thanks for the information, I couldn't have solved my problem without your help and that link you gave me.

For anyone else interested, you will have to do the following to make this project work:

(Side note:  this was all done in Visual studio 2010 Professional, .NET 3.5 framework)

1. Correct the type mismatches. In the WcfPox solution, you can look in the IOrderingService.cs file to see how the service is implemented (e.g., the Quantity member of the Item class is an int, and should be treated so in the Program.cs file within the PoxClient solution).

2. There is also a variable name mismatch. The IOrderingService defines the the OrderConfirmation class to return string data member titled orderId , however there are several locations in the Program.cs file (within the PoxClient solution) that refer to it as OrderId with a capital O. Correct either Program.cs or the IOrderingService interface definition.

3. I never had a problem with Environment.MachineName , but as Jim suggested you should consider changing baseAddress string to use 127.0.0.1 instead of Environment.MachineName if you are experiencing issues.

4. Most importantly, you need to add the following public static void Main() method to the OrderingService class in your OrderingService.cs file (within the WcfPox solution).

// Host the service within this EXE console application.
        public static void Main()
        {
            using (ServiceHost serviceHost = new ServiceHost(typeof(OrderingService)))
            {
                try
                {
                    // Open the ServiceHost to start listening for messages.
                    serviceHost.Open();

                    // The service can now be accessed.
                    Console.WriteLine("The service is ready.");
                    Console.WriteLine("Press <ENTER> to terminate service.");
                    Console.ReadLine();

                    // Close the ServiceHost.
                    serviceHost.Close();
                }
                catch (TimeoutException timeProblem)
                {
                    Console.WriteLine(timeProblem.Message);
                    Console.ReadLine();
                }
                catch (CommunicationException commProblem)
                {
                    Console.WriteLine(commProblem.Message);
                    Console.ReadLine();
                }
            }
        }

I am surprised the authors left this out of their code example.... This was the key to making everything work. As Jim suggested, you actually need to start the service!

5. Add a reference to the System.configuration dll. 

6. Open each solution with administrator privileges. Not sure if this matters for the client solution, but you will need admin rights to start your WCF service. First, start your WcfPox solution because this is the actual service that will open the port for listening. Then, run your PoxClient solution. Should be good.
Reply all
Reply to author
Forward
0 new messages