开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜