服务的三要素(ABC)
A:Address 意味着在哪里(也含有传输方式信息)
B:Binding 意味着怎么做(与地址的传输方式要匹配)
C:Contract意味着做什么(服务契约)
配置文件:
<system.serviceModel>
<services>
<service
name="CalculatorService"
behaviorConfiguration="CalculatorServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/service"/>
</baseAddresses>
</host>
<endpoint address="" binding="wsHttpBinding" contract="Samples.ICalculator" />
<endpoint address="/test" binding="wsHttpBinding"
contract="Samples.ICalculator" />
<endpoint address="net.tcp://localhost:9000/service"
binding="netTcpBinding"
contract="Samples.ICalculator" />
</service>
</services>
下面举例讲解一下WCF服务的调用及简单配置:
首先创建WCF服务应用程序WcfServiceLibrary2
Iservice接口:
using System.Data;
using System.Data.SqlClient;
namespace WcfServiceLibrary2
{
[ServiceContract]
public interface IService1
{
[OperationContract]
DataTable getLinks();
[OperationContract]
void addEmps( link e);
}
[DataContract]
public class link
{
private int id;
[DataMember]
public int Id
{
get { return id; }
set { id = value; }
}
private string site_path;
[DataMember]
public string Site_path
{
get { return site_path; }
set { site_path = value; }
}
private string user_name;
[DataMember]
public string User_name
{
get { return user_name; }
set { user_name = value; }
}
private string user_tel;
[DataMember]
public string User_tel
{
get { return user_tel; }
set { user_tel = value; }
}
}
}
对应的实现:
using System.Data;
using System.Data.SqlClient;
namespace WcfServiceLibrary2
{
// 注意: 如果更改此处的类名“IService1”,也必须更新 App.config 中对“IService1”的引用。
public class Service1 : IService1
{
#region IService1 成员
public System.Data.DataTable getLinks()
{
using (SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=123;database=GjjWebdb"))
{
string sql = " select * from dt_link";
SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
DataTable dt = new DataTable("员工表");
sda.Fill(dt);
return dt;
}
}
public void addEmps(link e)
{
using (SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=123;database=GjjWebdb"))
{
string sql = "insert into dt_link value(@id,@User_name,@User_tel,@Site_path)";
SqlCommand cmd = new SqlCommand(sql,conn);
cmd.Parameters.AddWithValue("@id",e.Id);
cmd.Parameters.AddWithValue("@User_name", e.User_name);
cmd.Parameters.AddWithValue("@User_tel", e.User_tel);
cmd.Parameters.AddWithValue("@Site_path", e.Site_path);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
#endregion
}
}
然后再创建web窗体应用程序WCFServer
1, 先添加引用System.serviceModel,和刚建立的WCF服务WcfServiceLibrary2
2, Form1窗体中添加两个按钮,分别表示服务的开启以及停止,并创建ServiceHost对象
ServiceHost host = null;
//开启
private void button1_Click(object sender, EventArgs e)
{
host = new ServiceHost(typeof(WcfServiceLibrary2.Service1));
host.Open();
label1.Text = "服务已开启";
}
//停止
private void button2_Click(object sender, EventArgs e)
{
if (host.State != CommunicationState.Closed)
{
host.Close();
label1.Text = "服务已停止";
}
}
3, 添加应用程序配置文件App.config
<configuration>
<system.serviceModel>
<services>
<service name="WcfServiceLibrary2.Service1" behaviorConfiguration="testBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:3333"/>
</baseAddresses>
</host>
<endpoint address="" binding="wsHttpBinding" contract="WcfServiceLibrary2.IService1"></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="testBehavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
到此,服务即可启动以及停止,可以用浏览器打开http://localhost:3333查看;