WCF分分钟入门

时间:2021-10-12 00:08:48

近来学习wcf,总结了一下入门的经验,小白的入门篇,也方便以后复习,省的去查质料。

第一步:创建wcf程序,程序初始化有一个接口和一个实现类写个简单的返回方法就可以了;

第二步:创建一个宿主,也就是服务,写好打开服务的代码和配置文件;

第三步:创建一个客户端服务,运行宿主,打开服务后在客户端添加服务引用;

下面的代码是建立在配置文件的基础上,下面也给出了配置的内容。

具体流程如下:

WCF程序代码

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Runtime.Serialization;
 using System.ServiceModel;
 using System.Text;

 namespace CommunicationsService
 {
     // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的类名“Service1”。
     public class Service1 : IService1
     {
         public string GetData(string value)
         {
             return string.Format("You entered: {0}", value);
         }
     }
 }
服务宿主代码
 using CommunicationsService;
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.ServiceModel;
 using System.ServiceModel.Description;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;

 namespace ServerUI
 {
     public partial class forServer : Form
     {
         public forServer()
         {
             InitializeComponent();
         }
         ServiceHost host = null;
         private void forServer_Load(object sender, EventArgs e)
         {
             host = new ServiceHost(typeof(Service1));
             host.Opened += delegate//打开服务时触发事件
             {
                 rtbMessage.Text = "Service已经启动服务!";
             };

             host.Open();//打开服务
         }

         private void forServer_FormClosing(object sender, FormClosingEventArgs e)
         {
             host.Close();
         }
     }
 }
 <system.serviceModel>
     <services>
       <service name="CommunicationsService.Service1">
         <endpoint address="http://172.16.140.207:8080/Service1" binding="wsHttpBinding"
           bindingConfiguration="" contract="CommunicationsService.IService1">
           <headers>
             <sn xmlns="http://www.artech.com/">
               {DDA095DA-93CA-49EF-BE01-EF01-EF5B471779FD0}
             </sn>
           </headers>
         </endpoint>
         <host>
           <baseAddresses>
             <add baseAddress="http://172.16.140.207:8080/" />
           </baseAddresses>
         </host>
       </service>
     </services>
     <behaviors>
       <serviceBehaviors>
         <behavior name="">
           <!--服务请求地址配置-->
           <serviceMetadata httpGetEnabled="true" httpGetUrl="http://172.16.140.207:8080/IService1/metadata"/>
           <serviceDebug includeExceptionDetailInFaults="false" />
         </behavior>
       </serviceBehaviors>
     </behaviors>
   </system.serviceModel>

客户端代码

 private void forClient_Load(object sender, EventArgs e)
         {
             ChannelFactory<IService1> channelFactory = new ChannelFactory<IService1>("ClientPoints");
             IService1 proxy = channelFactory.CreateChannel();
             rtbMessage.Text = proxy.GetData("hello");
         }
 <system.serviceModel>
     <bindings>
       <wsHttpBinding>
         <binding name="WSHttpBinding_IService1" />
       </wsHttpBinding>
     </bindings>
     <client>
       <endpoint address="http://172.16.140.207:8080/Service1" binding="wsHttpBinding"
         bindingConfiguration="" contract="CommunicationsService.IService1"
         name="ClientPoints" kind="" endpointConfiguration="">
         <identity>
           <dns value="localhost" />
           <certificateReference storeName="My" storeLocation="LocalMachine"
             x509FindType="FindBySubjectDistinguishedName" />
         </identity>
       </endpoint>
       <endpoint address="http://172.16.140.207:8080/Service1" binding="wsHttpBinding"
         bindingConfiguration="WSHttpBinding_IService1" contract="ServiceReference1.IService1"
         name="WSHttpBinding_IService1">
         <identity>
           <userPrincipalName value="MyPC\LiuZhen" />
         </identity>
       </endpoint>
     </client>
   </system.serviceModel>

希望每天的自己都比昨天的自己强。