一步一步学Linq to sql(十):分层构架的例子
[2] 一步一步学Linq to sql(十):分层构架的例子
[3] 一步一步学Linq to sql(十):分层构架的例子
[4] 一步一步学Linq to sql(十):分层构架的例子
系列文章导航:
一步一步学Linq to sql(二):DataContext与实体
WCF服务端与客户端
打开Host项目中的Program.cs,使用下面的代码来实现WCF的服务端:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using Service; using Contract;
namespace Host { class Program { static void Main(string[] args) {
Uri uri = new Uri("net.tcp://localhost:8080/DataAccessService"); using (ServiceHost sh = new ServiceHost(typeof(DataAccess), uri)) { NetTcpBinding ctb = new NetTcpBinding(); sh.AddServiceEndpoint(typeof(IDataAccess), ctb, string.Empty); sh.Opened += delegate { Console.WriteLine("服务已经启动"); }; sh.Open(); Console.ReadLine(); } } } } |
在WebSite项目中的App_Code文件夹下创建一个用户调用服务的类,GetService.cs:
using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Xml.Linq; using Contract; using System.ServiceModel.Description; using System.ServiceModel;
public class GetService { public static IDataAccess GetDataAccessService() { ServiceEndpoint sep = new ServiceEndpoint(ContractDescription.GetContract(typeof(IDataAccess)), new NetTcpBinding(), new EndpointAddress("net.tcp://localhost:8080/DataAccessService"));
ChannelFactory<IDataAccess> cf = new ChannelFactory<IDataAccess>(sep);
return cf.CreateChannel(); } } |