asp.net use the DIME protocol to upload files _ practical Tips

Source: Internet
Author: User
In some Web service scenarios, such as the transfer of documents, when the Web service returns results and returns Word documents and other attachments, you can use the DIME protocol to transfer files. It is highly efficient to use it to transmit serialization/deserialization that does not require SOAP messages. Of course, Web Services Enhancements (WSE) is used here, and the latest version is 3.0. The version used in this article is 2.0SP2, and it is interesting that the command space in each version of WSE varies greatly. This is a bit annoying indeed! It is recommended that visual Studio tools be installed when WSE is installed, which eliminates the task of manually modifying the Web.config files for the Web service.
Sample downloads for this article: Http://www.cnblogs.com/Files/lcybest/DIMESample.rar
Web Service:
To start by referencing Microsoft.Web.Services2.dll, modify the Web.config file and add the following configuration:
<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>
If you have the Visual Studio Tools for WSE installed, the above work can be accomplished through a tool.
The following code shows the implementation of adding a dime attachment to ResponseSoapContext:
[WebMethod]
public string GetDocument (string DocumentID)
{
if (documentid.length==0)
Return "DocumentID can empty!";
Attachment attach=new Attachment (Guid.NewGuid (). ToString (), @ "D:\test.doc");
MICROSOFT.WEB.SERVICES2.RESPONSESOAPCONTEXT.CURRENT.ATTACHMENTS.ADD (attach);
return "Sendok";
}
We use a Windows application to demonstrate a client that can receive Web service attachments
Start by referencing the Microsoft.Web.Services2.dll to your project and adding a reference to the Web service. At this point, if the WSE tool is installed, it will generate a proxy class that ends with "WSE". You can use this proxy class directly in your code.
If you do not have the installation tools, you will need to manually modify the proxy classes generated by Visual Studio. The proxy class is inherited from System.Web.Services.Protocols.WebClientProtocol by default, and is modified here to inherit from Microsoft.Web.Services2.WebServicesClientProtocol.
The following code can be implemented in our client to save files in response to the file system:
Program code
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");
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.