在 WCF 中通过 HTTP Post 接受表单字段
我需要将表单数据接受到基于 WCF 的服务。 这是接口:
[OperationContract]
[WebInvoke(UriTemplate = "lead/inff",
BodyStyle = WebMessageBodyStyle.WrappedRequest)]
int Inff(Stream input);
这是实现(示例 - 无错误处理和其他保护措施):
public int Inff(Stream input)
{
StreamReader sr = new StreamReader(input);
string s = sr.ReadToEnd();
sr.Dispose();
NameValueCollection qs = HttpUtility.ParseQueryString(s);
Debug.WriteLine(qs["field1"]);
Debug.WriteLine(qs["field2"]);
return 0;
}
假设使用 WCF,除了解析传入流之外,是否有更好的方法来实现此目的?
I need to accept form data to a WCF-based service. Here's the interface:
[OperationContract]
[WebInvoke(UriTemplate = "lead/inff",
BodyStyle = WebMessageBodyStyle.WrappedRequest)]
int Inff(Stream input);
Here's the implementation (sample - no error handling and other safeguards):
public int Inff(Stream input)
{
StreamReader sr = new StreamReader(input);
string s = sr.ReadToEnd();
sr.Dispose();
NameValueCollection qs = HttpUtility.ParseQueryString(s);
Debug.WriteLine(qs["field1"]);
Debug.WriteLine(qs["field2"]);
return 0;
}
Assuming WCF, is there a better way to accomplish this besides parsing the incoming stream?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我记得在 DevLink 上和你谈过这个问题。
由于您必须支持表单字段,因此获取这些字段(您当前正在做的事情)的机制不会改变。
可能有用的东西,特别是如果您想为不需要表单字段的新应用程序重用您的服务,那就是创建一个通道来解构您的流并将其重新打包为 XML/JSON/SOAP/Whatever 并让您的表单客户端通过该通道与服务进行通信,而不使用表单的客户端可以使用另一个通道堆栈。 只是一个想法...
希望有帮助。 如果您需要该频道的帮助,请随时告诉我。
I remember speaking to you about this at DevLink.
Since you have to support form fields the mechanics of getting those (what you are currently doing) don't change.
Something that might be helpful, especially if you want to reuse your service for new applications that don't require the form fields is to create a channel that deconstructs your stream and repackages it to XML/JSON/SOAP/Whatever and have your form clients communicate with the service through that while clients that don't use forms can use another channel stack. Just an idea...
Hope that helps. If you need help with the channel feel free to let me know.
您可以使用 jquery 序列化表单字段并将其打包为 wcf 服务的 json 请求。
You can serialize your form fields with jquery and package it as json request to wcf service.