创建 Restful 服务 - 无法检索使用 HttpClient 发布的数据
我正在 VS 2010 中创建 Restful 服务。我有一个类 Advisor。 SubmitAdvisor方法将此类作为参数,ListAdvisors返回顾问列表。
我编写了一个控制台客户端来获取和发布这些方法。我在我的客户端中包含了 Microsoft.Http。
即使使用 HttpClient 发布数据后,我也无法看到顾问列表。
内部服务接口 -
[WebInvoke(Method="POST",UriTemplate="/")]
[OperationContract]
void SubmitAdvisor(Advisor advisor);
[WebGet(UriTemplate = "/")]
[OperationContract]
List<Advisor> ListAdvisors();
内部服务实现 -
List<Advisor> advisors = new List<Advisor>();
public void SubmitAdvisor(Advisor advisor)
{
advisors.Add(advisor);
}
public List<Advisor> ListAdvisors()
{
return advisors;
}
在我的客户端应用程序中 -
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 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 data 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>>();
};
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能够通过将服务设置为单例或将 InstanceContextMode 设置为 Single 来检索列表。
I was able to retrieve the list by making the Service as singleton, or setting the InstanceContextMode to Single.