13.07.2015 Views

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

The VaryByHeaders property enables you to cache multiple versions of a page,depend<strong>in</strong>g on the value of the HTTP header (or headers) you specify—<strong>in</strong> this example,the header value is SOAPAction. The VaryByParams property, on the other hand, letsyou ma<strong>in</strong>ta<strong>in</strong> different caches <strong>for</strong> each set of dist<strong>in</strong>ct values of the specified parameters.In this case, us<strong>in</strong>g the asterisk (*) <strong>in</strong>dicates that all parameters must be consideredwhen cach<strong>in</strong>g a page.NoteUnder certa<strong>in</strong> conditions, the CacheDurationattribute can constitutea significant improvement <strong>for</strong> your Web services. Ideally, you mightwant to set this attribute when your method returns a large amountof data (<strong>for</strong> example, a DataSet object) but receives quite a fewrequests distributed throughout the day. The cach<strong>in</strong>g mechanism—the same mechanism available to all ASP.<strong>NET</strong> applications—letsyou dist<strong>in</strong>guish cached copies of the output that are also based onparameters. Under these circumstances, generat<strong>in</strong>g a new data setevery time the method is called isn't efficient—unless, of course,user requirements mandate that you return fresh data. Theadvantage <strong>in</strong> per<strong>for</strong>mance can be relevant and significant. In myexperimentation, I was able to get response times up to 8 timesfaster, with 2 or 3 times faster be<strong>in</strong>g the average.The Role of the <strong>XML</strong> SerializerAs shown <strong>in</strong> Figure 13-5, the return value of the method call is packed as <strong>XML</strong> us<strong>in</strong>gthe <strong>XML</strong> serializer that we saw <strong>in</strong> action <strong>in</strong> Chapter 11. The follow<strong>in</strong>g script representsthe pseudocode that creates the response <strong>for</strong> a Web service method:Response.ContentType = ContentType.Compose("text/xml",Encod<strong>in</strong>g.UTF8Encod<strong>in</strong>g);ser.Serialize(outputStream, returnValue);The <strong>XML</strong> serializer can't process all .<strong>NET</strong> Framework types. Remember, the <strong>XML</strong>serializer doesn't work with types that have circular references and only packs publicand read/write members. The <strong>XML</strong> serializer doesn't ensure type fidelity but simply aneffective XSD (or SOAP-encoded) representation of the data.NoteA Web service can't return an ADO.<strong>NET</strong> object other than theDataSet object <strong>for</strong> the simple reason that the XmlSerializer classdoesn't know how to handle them. On the other hand, XmlSerializercan normally handle arrays of primitive objects, and this can helpwhen you're creat<strong>in</strong>g workarounds <strong>for</strong> return<strong>in</strong>g complex data likethat stored <strong>in</strong> many ADO.<strong>NET</strong> objects.Disabl<strong>in</strong>g HTTP-POST and HTTP-GETAs we'll see <strong>in</strong> more detail <strong>in</strong> the section "Invok<strong>in</strong>g a Web Service Through Script," onpage 586, you can <strong>in</strong>voke a Web service method us<strong>in</strong>g a SOAP message as well as apla<strong>in</strong> HTTP-POST or HTTP-GET command. The latter two protocols have been<strong>in</strong>troduced to make access<strong>in</strong>g a Web service easier than ever. However, leav<strong>in</strong>g theWeb service door open to HTTP packets can constitute a potential security hole.If you want to disable the HTTP-POST and HTTP-GET support on a mach<strong>in</strong>e-widebasis, do as follows. First locate the mach<strong>in</strong>e.config file (more on configuration files <strong>in</strong>Chapter 15) <strong>in</strong> the local system. The file is normally located <strong>in</strong> the config subdirectory ofthe .<strong>NET</strong> Framework <strong>in</strong>stallation path. A typical path is shown here:c:\w<strong>in</strong>nt\microsoft.net\framework\v1.0.3705\config\mach<strong>in</strong>e.config469

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!