1.设计服务协定 比较好的方法是接口来定义服务的协定
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 WcfService2 9 { 10 [ServiceContract] // 服务协定定义 11 12 public interface IInterface1 13 { 14 [OperationContract] // 要公开的服务方法 15 string Function1( int value); 16 17 } 18 [ServiceContract] // 定义的第二个服务协定 19 public interface IService1 20 { 21 22 [OperationContract] 23 string GetData( int value); 24 25 [OperationContract] 26 CompositeType GetDataUsingDataContract(CompositeType composite); 27 28 // TODO: Add your service operations here 29 } 30 31 32 [DataContract] // 数据协定 定义 33 public class CompositeType 34 { 35 bool boolValue = true; 36 string stringValue = " Hello "; 37 38 [DataMember] 39 public bool BoolValue 40 { 41 get { return boolValue; } 42 set { boolValue = value; } 43 } 44 45 [DataMember] 46 public string StringValue 47 { 48 get { return stringValue; } 49 set { stringValue = value; } 50 } 51 } 52 }
2.实现服务协定 也就是用类实现已定义好的接口
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 WcfService2 9 { 10 public class Service1 : IService1,IInterface1 11 { 12 public string GetData( int value) 13 { 14 return string.Format( " You entered: {0} ", value); 15 } 16 17 public CompositeType GetDataUsingDataContract(CompositeType composite) 18 { 19 if (composite.BoolValue) 20 { 21 composite.StringValue += " Suffix "; 22 } 23 return composite; 24 } 25 26 public string Function1( int a) 27 { 28 return " hello world! "; 29 } 30 } 31 }
3.承载服务的实现 实现服务的宿主
a.用windowform 实现的宿主
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using WcfService2; 10 using System.ServiceModel; 11 using System.ServiceModel.Description; 12 13 namespace Host001 14 { 15 public partial class Form1 : Form 16 { 17 ServiceHost sHost = null; 18 19 public Form1() 20 { 21 InitializeComponent(); 22 } 23 24 private void button1_Click( object sender, EventArgs e) 25 { 26 Uri uri = new Uri( " http://localhost:8000 "); // 服务的地址 27 28 sHost = new ServiceHost( typeof(Service1), uri); // 创建服务宿主 29 30 sHost.AddServiceEndpoint( typeof(IService1), new WSHttpBinding(), " Service1 "); // 添加第一个终结点 31 sHost.AddServiceEndpoint( typeof(IInterface1), new WSHttpBinding(), " Interface1 "); // 第二个终结点 32 33 ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); // 定义服务传输层 34 smb.HttpGetEnabled = true; // 定义传输的属性 35 sHost.Description.Behaviors.Add(smb); 36 37 38 sHost.Open(); // 打开服务 39 } 40 } 41 }
b.用web服务器做为wcf 的宿主
实现wcf 服务发布 .svc文件
<% @ServiceHost language = " c# " Debug = " true " Service = " WcfService2.Service1 " %>
在web.config 中配置wcf 服务
1 < system.serviceModel > 2 < services > 3 < service name ="WcfService2.Service1" behaviorConfiguration ="MyServiceTypeBehaviors" > 4 5 <!-- This endpoint is exposed at the base address provided by host: http://localhost/servicemodelsamples/service.svc --> 6 < endpoint address ="Service1" 7 binding ="wsHttpBinding" 8 contract ="WcfService2.IService1" /> 9 < endpoint address ="Interface1" 10 binding ="wsHttpBinding" 11 contract ="WcfService2.IInterface1" /> 12 13 <!-- The mex endpoint is explosed at http://localhost/servicemodelsamples/service.svc/mex --> 14 < endpoint address ="mex" 15 binding ="mexHttpBinding" 16 contract ="IMetadataExchange" /> 17 18 </ service > 19 </ services > 20 < behaviors > 21 < serviceBehaviors > 22 < behavior name ="MyServiceTypeBehaviors" > 23 < serviceMetadata httpGetEnabled ="true" /> 24 </ behavior > 25 </ serviceBehaviors > 26 </ behaviors > 27 28 </ system.serviceModel >
4.添加wcf 服务引用
用.net IDE 或 用 svcutil.exe 工具 生成 wcf 服务的 stub 文件
5. 创建wcf 服务 的客户端
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using Client1.ServiceReference2; 10 11 namespace Client1 12 { 13 public partial class Form1 : Form 14 { 15 Service1Client service1 = new Service1Client(); // 实例化终结点1 16 Interface1Client interface1 = new Interface1Client(); // 实例化第二个终结点 17 18 public Form1() 19 { 20 InitializeComponent(); 21 service1.Open(); 22 } 23 24 private void button1_Click( object sender, EventArgs e) 25 { 26 label1.Text = service1.GetData( 21); // 调用wcf的方法 27 CompositeType v = new CompositeType() ; // 生成传入数据协定的数据实例 28 v.BoolValue = true; 29 v.StringValue = " hello "; 30 CompositeType type = service1.GetDataUsingDataContract(v); // 调用wcf的方法 31 string s = interface1.Function1( 1); // 调用wcf的方法 32 } 33 } 34 }