【发布时间】:2012-01-02 20:03:34
【问题描述】:
我想要完成的是向我的基于 WCF REST 的服务添加一个 GET 方法,并通过 Silverlight 3 客户端应用程序中的 WebRequest 类访问它。
我收到错误远程服务器返回错误:NotFound。据我了解,这可能只是服务器上遇到的任何 500 错误的一般错误。
WCF 运营合约:
[OperationContract, WebGet(UriTemplate = "path/{id}")]
Stream Get(string id);
操作实现:
public Stream Get(string id)
{
WebOperationContext.Current.OutgoingResponse.ContentType = "application/xml; charset=utf-8";
return new MemoryStream(Encoding.UTF8.GetBytes("<xml><id>1</id><name>Some Name</name></xml>));
}
引发异常的客户端代码:
HttpWebRequest webRequest = WebRequest.CreateHttp("http://domain.com/my-service.svc/path/1");
webRequest.BeginGetResponse(
x =>
{
try
{
using (WebResponse webResponse = webRequest.EndGetResponse(x)) <--Exception thrown here
using (Stream stream = webResponse.GetResponseStream())
{
//do stuff here...eventually.
}
}
catch (Exception ex)
{
}
},
null);
我怀疑它与返回类型有关,并且也尝试返回 XmlElement 无济于事。我真的被难住了,有什么想法我可能做错了吗?
请注意,我可以通过 Fiddler 和网络浏览器成功点击该方法。
【问题讨论】:
-
@rekire 我正在使用
WebHttpBinding。此外,我确实有许多其他方法可以处理此服务定义,但是当创建返回类型为Stream或XmlElement的方法时,它会失败。 -
好的,感谢您关注@rekire!
标签: c# wcf httpwebrequest silverlight-3.0