Compressing text is a procedure that can reduce the size of content representation up to 80%.
This means that compressed text can be stored using 80% less space than uncompressed text.
It also means that the content needs less time to be transferred over a network.
SOAP extensions is a Microsoft ASP.NET WebMethod interception mechanism that can be used to manipulate SOAP requests/responses before they are sent on the wire.
In most cases SOAP requests are much smaller in size than SOAP responses (for example, a large dataset), so we will only compress SOAP responses in this example.
Reference the Figure File
Improve XML Web Services' Performance by Compressing SOAP
This example is that:
The SOAP Message is compressed on AfterSerialize stage (server side) and decompressed on BeforeDeserialize stage (client side)
In order to compress our Web service's SOAP responses, we need to do two things:
· Compress the SOAP response message after serialization on the server.
· Decompress the SOAP response message before deserialization on the client.
The steps of server side are as following:
· First, here is the XML Web service that returns a large data.
· Second,On the server side, the SOAP response from the server is compressed in order to reduce its size.
· We add the ServerSoapExtension.dll assembly as reference and declare the SOAP extension on web.config of the XML Web service:(not necessary)
Then,We turned to the Consumer-Side SOAP Extension
· here need a soap extension too
· add the ClientSoapExtension.dll assembly as reference and declare the SOAP extension on the XML Web service reference of our application
Notice:
(1) In the Client,We banding the SoapExtension Class to the Method in the corresponding reference.cs,which we can find in the Web reference Item in the Client.
And we need to add the code as following:
/// <remarks/>
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/GetData", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped),SoapExtensionClient.DeCompressSoap()]
public string GetData() {
object[] results = this.Invoke("GetData", new object[0]);
return ((string)(results[0]));
}
When the WebMethod is updated , we need to add this code manually again.
(2) testing xml:
string test2 = "<?xml version=/"1.0/" encoding=/"utf-8/"?><soap:Envelope xmlns:soap=/"http://schemas.xmlsoap.org/soap/envelope//" xmlns:xsi=/"http://www.w3.org/2001/XMLSchema-instance/" xmlns:xsd=/"http://www.w3.org/2001/XMLSchema/"><soap:Body><GetDataResponse xmlns=/"http://tempuri.org//"><GetDataResult>Large Data Source KKK</GetDataResult></GetDataResponse></soap:Body></soap:Envelope>";
/************************************************************************************
* Qeustion:
(1)why this code can not be complied in the Web.config file;
<!--
<webServices>
<soapExtensionTypes>
<add type="TestSoapExtensionWebService.CompressSoapMessage" priority="1"
group="High"/>
</soapExtensionTypes>
</webServices>
-->
如果是这样的话,我们需要把soap扩展单独配置成程序集,这样才能找到,但是不能在web.config里面给每个方法指定不同的soap扩展处理。
如果要给每个方法指定不同的扩展处理,需要在WebMethod方法上一个一个的添加。
同理,在客户端我们可以在App.config中指定统一的soap扩展处理。但是如果要给每个方法指定不同的扩展处理,需要在具体的WebMethod方法的reference上一个一个的添加。
最好我们是将soap扩展统一配置成一个程序集,便于发布和管理