Exceptions and WCF  
Author Message
SnowJim





PostPosted: Windows Communication Foundation ("Indigo"), Exceptions and WCF Top

Hey!

I have read the msdn documentation on handling faults in WSF but can’t get it to work.

I have the fallowing Interface fore the service:

[ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IChatChangeHandler))]
public interface IChatChangePub
{
[OperationContract(IsOneWay = false, IsInitiating = true)]
bool subscribe(string inNickName);
[OperationContract(IsOneWay = false, IsInitiating = true)]
void unsubscribe();

[OperationContract]
[FaultContract(typeof(userListFault))]
List<string> getUserList(string inNickName);

[OperationContract(IsOneWay = true)]
void publishChatMessage(chatMessage inMessage);
}

As you can see i am using a costum faultExcetion(userListFault) that looks like this:

[DataContract]
public class userListFault
{
private string mOperation;
private string mProblemType;

public userListFault(string inOperation, string inProblemType)
{
mOperation = inOperation;
mProblemType = inProblemType;
}

[DataMember]
public string operation
{
get { return mOperation; }
set { mOperation = value; }
}
[DataMember]
public string problemType
{
get { return mProblemType; }
set { mProblemType = value; }
}
}

In my ServiceContract i throwing a fault like this:

public List<string> getUserList(string inNickName)
{
throw new FaultException<userListFault>(new userListFault("testOperation", "testProblemType"), new FaultReason("myReason"));
}

On the client i am using the fallowing code to catch:

private void InitializeConnection()
{
try
{
updateUserList(proxyChatChange.getUserList(txtNickName.Text));
}
catch (Exception ex)
{
MessageBox.Show("Error in InitializeConnection: " + ex.Message);
}
}

When debugging this i am getting a unhandle exception on the
throw new FaultException<userListFault> What am I missing


Visual Studio 200814  
 
 
Carlos Figueira - MSFT





PostPosted: Windows Communication Foundation ("Indigo"), Exceptions and WCF Top

What's the error you're getting on the call to throw new FaultException<userListFault> I tried the program below, and it worked fine (i.e., the client received the FaultException<userListFault> back):

[ServiceContract]
public interface IChatChangeHandler
{
  [OperationContract]
  void Foo(string str);
}
[ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IChatChangeHandler))]
public interface IChatChangePub
{
  [OperationContract(IsOneWay = false, IsInitiating = true)]
  bool subscribe(string inNickName);
  [OperationContract(IsOneWay = false, IsInitiating = true)]
  void unsubscribe();

  [OperationContract]
  [FaultContract(typeof(userListFault))]
  List<string> getUserList(string inNickName);

  [OperationContract(IsOneWay = true)]
  void publishChatMessage(string inMessage);
}

[DataContract]
public class userListFault
{
  private string mOperation;
  private string mProblemType;

  public userListFault(string inOperation, string inProblemType)
  {
    mOperation = inOperation;
    mProblemType = inProblemType;
  }

  [DataMember]
  public string operation
  {
    get { return mOperation; }
    set { mOperation = value; }
  }
  [DataMember]
  public string problemType
  {
    get { return mProblemType; }
    set { mProblemType = value; }
  }
}
public class Service : IChatChangePub
{
  public bool subscribe(string inNickName)
  {
    return false;
  }
  public void unsubscribe() { }
  public List<string> getUserList(string inNickName)
  {
    throw new FaultException<userListFault>(new userListFault("testOperation", "testProblemType"), new FaultReason("myReason"));
  }
  public void publishChatMessage(string inMessage) { }
}

public class Host
{
  class ClientContextClass : IChatChangeHandler
  {
    public void Foo(string str)
    {
      Console.WriteLine(str);
    }
  }
  public static void Main()
  {
    ServiceHost host = new ServiceHost(typeof(Service), new Uri("net.tcp://localhost:8000/Service"));
    host.AddServiceEndpoint(typeof(IChatChangePub), new NetTcpBinding(), "");
    host.Open();

    DuplexChannelFactory<IChatChangePub> factory = new DuplexChannelFactory<IChatChangePub>(new InstanceContext(new ClientContextClass()),
      new NetTcpBinding(), new EndpointAddress("net.tcp://localhost:8000/Service"));
    IChatChangePub proxy = factory.CreateChannel();
    try
    {
      List<string> userList = proxy.getUserList("hello");
      Console.WriteLine(userList.Count);
      ((IChannel)proxy).Close();
    }
    catch (Exception ex)
    {
      Console.WriteLine("Exception caught: " + ex);
      ((IChannel)proxy).Abort();
    }
    factory.Close();
    host.Close();
  }
}

 
 
SnowJim





PostPosted: Windows Communication Foundation ("Indigo"), Exceptions and WCF Top

Sorry for the delay, I am not reciving any notification about updated thread.

If i Run in debug mode, it stops(without break point) on the: throw new FaultException("test");
And says somthing about unhandle exception, but if i click resume, or run the project without debug mode then it works fine, the fault exception message are recivied on the client.