with a brief introduction to WCF in the previous article, here's a small example of how to build and apply a WCF application. 1. Add a WCF service: 2. Open the newly created service interface Ishowmyname and add the method (be careful not to discard the service tag or access it):
3. Add a method to implement the service:
4. Look in the browser as shown to indicate success:
5. Publish it in IIS so that the server is configured.
6. Client configuration, the first thing to do is to add the configuration file:(1
) Configure the Web. config
< Configuration >
< system.web >
< compilation Debug="true" targetframework="4.0" />
</ system.web >
< System.ServiceModel >
< Behaviors >
< servicebehaviors >
< Behavior name="">
< Servicemetadata httpgetenabled="true" />
< Servicedebug includeexceptiondetailinfaults="false" />
</ Behavior >
</ servicebehaviors >
</ Behaviors >
< servicehostingenvironment multiplesitebindingsenabled="true" />
< Bindings >
< BasicHttpBinding >
< binding name="basichttpbinding_default"
closetimeout="00:01:00" opentimeout=" c15>00:01:00" receivetimeout="00:10:00"
sendtimeout = " Span style= "Color:blue" >00:01:00 "&NBSP; AllowCookies = "false " &NBSP; bypassproxyonlocal = "false "
hostnamecomparisonmode = "strongwildcard " &NBSP; maxbuffersize = "65536 "
maxbufferpoolsize = "524288 " &NBSP; = "65536 " &NBSP; messageencoding = "text "
textencoding = "utf-8 "&NBSP; transfermode = "buffered " usedefaultwebproxy = " true ">
< readerquotas maxdepth = " 32 "&NBSP; Maxstringcontentlength = "8192 " &NBSP; maxarraylength = " Span style= "Color:blue" >16384 "
maxbytesperread="4096" maxnametablecharcount=" c8>16384" />
< Security mode="None">
< Transport clientcredentialtype="None" proxycredentialtype= "None"
Realm="" />
< message clientcredentialtype="UserName" algorithmsuite=" Default" />
</ Security >
</ binding >
</ BasicHttpBinding >
</ Bindings >
< Client >
< Endpoint
Address ="http://localhost:8080/WCFService/ShowMyName.svc?wsdl"
binding ="basichttpbinding" bindingconfiguration=" Basichttpbinding_default"
contract="contracts.ishowmyname"
name="basichttpbinding_ishowmyname" />
</ Client >
</ System.ServiceModel >
</ Configuration >
(2) client Invoke service
To add a reference to the client:
using System.ServiceModel.Channels;
Using System.ServiceModel ; You can then add code to invoke the WCF service:
//
because the service is created through a channel factory, it is necessary to instantiate the channel factory first
ChannelFactory <ishowmyname> Factory = new channelfactory <ishowmyname>("Basichttpbinding_ishowmyname");
ishowmyname MyObject = Factory. CreateChannel ();
Methods for invoking a service
string time = Myobject.showname (this. txtname.text);
WCF Instance App