1. Open vs, file-> New-> Project-> WCF Service Application
2. After creating a WCF, add the code in iservice1.svc as follows: For details, see comments.
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. runtime. serialization;
Using system. servicemodel;
Using system. servicemodel. Web;
Using system. text;
Namespace myserver
{
// Note: You can use the "RENAME" command on the "refactor" menu to change the Interface Name "iservice1" in both code and config file together.
[Servicecontract]
Public interface iservice1
{
[Operationcontract]
String getdata (INT value );
[Operationcontract]
Compositetype getdatausingdatacontract (compositetype composite );
// Custom Method
[Operationcontract]
String getname (string name );
// Todo: add your service operations here
}
// Use a data contract as your strated in the sample below to add composite types to service operations.
[Datacontract]
Public class compositetype
{
Bool boolvalue = true;
String stringvalue = "hello ";
[Datamember]
Public bool boolvalue
{
Get {return boolvalue ;}
Set {boolvalue = value ;}
}
[Datamember]
Public String stringvalue
{
Get {return stringvalue ;}
Set {stringvalue = value ;}
}
}
}
3. Add the following code to service1.svc-> service1.svc. CS: For details, see annotations.
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. runtime. serialization;
Using system. servicemodel;
Using system. servicemodel. Web;
Using system. text;
Namespace myserver
{
// Note: You can use the "RENAME" command on the "refactor" menu to change the class name "service1" in code, SVC and config file together.
Public class service1: iservice1
{
Public String getdata (INT value)
{
Return string. Format ("You entered: {0}", value );
}
Public compositetype getdatausingdatacontract (compositetype composite)
{
If (composite = NULL)
{
Throw new argumentnullexception ("composite ");
}
If (Composite. boolvalue)
{
Composite. stringvalue + = "suffix ";
}
Return composite;
}
// Custom Method
Public String getname (string name)
{
Return name;
}
}
}
4. Well, you have successfully created a WCF. Run the command and copy the URL. For example, my website is http: // localhost: 3285/
5. In the same solution, right-click Add-> Project-> Visual C #-> Windows-> console application
6. Right-click the references in the project you added, right-click Add service reference, copy your url to address, click Go, change your namespace to myserver, and click OK.
7. If everything goes well, then program. CS is as follows:
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
// Note the introduction of namespace (myserver is the server name I just set)
Using leleapplication1.myserver;
Namespace consoleapplication1
{
Class Program
{
Static void main (string [] ARGs)
{
Service1client SC = new service1client (); // new object
Console. writeline (SC. getname ("zhangyongbin"); // call the server Method
Console. readkey (); // to pause the output form
}
}
}
Then run the program.