Tonight I spent some time reviewing the latest batch of sample code for Volume 2 of the SLAR.  As you may recall, Volume 2 covers System.Xml, System.Net, System.Reflection namespaces (among others).  I really want every type and nearly every member in the book to have a real, compliable and run-able code sample that shows common usage. 

 

This poses a little bit of a problem for the System.Net samples.  Many of them need to refer to a working webserver and some of them require special behavior from that server.    I could just point these all at http://localhost, but I fear that would mean many of them would not run correctly on most customer’s machines unless they do some configuration. 

 

Thoughts on how to handle this? 

 

BTW – if you are interested in being a reviewer for Vol2, please let me know

 

Here are a couple of examples:

 

  public class EndPointSample

  {

    public static void Main()

    {

      IPAddress ip = IPAddress.Parse("127.0.0.1");

      IPEndPoint ep = new IPEndPoint(ip, 9999);

      Console.WriteLine("EndPoint.AddressFamily = '{0}'",

                        ep.AddressFamily.ToString());

      SocketAddress sktaddr = new

                    SocketAddress(AddressFamily.InterNetwork);

      EndPoint newep = (EndPoint)ep.Create(sktaddr);

      Console.WriteLine("New EndPoint.AddressFamily = '{0}'",

                        newep.AddressFamily.ToString());

    }

  }

 

  public class DnsSample

  {

    private static bool bDone = false;

 

    public static void Main()

    {

      String toFind = "microsoft.com";

      IAsyncResult dummy = Dns.BeginResolve(toFind, new AsyncCallback(DnsCallback), null);

      while(!bDone) {}

    }

 

    private static void DnsCallback(IAsyncResult ar)

    {

      IPHostEntry host = Dns.EndResolve(ar);

      ShowHostDetails(host);

      bDone = true;

    }

 

    private static void ShowHostDetails(IPHostEntry host)

    {

      Console.WriteLine("HostName = '{0}'", host.HostName);

      foreach (IPAddress addr in host.AddressList)

      {

        Console.WriteLine("IPAddress = {0}", addr.ToString());

      }

      foreach (String alias in host.Aliases)

      {

        Console.WriteLine("Alias = {0}", alias);

      }

    }

 

  }

  public class HttpStatusCodeSample

  {

    public static void Main()

    {

      HttpWebRequest req = (HttpWebRequest)

                     WebRequest.Create("http://localhost");

      HttpWebResponse result = (HttpWebResponse)req.GetResponse();

      Console.WriteLine("HttpWebResponse.StatusCode = {0}",

                         result.StatusCode.ToString());

    }

  }