Exemple #1
0
        static void Main(string[] args)
        {
            Person[] people = new Person[]
            {
                new Person {
                    Mark = 46, Name = "Jim"
                },
                new Person {
                    Mark = 73, Name = "Mike"
                },
                new Person {
                    Mark = 92, Name = "Stefan"
                },
                new Person {
                    Mark = 24, Name = "Arthur"
                }
            };
            WriteLine("People:");
            OutputPeople(people);
            IAwardService client = ChannelFactory <IAwardService> .CreateChannel(
                new WSHttpBinding(),
                new EndpointAddress("http://localhost:59558/AwardService.svc"));

            client.SetPassMark(70);
            Person[] awardedPeople = client.GetAwardedPeople(people);
            WriteLine();
            WriteLine("Awarded people:");
            OutputPeople(awardedPeople);
            ReadKey();
        }
        //Difference in this example is that no metadata is required by  the client, as the client has access to the
        //contract assembly.Instead of generating a proxy class from metadata, the client obtains the refence to the
        //service contract interface through an alternative method. Another point to note about this example is the
        //use of a session to maintain state in the servie, which requires the WSHttpBinding binding istead of the
        //BasicHttpBinding binding


        static void Main(string[] args)
        {
            Person[] people = new Person[]
            {
                new Person {
                    Mark = 46, Name = "Jim"
                },
                new Person {
                    Mark = 73, Name = "Mike"
                },
                new Person {
                    Mark = 92, Name = "Stefan"
                },
                new Person {
                    Mark = 24, Name = "Arthur"
                }
            };
            WriteLine("People:");
            OutputPeople(people);

//The client application has no app.config file to configue the communications with the service and no proxy class defined from metadata
//to communicate with the service. Instead a proxy class is created through the ChannelFactory<T>.CreateChannel() method. This method
//creates a proxy class that implements the IAwardService client, although behind the scenes, the generated class communicates with the
//service just like the metadata-generated proxy shown earlier. Note if you create a porxy class with ChannelFactory<T>.CreateChannel(),
//the communication channel will, by default, time out after a minute, which can lead to communication errors. There are ways to keep
//connections alive, but they are beyond the scope of this chapter. Creating proxy classes this way is extremely useful technique that you
//can use to quickly generate a client application on the fly.

            IAwardService client = ChannelFactory <IAwardService> .CreateChannel(
                new WSHttpBinding(),
                new EndpointAddress("http://localhost:51735/AwardService.svc"));

            client.SetPassMark(70);
            Person[] awardedPeople = client.GetAwardedPeople(people);
            WriteLine();
            WriteLine("Awarded people:");
            OutputPeople(awardedPeople);
            ReadKey();
        }