Click here to Skip to main content
15,036,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Experts,

I have one WCF Self Hosted JSon Service,
I want to return custom HTTP Status code from my service.
My code for set HTTP Status is as under.
C#
public void SetResponseHttpStatus(HttpStatusCode statusCode)
{
   try
   {
     OutgoingWebResponseContext response = WebOperationContext.Current.OutgoingResponse;
     response.StatusCode = statusCode;

     //WebOperationContext context = WebOperationContext.Current;
     //context.OutgoingResponse.StatusCode = statusCode;
   }
   catch (Exception)
   {}
}


But Above code is giving exception of "Object Reference .........";

May I Have any solution...?
Please help me as soon as possible
Posted

1 solution

C#
 public class MyService: IMyService
{

// GENERAL/GLOBAL DECLARATION
private OutgoingWebResponseContext response;

// THIS IS YOUR WEB SERVICE FUNCTION WHICH WILL EXECUTE WHILE CALLING SERVICE
public string MyServiceFunction(BLAA.., BLAA...)
{
    // ASSIGN OBJECT TO GLOBAL DECLARATION 
    response = WebOperationContext.Current.OutgoingResponse;
    SetResponseHttpStatus(HttpStatusCode.OK);
}

private void SetResponseHttpStatus(HttpStatusCode statusCode)
{
   try
   {
         // SET STATUS TO RESPONSE
         response.StatusCode = statusCode; 
   }
   catch (Exception)
   {}
}

}
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900