c# - How to catch a custom FaultException in WCF -


i testing wcf potentially implementing api remote controlling device runs our controller-software (c#/.net 4.6.1) on windows.

i trying figure out how throw , catch faultexception service , catch .net client.

the problem having when running code (in debug-mode on vs 2015), exception not caught client, vs ends showing me exception inside vs @ code-location of service (service.cs), being thrown. exception message is:

an exception of type 'system.servicemodel.faultexception`1' occurred in wcfservice.dll not handled in user code

additional information: argument value not 1

where the argument value not 1 custom message provide me. here relevant parts of code. hope can spot, doing wrong:

iservice.cs:

[servicecontract(callbackcontract = typeof(imyevents))] public interface iservice {     [operationcontract]     [faultcontract(typeof(invalidvaluefault))]     string throwsfaultifargumentvalueisnotone(int value);      ... }  [datacontract] public class invalidvaluefault {     private string _message;      public invalidvaluefault(string message)     {         _message = message;     }      [datamember]     public string message { { return _message; } set { _message = value; } } }    

service.cs: [servicebehavior(concurrencymode = concurrencymode.reentrant, instancecontextmode = instancecontextmode.single)] public class service : iservice { private string defaultstring;

    public service(string ctortestvalue)     {         this.defaultstring = ctortestvalue;     }      public string throwsfaultifargumentvalueisnotone(int value)     {         if (value == 1)             return string.format("passed value correct: {0}", value);          // box exception shown inside visual studio         throw new faultexception<invalidvaluefault>(new invalidvaluefault("the argument value not 1"), new faultreason("the argument value not 1"));     }      ... } 

server.cs: public class server { private servicehost svh; private service service;

    public server()     {         service = new service("a fixed ctor test value service should return.");         svh = new servicehost(service);     }      public void open(string ipadress, string port)     {         svh.addserviceendpoint(         typeof(iservice),         new nettcpbinding(),         "net.tcp://"+ ipadress + ":" + port);         svh.open();     }      public void close()     {         svh.close();     } } 

client.cs:

public class client : imyevents {     channelfactory<iservice> scf;     iservice s;      public void openconnection(string ipaddress, string port)     {         var binding = new nettcpbinding();          scf = new duplexchannelfactory<iservice>(         new instancecontext(this),         binding,         "net.tcp://" + ipaddress + ":" + port);         s = scf.createchannel();     }      public void closeconnection()     {         scf.close();     }      public string throwsfaultifargumentvalueisnotone(int value)     {         try         {             return s.throwsfaultifargumentvalueisnotone(value);         }          catch (system.servicemodel.faultexception<invalidvaluefault> fault)         {             console.writeline("exception thrown throwsfaultifargumentvalueisnotone(2):");             console.writeline("exception message: " + fault.message);             //throw;             return "exception happend.";         }     }      ... } 

program.cs (test program using server , client):

class program {     static void main(string[] args)     {         // start server         var server = new server();         server.open("localhost", "6700");         console.writeline("server started.");          var client = new client();         client.openconnection("localhost", "6700");          console.readline();         console.writeline("result client.throwsfaultifargumentvalueisnotone(1): {0}", client.throwsfaultifargumentvalueisnotone(1));          console.readline();         console.writeline("result client.throwsfaultifargumentvalueisnotone(2): {0}", client.throwsfaultifargumentvalueisnotone(2));          console.readline();         client.closeconnection();         thread.sleep(1000);         server.close();     } } 

if generated soap code wsdl using vs tools faultexception being thrown here generic faultexception - meaning faultexception<fault_contract>. generic type exception checking service reference code , inspecting [system.servicemodel.faultcontractattribute()] attribute. attribute has type parameter generic type.

so if looks

[system.servicemodel.faultcontractattribute(typeof(myfaultcontract)] [system.servicemodel.operationcontractattribute(action = "soapaction", replyaction = "*")] soapresponse soapaction(soaprequest request); 

then catch clause should this

try {     soapclient.soapaction(new soaprequest()); } catch (faultexception<myfaultcontract> e) {  } 

Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

mapreduce - Resource manager does not transit to active state from standby -

serialization - Convert Any type in scala to Array[Byte] and back -