WebService-soapextensionattribute and soapextension

Source: Internet
Author: User

Soapextension and soapextensionattribute are used to control the general process of WebService serialization and deserialization, and to control the functions of WebService such as compression and log, I have not done the function of compressing the WSDL transmitted by the entire WebService, but I should try it first next week.

Soapextensionattribute class:

Public class extensionattribute: soapextensionattribute
{
Int _ priority = 1;

Public override int priority
{
Get
{
Return _ priority;
}
Set
{
_ Priority = value;
}
}

Public override type extensiontype
{
Get {return typeof (myextension);} // defines the type of the extended soap
}
}

 

The soapextensionattribute abstract class is defined as follows:

 

Protected soapextensionattribute ();
// Return result:
// System. Type of the soap extension.
Public abstract type extensiontype {Get ;}
// Return result:
// Priority of the soap extension.

Soap extension assigns priority, which helps determine the relative sequence of execution when multiple soap extensions are configured to use XML Web Service methods. The higher the priority of a soap extension, the closer its execution is to sending or receiving soap messages over the network. The soap extension belongs to any of the three priority groups. In each group, the priority attribute distinguishes each member. The lower the priority attribute, the higher the relative priority (0 is the highest ).
Public abstract int priority {Get; set ;}

 

// The following is the extended soapextension class

Public class myextension: soapextension
{

Public override object getinitializer (type servicetype)
{
Return GetType ();
}

Public override object getinitializer (logicalmethodinfo methodinfo, soapextensionattribute attribute)
{
Return NULL;
}

Public override void initialize (Object initializer)
{

}

// This override method will be called four times
// Beforeserialize, afterserialize, beforedeserialize, and afterdeserialize of soapmessagestage
Public override void processmessage (soapmessage message)
{
If (message. Stage = soapmessagestage. afterdeserialize) // post-serialization
{
Bool check = false;
Foreach (soapheader header in message. headers)
{
If (header is certficatesoapheader)
{
Certficatesoapheader myheader = (certficatesoapheader) header;

If (myheader. Username = NULL | myheader. Password = NULL)
{
Break;
}

If (myheader. username. Equals ("ly") & myheader. Password. Equals ("ly "))
{
Check = true;
Break;
}
}
}

If (! Check)
{
Throw new soapheaderexception ("authentication failed", soapexception. clientfaultcode );
}
}
}
}

 

The stream in soapmessage can be used to obtain the stream data sent and received.

The above function implements soapheader permission Authentication

After the above class is implemented, we do not need to judge in webmethod. We only need to add relevant attributes.

For example:

 

Public certficatesoapheader soapheader;

[Extensionattribute]
[Soapheader ("soapheader", direction = soapheaderdirection. In)]
[Webmethod]
Public String helloworld ()
{
Return "Hello World ";
}

The client call is the same as the previous one. It can be seen that processmessage in soapextension in WebService is equivalent to an interceptor, which can intercept the stream that enters webmethod.

The test code is as follows:

: Download/files/fujinliang/webservice.rar

 

 

 

 

Related Article

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.