Chapter 9 of the WCF Service-Oriented Application series: binding-custom binding)

Source: Internet
Author: User

In the previous chapter, we introduced the basic knowledge of WCF binding. In this chapter, we use a demo to introduce how to create a custom binding (custombinding)Program. In this chapter, we assume that you have a fixed understanding of WCF. The demo creation process will omit some simple steps. The difference between custom binding and the previously introduced WCF program is that you can modify the configuration file to create custom binding.

Development Environment: Visual Studio 2010 + Net Framework 4.0.

1. Create a WCF Service, mainlyCodeAs follows:

Interface code

  [Servicecontract (namespace= "Http://schemas.xinhaijulan.com/demos/CustomBinding")]
Public InterfaceIhellowcfservice
{
[Operationcontract]
StringHellowcf (StringInputmsg );
}

 

Implementation Code

    Public     Class  Hellowcfservice: ihellowcfservice
{
Public String Hellowcf ( String Inputmsg)
{
Return " The message returned from server: " + Inputmsg;
}
}

2. Modify the app. config file:

For the WCF Service Project, you can use the configuration tool to modify app. config.

2.1 right-click app. config and select Edit WCF configuration, as shown in the following figure:

2.2 add custom binding items, such:

2.3 modify the endpoint using the custombinding method to modify binding and bindingconfiguration, such:

2.4 close the configuration tool and save the settings. The custom configuration file has been modified.

3. Add a client project and add a reference to custombindingserver. The main code is as follows:

Client code

    Static     Void  Main (  String  [] ARGs)
{
Using (Custombindingserver. hellowcfserviceclient Client = New Custombindingserver. hellowcfserviceclient ())
{
Console. writeline ( " Please input some keys: " );
String Inputmsg = Console. Readline ();
Console. writeline ( " The sever will return some message below: " );
Console. writeline (client. hellowcf (inputmsg ));
}

Console. Readline ();
}

4. In this case, we can open the app. config file of the client, and we can see that the configuration method automatically generated in this file is the custombinding method configured on the server, which is consistent with that configured on the server, as shown below:

Configuration file generated by the client

  <?  XML version  =  "  1.0  "  Encoding  = "  UTF-8  "     ?>  
< Configuration >
< System. servicemodel >
< Bindings >
< Custombinding >
< Binding name = " Custombinding_ihellowcfservice " >
< Reliablesession acknowledgementinterval = " 00:00:00. 2000000 " Flowcontrolenabled = " True "
Inactivitytimeout = " 00:10:00 " Maxpendingchannels = " 4 " Maxretrycount = " 8 "
Maxtransferwindowsize = " 8 " Ordered = " True " Reliablemessagingversion = " Default " />
< Binarymessageencoding maxreadpoolsize = " 64 " Maxwritepoolsize = " 16 "
Maxsessionsize = " 2048 " >
< Readerquotas maxdepth = " 32 " Maxstringcontentlength = " 8192 " Maxarraylength = " 16384 "
Maxbytesperread = " 4096 " Maxnametablecharcount = " 16384 " />
</ Binarymessageencoding >
< Httptransport manualaddressing = " False " Maxbufferpoolsize = " 524288 "
Maxcompute edmessagesize = " 65536 " Allowcookies = " False " Authenticationscheme = " Anonymous "
Bypassproxyonlocal = " False " Decompressionenabled = " True " Hostnamecomparisonmode = " Strongwildcard "
Keepaliveenabled = " True " Maxbuffersize = " 65536 " Proxyauthenticationscheme = " Anonymous "
Realm = "" Transfermode = " Buffered " Unsafeconnectionntlmauthentication = " False "
Usedefawebwebproxy = " True " />
</ Binding >
</ Custombinding >
</ Bindings >
< Client >
< Endpoint address = " Http: // localhost: 8732/design_time_addresses/custombindingserver/hellowcfservice/ "
Binding = " Custombinding " Bindingconfiguration = " Custombinding_ihellowcfservice "
Contract = " Custombindingserver. ihellowcfservice " Name = " Custombinding_ihellowcfservice " />
</ Client >
</ System. servicemodel >
</ Configuration >

5. Set the client as the startup Item and run F5. The demo runs normally and the output is as follows:

 
  Please input some keys:
You are a good boy!
The sever willReturnSome message below:
The message returned from server: You are a good boy!

Now, the introduction of custom binding is complete.

Click to download the demo.

 

 
  Author: Xinhai julan
Source: http://Xinhaijulan.cnblogs.com
The copyright is shared by the author and the blog. You are welcome to reprint the copyright. However, you must keep this statement without the author's consent andArticleThe original text connection is clearly displayed on the page. Otherwise, the legal liability is retained.

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.