none
WCF Faults - custom types

    Question

  • I have a silverlight 4 project that calls several WCF services I've written.  In the WCF services, the first thing I do in each method is an Access Check to make sure the user is logged in and authorized.  I've just started trying to catch exceptions using faults and having the following problem.

    -  When a session is timed out (or a user isn't authorized) I send a SecurityFault (which is just a custom class with a couple of properties in it) back to my silerverlight project.

    -  In my silverlight project, I'm attempting to do the following:

    void callbackMethod(sender s, EventArgs e)
    {
        if (e.Error is FaultException<SecurityFault>)
        {
            FaultException<SecurityFault> fault = e.Error as FaultException<SecurityFault>;
            //do some stuff with the fault here
        }
        else
        {
           // no fault thrown, proceed normally
        }
    }

    However, when I define multiple methods on different services as having [FaultContract(typeof(SecurityFault))] then my compiler (in SL) wants me to specify which service my <SecurityFault> comes from.  For example, it wants me to specify:

             

    if (e.Error is FaultException<namespace.service1.SecurityFault>)

    so if I want the same generic code to handle faults from multiple services I need to write:

     if (e.Error is FaultException<namespace.service1.SecurityFault>
    || e.Error is FaultException<namespace.service2.SecurityFault>
    || e.Error is FaultException<namespace.service3.SecurityFault>)


    Is there anyway to avoid having to do this?


    Thanks and Merry Christmas,

    Trevor

    Friday, December 24, 2010 5:37 PM

Answers

  • I'm going to assume you are using Add Service Reference in your Silverlight Project. That creates a few problems for you, specifically it creates new classes in the namespace of your services, thus there are different "versions" of your SecurityFault.

    To get around that issue, you simply need to put your SecurityFault class in a seperate Dll and reference that Dll from your silverlight project. Then update your services. That way your services will not create its own version of SecurityFault.

    That's the quick and easy solution. You'll encounter the same problem with any other objects you are trying to share and re-use. It is a good practice to have all your model objects in a seperate Dll. If you can't do that (like us) then you might want to investigate on how to create your own service factory and service clients. It is not really that difficult, just time consuming.

    Wednesday, December 29, 2010 10:18 AM

All replies

  • I'm going to assume you are using Add Service Reference in your Silverlight Project. That creates a few problems for you, specifically it creates new classes in the namespace of your services, thus there are different "versions" of your SecurityFault.

    To get around that issue, you simply need to put your SecurityFault class in a seperate Dll and reference that Dll from your silverlight project. Then update your services. That way your services will not create its own version of SecurityFault.

    That's the quick and easy solution. You'll encounter the same problem with any other objects you are trying to share and re-use. It is a good practice to have all your model objects in a seperate Dll. If you can't do that (like us) then you might want to investigate on how to create your own service factory and service clients. It is not really that difficult, just time consuming.

    Wednesday, December 29, 2010 10:18 AM
  • Thank you very much for your solution.  Worked like a charm.  Does this work because the SecurityFault classes are already defined (when you include the dll in the SL project) so the act of building the service references doesn't re-define them?  

    Thursday, January 06, 2011 12:04 PM
  • Yep. That's pretty much the reason.

    Monday, January 10, 2011 11:04 AM
  • Has something changed in SL5?  When I upgraded I started having this same problem again.  The dll that contains my custom types has also been changed to target SL5 and all my WCF services have been updated.  Anyone have any thoughts?

    Friday, March 16, 2012 6:14 PM