I recently learned about WCF and summarized my experience in getting started. I am also a beginner in this article, which facilitates future review and saves the trouble of checking materials.
Step 1: Create a WCF program. The program initialization has an interface and an implementation class to write a simple return method;
Step 2: Create a host, namely a service, and write the code and configuration file for opening the service;
Step 3: Create a client service, run the host, open the service, and add service references to the client;
The following code is based on the configuration file, and the configuration content is also given below.
The specific process is as follows:
WCF program code
1 using system; 2 using system. collections. generic; 3 using system. LINQ; 4 using system. runtime. serialization; 5 using system. servicemodel; 6 using system. text; 7 8 namespace communicationsservice 9 {10 // Note: use the "RENAME" command on the "refactor" menu to change the class name "service1" in the Code and configuration file at the same time ". 11 public class service1: iservice112 {13 Public String getdata (string value) 14 {15 return string. format ("You entered: {0}", value); 16} 17} 18}View code service host code
1 using communicationsservice; 2 using system; 3 using system. collections. generic; 4 using system. componentmodel; 5 using system. data; 6 using system. drawing; 7 using system. LINQ; 8 using system. servicemodel; 9 using system. servicemodel. description; 10 using system. text; 11 using system. threading. tasks; 12 using system. windows. forms; 13 14 namespace serverui15 {16 public partial class forserver: form1 7 {18 public forserver () 19 {20 initializecomponent (); 21} 22 servicehost host = NULL; 23 private void forserver_load (Object sender, eventargs E) 24 {25 host = new servicehost (typeof (service1); 26 Host. opened + = delegate // trigger event 27 {28 rtbmessage when the service is enabled. TEXT = "service has started the service! "; 29}; 30 31 host. open (); // open the service 32} 33 34 Private void forserver_formclosing (Object sender, formclosingeventargs e) 35 {36 host. close (); 37} 38} 39}View code
1 <system. servicemodel> 2 <services> 3 <service name = "communicationsservice. service1 "> 4 <endpoint address =" http: // 172.16.140.207: 8080/service1 "binding =" wshttpbinding "5 bindingconfiguration =" "Contract =" communicationsservice. iservice1 "> 6 View code
Client code
1 private void forClient_Load(object sender, EventArgs e)2 {3 ChannelFactory<IService1> channelFactory = new ChannelFactory<IService1>("ClientPoints");4 IService1 proxy = channelFactory.CreateChannel();5 rtbMessage.Text = proxy.GetData("hello");6 }View code
1 <system.serviceModel> 2 <bindings> 3 <wsHttpBinding> 4 <binding name="WSHttpBinding_IService1" /> 5 </wsHttpBinding> 6 </bindings> 7 <client> 8 <endpoint address="http://172.16.140.207:8080/Service1" binding="wsHttpBinding" 9 bindingConfiguration="" contract="CommunicationsService.IService1"10 name="ClientPoints" kind="" endpointConfiguration="">11 <identity>12 <dns value="localhost" />13 <certificateReference storeName="My" storeLocation="LocalMachine"14 x509FindType="FindBySubjectDistinguishedName" />15 </identity>16 </endpoint>17 <endpoint address="http://172.16.140.207:8080/Service1" binding="wsHttpBinding"18 bindingConfiguration="WSHttpBinding_IService1" contract="ServiceReference1.IService1"19 name="WSHttpBinding_IService1">20 <identity>21 <userPrincipalName value="MyPC\LiuZhen" />22 </identity>23 </endpoint>24 </client>25 </system.serviceModel>
View code
I hope that I will be better at myself every day than I did yesterday.