标签:
使用配置文件
1.创建一个类库
添加”System.ServiceModel“引用
IHelloService:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.ServiceModel; namespace wcfService { [ServiceContract] public interface IHelloService { [OperationContract] DateTime GetDataTime(); [OperationContract] DataTable GetUserInfo(); } }
2.创建一个windows窗体程序
添加”System.ServiceModel“,还有项目wcfservice层的引用
HelloService:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.Data.SqlClient; using wcfService; namespace WinformHello { public class HelloService:IHelloService { public DateTime GetDataTime() { return DateTime.Now; } public System.Data.DataTable GetUserInfo() { string sql = @"Data Source=.;Initial Catalog=Students;User ID=sa;Password=abcd1234!"; SqlConnection conn = new SqlConnection(); SqlDataAdapter sda = new SqlDataAdapter("select * from student",conn); //DataSet ds = new DataSet(); DataTable dt = new DataTable(); sda.Fill(dt); return dt; } } }
App.config:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <system.serviceModel> <services> <service name="WinformHello.HelloService" behaviorConfiguration="TestBehavior"> <host> <baseAddresses> <add baseAddress="http://localhost:8080/Hello"/> </baseAddresses> </host> <endpoint address="" binding="basicHttpBinding" contract="wcfService.IHelloService"> </endpoint> </service> </services> <behaviors> <serviceBehaviors> <behavior name="TestBehavior"> <serviceMetadata httpGetEnabled="true"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>
Form1:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.ServiceModel; namespace WinformHello { 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 = "服务已启动……"; } } }
标签:
原文地址:http://www.cnblogs.com/zychengzhiit1/p/4356737.html