Welcome to MSDN Blogs Sign in | Join | Help

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)

Published Tuesday, February 27, 2007 5:00 AM by Nicholas Allen
Filed under: , ,

Comments

Wednesday, February 28, 2007 4:38 AM by Nicholas Allen's Indigo Blog

# Channels Illustrated

In the channel development series last week, we looked at the characteristics of channels (protocol channels,

New Comments to this post are disabled
 
Page view tracker