• Linq to sql(十):分层构架的例子(三)


    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();

        }

    }

  • 相关阅读:
    React中autoComplete="off" 失效
    git配置文件—— .editorconfig
    git配置文件—— .gitattributes
    配置文件—— .travis.yml
    python入门:常用模块—sys模块
    python入门:常用模块—random模块
    python入门:常用模块—time & datetime模块
    python入门:常用模块—模块,包介绍
    Python入门:生成器&迭代器
    python入门:函数进阶(名称空间,闭包,装饰器)
  • 原文地址:https://www.cnblogs.com/kevin2013/p/1748998.html
Copyright © 2020-2023  润新知