[WCF Study Notes] 4. Message operations

Source: Internet
Author: User

In WCF, everything is centered around the message. What exactly does the message look like?

[Servicecontract]
Public interface icalculate
{
[Operationcontract]
Double add (double A, double B );
}

Public class calculateservice: icalculate
{
Public double add (double A, double B)
{
Message MSG = operationcontext. Current. requestcontext. requestmessage;
Console. writeline (MSG );

Return A + B;
}
}

Public class wcftest
{
Public static void test ()
{
Appdomain. createdomain ("server"). docallback (delegate
{
Servicehost host = new servicehost (typeof (calculateservice ));
Host. addserviceendpoint (typeof (icalculate), new basichttpbinding (),
"Http: // localhost: 8080/calc ");

Host. open ();
});

Channelfactory <icalculate> factory = new channelfactory <icalculate> (New basichttpbinding (),
"Http: // localhost: 8080/calc ");
Icalculate o = factory. createchannel ();
Console. writeline (O. Add (1, 2 ));
}
}

 

Output

<S: envelope xmlns: S = "http://schemas.xmlsoap.org/soap/envelope/">
<S: Header>
<To S: mustunderstand = "1" xmlns = "http: //..."> http: // localhost: 8080/calc </to>
<Action s: mustunderstand = "1" xmlns = "http: //..."> http://tempuri.org/ICalculate/Add </Action>
</S: Header>
<S: Body>
<Add xmlns = "http://tempuri.org/">
<A> 1 </a>
<B> 2 </B>
</Add>
</S: Body>
</S: envelope>

In fact, we can directly program based on the message layer and use operationcontract. Action to capture messages of specific actions.

[Servicecontract]
Public interface icalculate
{
[Operationcontract (Action = "add", replyaction = "add")]
Message processmessage (message m );
}

Public class calculateservice: icalculate
{
Public message processmessage (message m)
{
Data d = M. getbody <DATA> ();
Console. writeline (D. I );

Return message. createmessage (messageversion. soap11, "add", new data (9999 ));
}
}

[Datacontract]
Public class data
{
[Datamember]
Public int I;

Public Data (int I)
{
This. I = I;
}
}

Public class wcftest
{
Public static void test ()
{
Appdomain. createdomain ("server"). docallback (delegate
{
Servicehost host = new servicehost (typeof (calculateservice ));
Host. addserviceendpoint (typeof (icalculate), new basichttpbinding (),
"Http: // localhost: 8080/calc ");

Host. open ();
});

Channelfactory <irequestchannel> factory = new channelfactory <irequestchannel> (
New basichttpbinding (), "http: // localhost: 8080/calc ");
Irequestchannel channel = factory. createchannel ();
Channel. open ();

Message request = message. createmessage (messageversion. soap11, "add", new data (1234 ));
Message reply = channel. Request (request );
Console. writeline ("-------------------");
Console. writeline (reply );

Channel. Close ();
Factory. Close ();
}
}

Output:

1234
-------------------
<S: envelope xmlns: S = "http://schemas.xmlsoap.org/soap/envelope/">
<S: Header/>
<S: Body>
<Data xmlns = "http: //..." xmlns: I = "http: //...">
<I> 9999 </I>
</Data>
</S: Body>
</S: envelope>

Zheng
As shown above, all calls are converted into messages and sent. This is also in line with SOA specifications, completely isolated and clear boundaries. (Call
"M. getbody <DATA> ()", the message. State changes, and access again fails. For more information, see msdn
Documentation .)

We can also use messagecontractattribute/messageheaderattribute
To control the message format.
More flexible. We can set the message header and body, including whether to sign and encrypt some of them.

[Servicecontract]
Public interface icalculate
{
[Operationcontract]
Void add (Data D );
}

Public class calculateservice: icalculate
{
Public void add (Data D)
{
Console. writeline (operationcontext. Current. requestcontext. requestmessage );
Console. writeline ("----------------");
Console. writeline ("{0}/{1}", d. A, D. B );
}
}

[Messagecontract]
Public class data
{
[Messageheader]
Public double A = 1;

[Messagebodymember]
Public Double B = 2;
}

Public class wcftest
{
Public static void test ()
{
Appdomain. createdomain ("server"). docallback (delegate
{
Servicehost host = new servicehost (typeof (calculateservice ));
Host. addserviceendpoint (typeof (icalculate), new basichttpbinding (),
"Http: // localhost: 8080/calc ");

Host. open ();
});

Channelfactory <icalculate> factory = new channelfactory <icalculate> (New basichttpbinding (),
"Http: // localhost: 8080/calc ");
Icalculate o = factory. createchannel ();

Data d = new data ();
D. A = 1234;
D. B = 5678;
O. Add (d );
}
}

Output:

<S: envelope xmlns: S = "http://schemas.xmlsoap.org/soap/envelope/">
<S: Header>
<H: A xmlns: H = "http://tempuri.org/"> 1 <To S: mustunderstand = "1" xmlns = "http: // s..."> http: // localhost: 8080/calc </to>
<Action s: mustunderstand = "1" xmlns = "http: //..."> http://tempuri.org/ICalculate/Add </Action>
</S: Header>
<S: Body>
<Data xmlns = "http://tempuri.org/">
<B> 2 </B>
</Data>
</S: Body>
</S: envelope>
----------------
1234/5678

For more information about message, see the msdn (Microsoft Windows SDK) documentation.

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.