Partager via


A Trick with Faults

What does this code print? It seems like both choices are quite reasonable. I'll have some discussion about this tomorrow.

 [ServiceContract]
interface IService
{
   [OperationContract(Action="foo")]
   Message Verb(Message input);
}

class Service : IService
{
   public Message Verb(Message input)
   {
      throw new FaultException("boo!");
   }
}

class Program
{
   static void Service()
   {
      ServiceHost host = new ServiceHost(typeof(Service), new Uri("net.tcp://localhost/"));
      host.AddServiceEndpoint(typeof(IService), new NetTcpBinding(), "");
      host.Open();
      Console.ReadLine();
   }

   static void Main(string[] args)
   {
      new Thread(new ThreadStart(Service)).Start();
      Binding binding = new NetTcpBinding();
      ChannelFactory<IService> factory = new ChannelFactory<IService>(binding, "net.tcp://localhost/");
      IService proxy = factory.CreateChannel();
      try
      {
         Message response = proxy.Verb(Message.CreateMessage(binding.MessageVersion, "foo"));
         Console.WriteLine("Received message");
         Console.WriteLine(response.ToString());
      }
      catch (FaultException fault)
      {
         Console.WriteLine("Received fault");
         Console.WriteLine(fault.ToString());
      }
   }
}

Next time: A Trick with Faults (Discussion)

Comments

  • Anonymous
    February 27, 2007
    In the channel development series last week, we looked at the characteristics of channels (protocol channels,