Original: 15 days proficient wcf--fourth day you must understand. Communication Unit Message
In the blink of an eye you have learned three days of WCF, is not very curious what WCF in the transport layer on the end of what is a bird hair thing??? Someone should know it's soap.
Like what? This one we come to uncover the answer ...
One: What does soap really look like?
In order to be able to see what the soap long, I can use the powerful fiddler to monitor, suddenly very excited Ah!!!
1.Server
1 Static voidMain (string[] args)2 {3ServiceHost host =NewServiceHost (typeof(Homeservice),NewUri ("http://192.168.1.105:19200"));4 5Host. AddServiceEndpoint (typeof(Ihomeservice),NewBasicHttpBinding (),"Homeservie");6 7 //Publish Metadata8Host. DESCRIPTION.BEHAVIORS.ADD (NewServiceMetadataBehavior () {httpgetenabled =true });9 TenHost. AddServiceEndpoint (typeof(IMetadataExchange), metadataexchangebindings.createmexhttpbinding (),"Mex"); One A host. Open (); - -Console.WriteLine ("Service is open ... "); the - Console.read (); -}
2.Client
1 New Channelfactory<ihomeservice> (Thenewnew endpointaddress ("http/ 192.168.1.105:19200/homeservie")); 2 3 var client = Factory. CreateChannel (); 4 5 Client. Update (" king eight eggs ");
Now I think you probably see this thing is a look, a message format built on the XML, the root element is envelope, I know that the hair translation is "envelope", so there is a "head"
and "Seal", is S:header and s:body, from this soap you can see it ignores the Header, and then we continue to look down, remember the meaning of update??? If you read the previous article,
You should know that this is an action, the so-called input message. The corresponding is updateresponse this output message, right, remember xmlns= "http://tempuri.org/" >?
It's the default namespace for Ihomeservice, right ...
The next thing we're interested in is update the <str> in this action, as you can see, this is the str parameter in the Update method, and finally let's take a look at the Updateresponse
<updateresult xmlns:a= "Http://schemas.datacontract.org/2004/07/MyService, don't know if you remember it is the XSD node in WSDL about student
Structure, see:
Well, the SOAP structure in WCF we probably know a little bit, do not know whether or not to trigger you to the soap more in-depth thinking???
Second: More in-depth thinking on soap
By fiddler observation, you should also understand that, whether it is the client or the server, WCF's high-level package simply takes out the body node in the envelope, while the other nodes seem to us
There is no egg to use, such as I said the header node, so that the header is a bit wasteful??? So here's a question, what does WCF use to construct messages at the bottom??? Below
We probably find the source of the client side ...
From the diagram above, you should now also know that in. NET is actually a message construct, so what I want to tell you is: Since WCF is also constructed with a message at the bottom, why don't I
To construct a message??? Wouldn't it be beautiful??? So I can manipulate the message freely, right? Otherwise WCF this high-level package of hair, for me is a kind of bondage ... Because
I already know the WSDL for the service publication, so I can easily construct a message ...
Third: Use message to invoke server side
Needless to say, construct a message you must know three points: (Input this action, contract method and service address).
Okay, let me start by constructing a data contract, specifying the namespace of the service contract and the name of the action in soap
1 [DataContract (Namespace = " http://tempuri.org/ , Name = " Span style= "color: #800000;" >update )] class Test 3 { 4 [DataMember] 5 public string str {set ;} 6 }
Then I plug this data contract into the body of envelope, as follows:
1BasicHttpBinding bingding =NewBasicHttpBinding ();2 3BindingParameterCollection param =Newbindingparametercollection ();4 5 var u = new Test () {str = "King eight Eggs"};6 7Message request = Message.createmessage (MESSAGEVERSION.SOAP11,"http://tempuri.org/IHomeService/Update", u);8 9Ichannelfactory<irequestchannel> factory = bingding. Buildchannelfactory<irequestchannel>(param);Ten One Factory. Open (); A -IRequestChannel channel = Factory. CreateChannel (NewEndpointAddress ("Http://192.168.1.105:19200/HomeServie")); - the Channel. Open (); - - varresult =Channel. Request (request); - + Channel. Close (); - +Factory. Close ();
Next, we ran up to see how the effect ...
See, this is my manual construction of the message, is not too handsome ... Haha, too handsome should be in the back, just also said, since we play all is a message, and you this a few WCF but just put
My message.body took out, that simply I add a message directly in the contract method is not better??? What's the benefit of being free to manipulate a message?? Of course, I can do it in the message
Add some parameters to the header token,client IP address, client's identity, client's time and so on these statistics, right ... This is the most handsome, well, say dry, we modify the server side of the
The contract method is used only to accept the message.
Server side:
1 Public classHomeservice:ihomeservice2 {3 Publicmessage Update (Message message)4 {5 varHeader =message. Headers;6 7 varIP = header. getheader<string> ("IP",string. Empty);8 9 varCurrentTime = header. getheader<string> ("currenttime",string. Empty);Ten One //This is the stats ... AConsole.WriteLine ("client-side ip="+ IP +"Current time ="+currenttime); - - returnMessage.createmessage (message. Version, message. Headers.action +"Response","after I eat KFC, and then kill you this stupid!!! "); the } -}
Client side:
1 namespaceConsoleApplication12 {3 class Program4 {5 Static voidMain (string[] args)6 {7BasicHttpBinding bingding =NewBasicHttpBinding ();8 9BindingParameterCollection param =Newbindingparametercollection ();Ten One varU =NewTest () {str ="King eight eggs" }; A -Message request = Message.createmessage (MESSAGEVERSION.SOAP11,"http://tempuri.org/IHomeService/Update", u); - the //Append IP information to header -Request. Headers.add (Messageheader.createheader ("IP",string. Empty, Dns.gethostbyname (Dns.gethostname ()). addresslist[0]. ToString ())); -Request. Headers.add (Messageheader.createheader ("currenttime",string. Empty, DateTime.Now)); - +Ichannelfactory<irequestchannel> factory = bingding. Buildchannelfactory<irequestchannel>(param); - + Factory. Open (); A atIRequestChannel channel = Factory. CreateChannel (NewEndpointAddress ("Http://192.168.1.105:19200/HomeServie")); - - Channel. Open (); - - varresult =Channel. Request (request); - in Channel. Close (); - to Factory. Close (); + } - } the *[DataContract (Namespace ="http://tempuri.org/", Name ="Update")] $ classTestPanax Notoginseng { - [DataMember] the Public stringSTR {Get;Set; } + } A}
Then we'll use Fiddler to monitor the results:
Now everything is as I wish, well, I think you also probably understand this magical message, do not forget that it is the basic communication unit of WCF, I want to eat KFC ...
15 days Proficient wcf--fourth day you must understand. Communication Unit Message