Creating Restful Service - Not able to retrieve the data posted using HttpClient
I am creating a Restful service in VS 2010. I have a class Advisor. The SubmitAdvisor method takes this class as a parameter, and the ListAdvisors returns the List of Advisor.
I wrote a console client to Get and Post these methods. I included Microsoft.Http in my client.
I am not able to see the list of Advisors even after Posting the d开发者_JAVA百科ata using HttpClient.
Inside Service Interface -
[WebInvoke(Method="POST",UriTemplate="/")]
[OperationContract]
void SubmitAdvisor(Advisor advisor);
[WebGet(UriTemplate = "/")]
[OperationContract]
List<Advisor> ListAdvisors();
Inside Service Implementation -
List<Advisor> advisors = new List<Advisor>();
public void SubmitAdvisor(Advisor advisor)
{
advisors.Add(advisor);
}
public List<Advisor> ListAdvisors()
{
return advisors;
}
In my client app -
public static void InsertData(Advisor obj)
{
using (HttpResponseMessage response = new HttpClient().Post(uri,HttpContentExtensions.CreateDataContract(obj)))
{
};
}
public static List<Advisor> GetAllAdvisors()
{
using (HttpResponseMessage response = new HttpClient().Get(uri))
{
return response.Content.ReadAsDataContract<List<Advisor>>();
};
}
I was able to retrieve the list by making the Service as singleton, or setting the InstanceContextMode to Single.
精彩评论