输入“System.Web.HttpRequest”无法序列化

发布于 2024-08-07 23:17:09 字数 771 浏览 6 评论 0原文

我正在尝试在网站中设计图片上传功能。 我正在使用 ASP.NET 3.5、C# 和 WCF。

我被要求完成以下任务:

1) 将上传器设为 Web 服务
2) 在文件上传时向用户返回进度更新。
3) 在数据库中记录其他相关的用户选择的选项。

因此,我首先使用以下命令创建一个 WCF Web 客户端 下面的服务合同:

IService.UploadPictures(HttpRequest request);

private UploadServiceClient upload;

protected void Page_Load(object sender, EventArgs e)
{

      upload = new UploadServiceClient();
      upload.UploadPictures(Request.Files);

}

当我编译时,出现以下错误:

类型“System.Web.HttpRequest”不能 被连载。考虑标记一下 使用 DataContractAttribute,并且 标记您想要的所有成员 连载于 DataMemberAttribute 属性。

所以,我重新签订了我的服务合同 将 [OperationContract] 更改为 [DataContract] 但更改产生了相同的错误。

有人可以告诉我我做错了什么吗 并举例说明如何最好地向前推进?

感谢您抽出时间。

I am trying to design an Picture Upload feature into a web site.
I am using ASP.NET 3.5, C#, and WCF.

I have been asked to accomplish the following:

1) Make the Uploader a Web Service
2) Return progress updates to the user as files are uploaded.
3) Log other relevant user-selected options in the database.

So, I have started off by creating a WCF web client with the
below service contract:

IService.UploadPictures(HttpRequest request);

private UploadServiceClient upload;

protected void Page_Load(object sender, EventArgs e)
{

      upload = new UploadServiceClient();
      upload.UploadPictures(Request.Files);

}

When I compile, I get the below error:

Type 'System.Web.HttpRequest' cannot
be serialized. Consider marking it
with the DataContractAttribute, and
marking all of its members you want
serialized with the
DataMemberAttribute attribute.

So, I went back into my service contract and
changed [OperationContract] to [DataContract]
but the change produced the same error.

Can somebody kindly tell me what I am doing wrong
and provide examples as to how to best move forward?

Thanks for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧话新听 2024-08-14 23:17:09

您不能使用 HttpRequest 之类的内容作为 WCF 参数。错误消息说明了一切 - HttpRequest 不可序列化,并且为了与 WCF 一起使用,类型必须可序列化。

另外,您需要记住:您不只是将对象实例传递给此处的方法 - 您真正要做的是让 WCF 运行时将您的请求(要调用的方法名称加上所有传入的参数)序列化为消息(例如:电子邮件或 xml 文件),将其发送到服务器,在那里反序列化并构建给定数据类型的新副本(如 DataContract 中所定义),然后对其执行某些操作。

您的 WCF 服务很可能是自托管的,例如在 NT 服务或控制台应用程序中运行 - 在这些情况下没有 HttpRequest 可用!

您确实需要重新架构您的解决方案 - 您需要检查 WCF 流以将文件上传到 WCF(谷歌搜索 - 您会发现很多点击),或者您需要找到另一种方法来传递相关信息(例如文件名列表)到 WCF 服务,而不使用 HttpRequest 对象。

马克

You cannot use something like a HttpRequest as a WCF parameter. The error messages says it all - the HttpRequest is not serializable, and in order to work with WCF, types have to be serializable.

Also, you need to remember: you're not just passing an object instance to a method here - what you're really doing is having the WCF runtime serialize your request (the method name to call plus all the parameters passed in) into a message (think: e-mail or xml file), sending it to the server, deserialising there and building up a new copy of the given datatype (as defined in your DataContract), and doing something with it.

Your WCF service could well be self-hosted, e.g. running in a NT Service or console app - no HttpRequest available in those circumstances!

You need to definitely rearchitect your solution - you need to either check into WCF streaming to upload files to WCF (google for it - you'll find plenty of hits) or you'll need to find another way to pass the relevant info (e.g. list of filenames) to the WCF service without use of a HttpRequest object.

Marc

忆悲凉 2024-08-14 23:17:09

您正在将请求作为请求的参数提交。这不是你想做的。我假设“Request.Files”是一个文件数组。这就是您要上传的内容。所以像这样:

IService.UploadPictures(List<SomeFileType> request);

You are submitting a request as a parameter to a request. This is not what you want to do. I'm assuming that "Request.Files" is an array of files. This is what you want to upload. So something like:

IService.UploadPictures(List<SomeFileType> request);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文