在某些Web Service的应用场景下,例如公文的传送,在Web Service返回结果的同时将word文档及其它附件返回,这时候可以使用DIME协议来进行文件的传输。使用它来传输不需要经过SOAP消息的序列化/反序列化,有很高的效率。当然这里要用到Web Services Enhancements (WSE) ,目前的最新版本为3.0。本文中所使用的版本为2.0sp2,有趣的是WSE的各个版本中的命令空间都有很大的变化。这一点的确有点让人苦恼!在安装WSE时推荐将Visual Studio Tools也安装上,这样会免去手工修改Web Service的Web.config文件的工作。
本文的示例下载:http://www.cnblogs.com/Files/lcybest/DIMESample.rar
Web Service:
首先要引用Microsoft.Web.Services2.dll,修改Web.config文件,将下面这段配置添加进去:
<webServices>
<soapExtensionTypes>
<add type="Microsoft.Web.Services2.WebServicesExtension, Microsoft.Web.Services2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" priority="1" group="0" />
</soapExtensionTypes>
</webServices>
</system.web>
如果你安装了WSE的Visual Studio工具,以上工作可以通过工具来实现。
下面的代码演示了在ResponseSoapContext中加入DIME附件的实现:
[WebMethod]
public string GetDocument(string DocumentId)
{
if(DocumentId.Length==0)
return "DocumentId can not be empty!";
Attachment attach=new Attachment(Guid.NewGuid().ToString(),@"D:\test.doc");
Microsoft.Web.Services2.ResponseSoapContext.Current.Attachments.Add(attach);
return "SendOK";
}
我们使用一个windows应用程序来演示一下可以接收Web Service附件的客户端
首先要将Microsoft.Web.Services2.dll引用到项目中,添加对Web Service的引用。此时如果安装了WSE工具会自己动生成一个以“WSE”为结尾的代理类。在代码中可以直接使用这个代理类。
如果没有安装工具则需要手工修改Visual Studio生成的代理类,代理类默认是从System.Web.Services.Protocols.WebClientProtocol继承的,在这里要修改为从Microsoft.Web.Services2.WebServicesClientProtocol来继承。
在我们客户端中可以通过以下代码来实现将Response中的文件取出来保存到文件系统中:
程序代码
private void button1_Click(object sender, System.EventArgs e)
{
TalkServer.DataInterface client=new DIMEClient.TalkServer.DataInterface();
string strvalue=client.GetDocument("test111");
if(client.ResponseSoapContext.Attachments.Count==0)
{
MessageBox.Show("No Attachments in the webservice response!");
return;
}
Microsoft.Web.Services2.Attachments.Attachment attach;
attach=client.ResponseSoapContext.Attachments[0];
byte[] buffer=new byte[attach.Stream.Length];
client.ResponseSoapContext.Attachments[0].Stream.Read(buffer,0,buffer.Length);
System.IO.FileStream stream=new System.IO.FileStream(@"C:\test.doc",System.IO.FileMode.Create);
stream.Write(buffer,0,buffer.Length);
stream.Flush();
stream.Close();
if(strvalue=="SendOK")
MessageBox.Show("Receive succeed");
else
MessageBox.Show("Receive fail");
}
本文的示例下载:http://www.cnblogs.com/Files/lcybest/DIMESample.rar
Web Service:
首先要引用Microsoft.Web.Services2.dll,修改Web.config文件,将下面这段配置添加进去:
<webServices>
<soapExtensionTypes>
<add type="Microsoft.Web.Services2.WebServicesExtension, Microsoft.Web.Services2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" priority="1" group="0" />
</soapExtensionTypes>
</webServices>
</system.web>
如果你安装了WSE的Visual Studio工具,以上工作可以通过工具来实现。
下面的代码演示了在ResponseSoapContext中加入DIME附件的实现:
[WebMethod]
public string GetDocument(string DocumentId)
{
if(DocumentId.Length==0)
return "DocumentId can not be empty!";
Attachment attach=new Attachment(Guid.NewGuid().ToString(),@"D:\test.doc");
Microsoft.Web.Services2.ResponseSoapContext.Current.Attachments.Add(attach);
return "SendOK";
}
我们使用一个windows应用程序来演示一下可以接收Web Service附件的客户端
首先要将Microsoft.Web.Services2.dll引用到项目中,添加对Web Service的引用。此时如果安装了WSE工具会自己动生成一个以“WSE”为结尾的代理类。在代码中可以直接使用这个代理类。
如果没有安装工具则需要手工修改Visual Studio生成的代理类,代理类默认是从System.Web.Services.Protocols.WebClientProtocol继承的,在这里要修改为从Microsoft.Web.Services2.WebServicesClientProtocol来继承。
在我们客户端中可以通过以下代码来实现将Response中的文件取出来保存到文件系统中:
程序代码
private void button1_Click(object sender, System.EventArgs e)
{
TalkServer.DataInterface client=new DIMEClient.TalkServer.DataInterface();
string strvalue=client.GetDocument("test111");
if(client.ResponseSoapContext.Attachments.Count==0)
{
MessageBox.Show("No Attachments in the webservice response!");
return;
}
Microsoft.Web.Services2.Attachments.Attachment attach;
attach=client.ResponseSoapContext.Attachments[0];
byte[] buffer=new byte[attach.Stream.Length];
client.ResponseSoapContext.Attachments[0].Stream.Read(buffer,0,buffer.Length);
System.IO.FileStream stream=new System.IO.FileStream(@"C:\test.doc",System.IO.FileMode.Create);
stream.Write(buffer,0,buffer.Length);
stream.Flush();
stream.Close();
if(strvalue=="SendOK")
MessageBox.Show("Receive succeed");
else
MessageBox.Show("Receive fail");
}
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com
暂无“asp.net下使用DIME协议上传文件”评论...
更新日志
2025年05月07日
2025年05月07日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]