Legendary WCF (4): send and receive SOAP Headers

Source: Internet
Author: User
Tags baseuri
The legendary WCF (1): Is this hard to learn?The legendary WCF (2): What about service agreements?Legendary WCF (3): multiple protocolsLegendary WCF (4): send and receive SOAP HeadersLegendary WCF (5): data protocols ()Legendary WCF (6): data protocols (B)Legendary WCF (7): "one-way" & "two-way"Legendary WCF (8): Play with message protocolsLegendary WCF (9): stream and file transferLegendary WCF (10): message interception and tamperingThe legendary WCF (11): Session)The legendary WCF (12): What is the use of server callback?Legendary WCF (13): group chat programThe legendary WCF (14): WCF can also be used as a chat program.

 

 

 

If you really don't understand what the header is, you should think about whether there is an attachment when you send an email? Yes. Can the SOAP header be considered as an additional information? Is the content appended to the message body.

What is the message body? In addition to the stream mode for data transmission, the rest is basically the message mode. We may wish to understand that the server and client of WCF interact through messages, just like sending text messages between us. You can reply to me, this is called "duplex". It's not easy to read. It's a two-way solution. If you are in a bad mood, you may not send me a text message. This is called "single job". It's not nice. It's a one-way solution.

For "messages", even more Nb means that the client sends a message to the server every time it calls the server method. Well, this is more intuitive to understand, right? No matter whether it is professional or not, it is king to understand it.

 

Since the message header is an additional information, what is the use? Don't mention it. Sometimes it's actually quite helpful. For example, is it difficult to perform identity authentication for WCF? What certificate should be issued (of course not an honor certificate). If you only want to verify the identity of a client, such as the user name or something, add some message headers dynamically when calling the service method, obtain and verify the message header on the server. In this way, does it also implement identity authentication?

In this way, it is not as secure as installing certificates and encrypting messages. However, it is sufficient for general use cases.

 

So, how to pass the message header? Of course, the client sends messages, and the server receives many messages. There is nothing magical about the operation methods and techniques, so if you have a good memory, you may wish to back these codes. A joke.

 

First, implement the server. In the operationcontract method, use operationcontext. Current. incomingmessageheaders to get the message header received from the client. Remember to introduce the system. servicemodel namespace.

Look at what I wrote.

[Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. servicemodel;
Using system. servicemodel. description;
Using system. servicemodel. channels;

Namespace Server
{
[Servicecontract]
Public interface iservice
{
[Operationcontract]
Void testmethod ();
}

Public class myservice: iservice
{
Public void testmethod ()
{
Int Index = operationcontext. Current. incomingmessageheaders. findheader ("Header", "http: // my ");
If (index! =-1)
{
String Hd = operationcontext. Current. incomingmessageheaders. getheader <string> (INDEX );
Console. writeline ("Received Header: {0}", HD );
}
}
}

Class Program
{
Static void main (string [] ARGs)
{
Uri baseuri = new uri ("http: // localhost: 7999/service ");
Using (servicehost host = new servicehost (typeof (myservice), baseuri ))
{
Basichttpbinding binding = new basichttpbinding ();
Binding. Security. mode = basichttpsecuritymode. None;
Host. addserviceendpoint (typeof (iservice), binding, "http: // localhost: 9000/testsv ");
Servicemetadatabehavior behavior = new servicemetadatabehavior ()
{
Httpgetenabled = true
};
Host. description. behaviors. Add (behavior );

Host. Opened + = new eventhandler (delegate (Object sender, eventargs E)
{
Console. writeline ("the service has been started. ");
});

// Start the service
Try
{
Host. open ();
}
Catch (exception ex)
{
Console. writeline (ex. Message );
}

Console. readkey ();
Host. Close ();
}
}
}
}

The operationcontext. Current. incomingmessageheaders. findheader method can be used to find the message header,

After the method is called, an index is returned, starting from 0, you know, like an array.

After obtaining the index, return the value of the corresponding message header Through operationcontext. Current. incomingmessageheaders. getheader <string>. Note that the header name and namespace must be consistent with the message inserted in the client. Otherwise, you cannot find the header.

As for the T, it's easy to understand. When you insert a message header in the client, what type of value is used, and the T is best matched. You want it to return a string, t is string. If you want it to return an integer, t is int.

 

OK, add it on the client and reference the service. Check the code and insert the message header.

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. servicemodel;
Using system. servicemodel. channels;

Namespace Client
{
Class Program
{
Static void main (string [] ARGs)
{
WS. serviceclient myclient = new ws. serviceclient ();
Using (operationcontextscope scope = new operationcontextscope (myclient. innerchannel ))
{
Messageheader myheader = messageheader. createheader (
"Header", "http: // my", "Hello, this is the message header. ");
Operationcontext. Current. outgoingmessageheaders. Add (myheader );

// Call Method
Myclient. testmethod ();
Console. writeline ("the service method has been called. ");
}
Console. readkey ();
}
}
}

Okay, it's almost the same. Now, you can test it. First run the server and then the client.

 

 

 

 

Turn to it

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.