I recently bought a book on WCF Service programming and started to learn about it again. Now I will learn the record for later review and hope to make progress together with you.
WCF uses an endpoint to represent an integral relationship. An endpoint is a mixture of addresses, contracts, and bindings, that is, addresses, contracts, and Binding ).
In a small example, call our first WCF program. I love to use Demo for learning, and I will learn it step by step.
I used vs2012 to create a WCF Service Application.
Displayed after running
This is the WCF server. We will not process it first. Next we will add the WCF client, and we will add a common console program.
Add service reference:
The client must first import the service contract to the local description of the client to call the service operation. If the client uses WCF, a common method for calling operations is to use a proxy. A proxy is a CLR class that exposes a separate CLR interface to represent a service contract.
We create our proxy
namespace ConsoleApplication1{ class MyContractClient : ClientBase<ServiceReference1.IService1>, ServiceReference1.IService1 { public MyContractClient() { } public MyContractClient(string endpointName) : base(endpointName) { } public string GetData(int value) { return Channel.GetData(value); } public ServiceReference1.CompositeType GetDataUsingDataContract(ServiceReference1.CompositeType composite) { throw new NotImplementedException(); } public Task<string> GetDataAsync(int value) { throw new NotImplementedException(); } public Task<ServiceReference1.CompositeType> GetDataUsingDataContractAsync(ServiceReference1.CompositeType composite) { throw new NotImplementedException(); } }}
This class inherits from ClientBase and must be referenced by using System. ServiceModel.
The contract and binding between the client and the server must be consistent. Otherwise, an error will be reported. We will change the server configuration file and modify the binding.
Modify the client configuration file
Both the server and client use basicHttpBinding binding. Finally, we add the call code in the Program startup class:
class Program { static void Main(string[] args) { using (MyContractClient client = new MyContractClient()) { string result = client.GetData(3); Console.WriteLine(result); } } }
After running, the server method is called and the returned value is. The GetData method of the server is
// Note: You can use the "RENAME" command on the "refactoring" menu to change the class name "Service1" in the code, svc, and configuration file at the same time ". // Note: To start the WCF test client to test this service, select Service1.svc or Service1.svc. cs in Solution Explorer and start debugging. 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 ;}}
Therefore, the client call result is
The above is a small example of the wcf I created. I hope it will be helpful to new users.