类型'System.Web.HttpRequest'无法序列化 [英] Type 'System.Web.HttpRequest' cannot be serialized

查看:76
本文介绍了类型'System.Web.HttpRequest'无法序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)使上载程序成为Web服务
2)随着文件上传,将进度更新返回给用户.
3)在数据库中记录其他相关的用户选择的选项.

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.

因此,我首先创建了一个WCF Web客户端,其中包含低于服务合同:

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:

类型'System.Web.HttpRequest'不能被序列化.考虑标记它使用DataContractAttribute,以及标记您想要的所有成员与序列化DataMemberAttribute属性.

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.

所以,我回到了服务合同中,将[OperationContract]更改为[DataContract]但是更改产生了相同的错误.

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?

感谢您的时间.

推荐答案

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

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.

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

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.

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

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

您肯定需要重新设计解决方案-您需要检查WCF流以将文件上传到WCF(使用Google进行搜索-您会找到大量点击量),或者需要找到另一种方式来传递相关内容信息(例如文件名列表)到WCF服务,而无需使用HttpRequest对象.

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.

马克

这篇关于类型'System.Web.HttpRequest'无法序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆