Original address: http://blog.csdn.net/mane_yao/article/details/5852845
ABC for WCF:
A stands for Address-where (where the object is)
B stands for binding-how (by what agreement to obtain the object)
C for Contact (contract)-what (define what the object is, how to manipulate it)
Service side
1. Create an empty solution: Wcfdemo:
2. Create a host console program: Host
3. Right-click the host project, select "Add"--"New Item" and select "WCF Service" to create a service named "Service1.cs"
So: VS2010 has created IService1.cs Service1.cs App. Config three files for us, where IService1.cs and Service1.cs have created classes that belong to the host namespace, and we can modify these three files:
IService1.cs:
Using System.ServiceModel;
Namespace Host
{
Note: Using the rename command on the Refactor menu, you can change the interface name "IService1" in code and configuration files at the same time.
[ServiceContract]
public interface IService1
{
[OperationContract]
string DoWork (int value);
}
}
Service1.cs:
Using System.ServiceModel;
Namespace Host
{
Note: Using the rename command on the Refactor menu, you can change the class name "Service1" in code and configuration files at the same time.
public class Service1:iservice1
{
public string DoWork (int value)
{
int Ipingfang = value * value;
return string. Format ("After square value: {0}", Ipingfang);
}
}
}
App. Config:
The file can not be changed in principle, but the address is too long (the default is baseaddress= "http://localhost:8732/Design_Time_Addresses/Host/Service1/" ) shortened to baseaddress= "http://localhost:8732/Service1/"
<?xml version= "1.0" encoding= "Utf-8"?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name= "" >
<servicemetadata httpgetenabled= "true"/>
<servicedebug includeexceptiondetailinfaults= "false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name= "Host.service1" >
<endpoint address= "" binding= "Wshttpbinding" contract= "Host.iservice1" >
<identity>
<dns value= "localhost"/>
</identity>
</endpoint>
<endpoint address= "Mex" binding= "mexHttpBinding" contract= "IMetadataExchange"/>
<baseAddresses>
<add baseaddress= "http://localhost:8732/Service1/"/>
</baseAddresses>
</service>
</services>
</system.serviceModel>
</configuration>
4. In addition, the WCF service must run in the host process, and we can modify the Program.cs file to create the host process:
Using System.ServiceModel;
Namespace Host
{
Class Program
{
static void Main (string[] args)
{
using (ServiceHost host = new ServiceHost (typeof (Host.service1)))
{
Host. Open ();
Console.WriteLine ("Service has started ...");
Console.ReadLine ();
Host. Close ();
}
}
}
}
5. Compile and run the program to generate the Host.exe file
Client
1. Start the WCF Service host process that you just created Host.exe
2. Create a client console program: Clients
3. Right-click "Reference"-"Add Service Reference", enter the address of the server in the "Address" TextBox (the BaseAddress address we set earlier), and click "Go" to get the services on the target server, as shown in:
As such, this step will create the client agent (the namespace: using Client.servicereference1;) and the config file,, in the client side, with the SvcUtil.exe file, as follows:
App. Config:
<?xml version= "1.0" encoding= "Utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name= "Wshttpbinding_iservice1" closetimeout= "00:01:00"
opentimeout= "00:01:00" receivetimeout= "00:10:00" sendtimeout= "00:01:00"
Bypassproxyonlocal= "false" transactionflow= "false" hostnamecomparisonmode= "StrongWildcard"
Maxbufferpoolsize= "524288" maxreceivedmessagesize= "65536"
messageencoding= "Text" textencoding= "Utf-8" usedefaultwebproxy= "true"
Allowcookies= "false" >
<readerquotas maxdepth= "+" maxstringcontentlength= "8192" maxarraylength= "16384"
Maxbytesperread= "4096" maxnametablecharcount= "16384"/>
<reliablesession ordered= "true" inactivitytimeout= "00:10:00"
Enabled= "false"/>
<security mode= "Message" >
<transport clientcredentialtype= "Windows" proxycredentialtype= "None"
Realm= ""/>
<message clientcredentialtype= "Windows" negotiateservicecredential= "true"
algorithmsuite= "Default"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address= "http://localhost:8732/Service1/" binding= "Wshttpbinding"
bindingconfiguration= "Wshttpbinding_iservice1" contract= "Servicereference1.iservice1"
Name= "Wshttpbinding_iservice1" >
<identity>
<dns value= "localhost"/>
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>
4. Modify the client program Program.cs and use the proxy to access the service contract:
Using System.ServiceModel;
Using client.servicereference1;//reference namespaces
Namespace Client
{
Class Program
{
static void Main (string[] args)
{
Servicereference1.service1client proxy = new Service1client ();
String str = proxy. DoWork (2);//Run a service-side approach
Console.WriteLine (str);
Console.ReadLine ();
}
}
}
5. Compile and run the program to generate Client.exe
Go: HelloWorld: A complete WCF case