Now that the Indigo Roadshow is officially over, I’m hoping to have some time to breathe and recap.  After PDC, Payam and I did a whirlwind of stops through Chicago, Philadelphia, and Columbus.  Payam, who’s Canadian and very well-traveled, kept complaining that all American cities look alike, and I kept refraining from hitting him.  The tours went well for the most part—I was pretty exhausted by the end, especially since I went on the tour straight from a very tiring PDC, but I had a good time regardless

 

I had several requests to post the code from the demos.  The code from my first demo, the P2P chat demo, is available on the DVDs we handed out, but I’ll post the code from the Magic8Ball demo that Payam did below, doing my best to format it reasonably.  I think Richard will post some more complex versions soon, showing off IIS hosting among other things.  

 

All of this is written in C# using Indigo Beta1 on Whidbey Beta2.  Okay, here goes:

 

-create a new Console App.  Call it Magic8BallService.

-add a reference to System.ServiceModel (r-click on the Magic8BallService project name in the Solution Explorer and choose Add Reference)

-paste the following code into Program.cs:

 

using System;

using System.Collections.Generic;

using System.Text;

using System.ServiceModel;

 

namespace Magic8Ball

{

    class Program

    {

        static void Main(string[] args)

        {

            ServiceHost<Magic8BallService> service =

                new ServiceHost<Magic8BallService>();

 

            service.AddEndpoint(typeof(Magic8BallService),

                new BasicProfileBinding(),

                new Uri("http://localhost/magic8ballservice"));

 

            service.Open();

 

            Console.WriteLine("Service has been started.");

            Console.ReadKey();

            service.Close();

            Console.WriteLine("Serivce has been shut down.");

 

        }

    }

 

    [ServiceContract]

    class Magic8BallService

    {

 

        [OperationContract]

        public string GiveMeTheAnswer()

        {

 

            string answer = AnswerGenerator.GiveMeTheAnswer();

            Console.WriteLine("  " + answer);

            return answer;

 

        }

    }

}

 

-add a new C# class to the solution and call it AnswerGenerator.cs, and paste in the following code:

 

using System;

using System.Collections.Generic;

using System.Text;

 

namespace Magic8Ball

{

    class AnswerGenerator

    {

 

        public static string GiveMeTheAnswer()

        {

            int p = 103; // prime

            string[] answers = {"Outlook Good", "Outlook Not So Good", "My Reply Is No",

                                "Don't Count On It", "You May Rely On It", "Ask Again Later",

                                "Most Likely", "Cannot Predict Now", "Yes", "Yes Definitely",

                                "Better Not Tell You Now", "It Is Certain", "Very Doubtful",

                                "It Is Decidedly So", "Concentrate and Ask Again", "Signs Point to Yes",

                                "My Sources Say No", "Without a Doubt", "Reply Hazy, Try Again",

                                "As I See It, Yes", "Ask Ari"};

            Random rand = new Random();

            int r = ((int) Math.Round(rand.NextDouble() * p)) % answers.Length;

            if (r < 0 || r >= answers.Length)

                return answers[0];

            else

                return answers[r];

           

 

 

 

        }

    }

}

 

(Payam’s got a better one, but this is the one I have.  Yes, it’s not very good.)

-that’s it:  build and run, and you’ve got a working Indigo service.  You can point a web browser at http://localhost/magic8ballservice, and you should be greeted with a welcome page.  Congratulations!

 

Now on to the client.  Remember, we’re pretending that the client and service know nothing about each others’ implementations, so we’re going to do all the client work in a different instance of Whidbey.

-start up a new instance of VS05, and create a new console application (I call mine Magic8BallClient, but I’m unoriginal).

-add a reference to System.ServiceModel

-now we have to get the metadata from the service (which is still running) to generate a proxy on the client side.  Open up a Visual Studio Command Prompt and type:  svcutil http://localhost/Magic8BallService?wsdl .  That’ll generate a couple of files in that directory that we’ll need to add to our client solution.

-add tempuri.org.cs and output.config to the Magic8BallClient solution.  (I do this by running start . in my VS Command Prompt and dragging the files into the Solution Explorer.)

-rename output.config to app.config

-paste the following code into Program.cs

 

using System;

using System.Collections.Generic;

using System.Text;

using System.ServiceModel;

 

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

 

            Magic8BallServiceProxy proxy = new Magic8BallServiceProxy();

            Console.WriteLine(proxy.GiveMeTheAnswer());

 

        }

    }

}

 

-build and run, and you’ve got a working Magic8Ball simulator

 

There are a lot of ways to change this, of course, but I’ll leave most of those as an exercise for the readers.  I’m just going to offer one tweak, and that’s moving the declaration of the endpoint in the service app from code to config.  Here’s how to do it:

-shut down the service by pressing a key in the service console window.

-comment out the three lines that declare the endpoint in Program.cs of Magic8BallService:

            //service.AddEndpoint(typeof(Magic8BallService),

            //    new BasicProfileBinding(),

            //    new Uri("http://localhost/magic8ballservice"));

 

-add a new application configuration file to the solution, and paste in the following code:

 

<configuration>

      <system.serviceModel>

            <services>

                  <service serviceType="Magic8Ball.Magic8BallService">

                        <endpoint

                    address="http://localhost/magic8ballservice"

                    bindingSectionName="basicProfileBinding"

                    contractType="Magic8Ball.Magic8BallService" />

                  </service>

            </services>

      </system.serviceModel>

</configuration>

 

-build and run, and see that it works just as before, only now you’ve got your endpoint defined in config instead of code.  Amazing!

 

Well, that’s it for me now.  Feel free to leave a comment/question.