c# - WCF exception handling using IErrorHandler -


i implementing ierrorhandler interface catch kinds of exceptions wcf service , sending client implementing providefault method.

i facing 1 critical issue however. of exceptions sent client faultexception disables client handle specific exceptions may have defined in service.

consider: someexception has been defined , thrown in 1 of operationcontract implementations. when exception thrown, converted fault using following code:

var faultexception = new faultexception(error.message); messagefault messagefault = faultexception.createmessagefault(); fault = message.createmessage(version, messagefault, faultexception.action); 

this send error string, client has catch general exception like:

try{...} catch(exception e){...} 

and not:

try{...} catch(someexception e){...} 

not custom exceptions someexception, system exceptions invalidoperationexception cannot caught using above process.

any ideas how implement behavior?

in wcf desirable use special exceptions described contracts, because client may not .net application has information standard .net exceptions. can define faultcontract in service , use faultexception class.

server side

[servicecontract] public interface isampleservice {     [operationcontract]     [faultcontractattribute(typeof(myfaultmessage))]     string samplemethod(string msg); }  [datacontract] public class myfaultmessage {     public myfaultmessage(string message)     {         message = message;     }      [datamember]     public string message { get; set; } }  class sampleservice : isampleservice {     public string samplemethod(string msg)     {         throw new faultexception<myfaultmessage>(new myfaultmessage("an error occurred."));     }         } 

in addition, can specify in configuration file server returns exception details in it's faultexceptions, not recommended in production application:

<servicebehaviors>    <behavior>    <!-- receive exception details in faults debugging purposes, set value below true.  set false before deployment avoid disclosing exception information -->       <servicedebug includeexceptiondetailinfaults="true"/>    </behavior> </servicebehaviors> 

after that, can rewrite method handling exceptions:

var faultexception = error faultexception; if (faultexception == null) {     //if includeexceptiondetailinfaults = true, fault exception details created wcf.     return; } messagefault messagefault = faultexception.createmessagefault(); fault = message.createmessage(version, messagefault, faultexception.action); 

client:

try {     _client.samplemethod(); } catch (faultexception<myfaultmessage> e) {     //handle             } catch (faultexception<exceptiondetail> exception) {     //getting original exception detail if includeexceptiondetailinfaults = true     exceptiondetail exceptiondetail = exception.detail; } 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -