服务三要素ABC
A:Address 在哪里 (包含传输方式的信息)B:Binding 怎么实现(与地址的传输方式要匹配)C:Contract做什么(服务契约)
namespace WCFServiceDemo{ [ServiceContract] public interface IHelloService { [OperationContract] DateTime GetDateTime(); [OperationContract] DataTable GetUserInfo(); }}
继承接口
class HelloService:IHelloService { public DateTime GetDateTime() { return DateTime.Now; } public DataTable GetUserInfo() { string connstring = @"Data Source=WIN7U-20130122R\SQLEXPRESS;Initial Catalog=TCEKT;Persist Security Info=True;User ID=sa"; SqlConnection conn = new SqlConnection(connstring); SqlDataAdapter sda=new SqlDataAdapter("select * from User",conn); DataTable dt = new DataTable(); sda.Fill(dt); return dt; } }
Winfom调用
public partial class Form1 : Form { public Form1() { InitializeComponent(); } ServiceHost host = null; private void button1_Click(object sender, EventArgs e) { host = new ServiceHost(typeof(WinformHello.HelloService)); host.Open(); label1.Text = "服务已启动!"; } }
配置文件App.config
启动:
因win7安全性问题,如果启动不了 vs2012 右键
如果启动成功
在app.config文件中找到
<baseAddresses>
<add baseAddress="http://localhost:8001/Hello"/> </baseAddresses>Ctrl键+左击单击
OK 成功……
--学云网Tiger老师的视频