c# - How to display WCF HTTP codes in service response -


i'm working through web service using wcf , entity framework , know how view or return http status codes calling clients.

the code have follows:

iuserservice.cs

   [operationcontract]         [webinvoke(method = "get", responseformat = webmessageformat.json, bodystyle = webmessagebodystyle.wrappedrequest, uritemplate = "/getusers")]         list<user> getusers(); 

userservice.svc.cs

  public list<user> getusers()         {             var usercontroller = new usercontroller();             return usercontroller.getusers();         } 

usercontroller.cs

 public list<user> getusers()         {             list<user> serverresponse = new list<user>();              try             {                 using (var db = new myentities())                 {                   list<user> userlist = db.users.tolist();                      foreach (user userrecord in userlist)                     {                         user userdto = new user();                         userdto.userid = userrecord.user_id;                         userdto.name = userrecord.user_name;                         serverresponse.add(userdto);                     }                 }             }              catch (exception ex)             {              }              return serverresponse; 

user dto

  [datacontract]     public class user     {         [datamember(name = "name")]         public string name { get; set; }          [datamember(name = "userid")]         public int userid { get; set; }      } 

i've seen when using or working on other apis status code can returned in dictionary response, example "success" "200" in key value pair in client calling response. there way enable wcf in web.config or interface class? i'd client able receive error codes success or failure able react if goes wrong. when run request in browser following valid json:

 [     {         "name": "apiclienttestuser",         "userid": 212,     } ] 

either json or somewhere else client know call succeeded or failed appropriate http code. tips or advice on how appreciated.

probably simple approach wrap results generic response objects

[datacontract] public class response<t> {     [datamember]     public t result { get; set; }      [datamember]     public int status { get; set; } }  // declaration response<list<user>> serverresponse = response<list<user>>();  // on success serverresponse.result = userlist; serverresponse.status = 200; // ok  // on fail serverresponse.status = 500; // fail  // , contract [operationcontract] [webinvoke(method = "get", responseformat = webmessageformat.json, bodystyle = webmessagebodystyle.wrappedrequest, uritemplate = "/getusers")] response<list<user>> getusers(); 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -