本文的示例下载:http://www.cnblogs.com/Files/lcybest/DIMESample.rar
Web Service:
首先要引用Microsoft.Web.Services2.dll,修改Web.config文件,将下面这段配置添加进去:
如果你安装了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");
}



