首页 > 代码库 > WCF分分钟入门
WCF分分钟入门
近来学习wcf,总结了一下入门的经验,小白的入门篇,也方便以后复习,省的去查质料。
第一步:创建wcf程序,程序初始化有一个接口和一个实现类写个简单的返回方法就可以了;
第二步:创建一个宿主,也就是服务,写好打开服务的代码和配置文件;
第三步:创建一个客户端服务,运行宿主,打开服务后在客户端添加服务引用;
下面的代码是建立在配置文件的基础上,下面也给出了配置的内容。
具体流程如下:
WCF程序代码
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 // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的类名“Service1”。11 public class Service1 : IService112 {13 public string GetData(string value)14 {15 return string.Format("You entered: {0}", value);16 }17 }18 }
服务宿主代码
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 : Form17 {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//打开服务时触发事件27 {28 rtbMessage.Text = "Service已经启动服务!";29 };30 31 host.Open();//打开服务32 }33 34 private void forServer_FormClosing(object sender, FormClosingEventArgs e)35 {36 host.Close();37 }38 }39 }
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 <headers> 7 <sn xmlns="http://www.artech.com/"> 8 {DDA095DA-93CA-49EF-BE01-EF01-EF5B471779FD0} 9 </sn>10 </headers>11 </endpoint>12 <host>13 <baseAddresses>14 <add baseAddress="http://172.16.140.207:8080/" />15 </baseAddresses>16 </host>17 </service>18 </services>19 <behaviors>20 <serviceBehaviors>21 <behavior name="">22 <!--服务请求地址配置-->23 <serviceMetadata httpGetEnabled="true" httpGetUrl="http://172.16.140.207:8080/IService1/metadata"/>24 <serviceDebug includeExceptionDetailInFaults="false" />25 </behavior>26 </serviceBehaviors>27 </behaviors>28 </system.serviceModel>
客户端代码
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 }
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=http://www.mamicode.com/"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=http://www.mamicode.com/"MyPC\LiuZhen" />22 </identity>23 </endpoint>24 </client>25 </system.serviceModel>
希望每天的自己都比昨天的自己强。
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。