zoukankan      html  css  js  c++  java
  • WCF>一个运行环境,一个服务逻辑人,一个客户

    该例子的过程是:服务逻辑人有了本事,服务通信人和他建立联系。客户指定服务逻辑人,指派客户通信人与服务通信人接触,使用服务逻辑人的本事。

    先教服务逻辑人本事。注意先新建类库,再在类库中新建类。

    服务逻辑人
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ServiceModel;//在类库中添加引用System.ServiceModel

    namespace HelloIndigo//服务逻辑人
    {
        [ServiceContract(Namespace 
    = "http://www.thatindigogirl.com/samples/2006/06")]//服务逻辑人指定,该本事,能被客户调用(术语:服务契约)
        public interface IHeloIndigoService
        {
            [OperationContract]
    //服务逻辑人指定,该本事的具体行为,能被客户调用(术语:操作契约)
            string HelloIndigo();
        }
        
    public class HelloIndigoService:IHeloIndigoService//逻辑人的本事
        {
            
    public string HelloIndigo()//该本事的具体行为
            {
                
    return "Hello Indigo";
            }
        }
    }


    搭建运行环境,里面包括了服务通信人。这是个控制台应用程序。

    运行环境
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ServiceModel;//添加引用>.NET>System.ServiceModel
    //添加引用>项目>逻辑人HelloIndigo

    namespace Host//运行环境
    {
        
    class Program
        {
            
    static void Main(string[] args)
            {
                
    using (ServiceHost host = new ServiceHost(typeof(HelloIndigo.HelloIndigoService), new Uri("net.tcp://localhost:8000/HelloIndigo")))//通信人的交通工具net.tcp,逻辑人地址localhost:8000/HelloIndigo
                {

                    host.AddServiceEndpoint(
    typeof(HelloIndigo.IHeloIndigoService), new NetTcpBinding(), "HelloIndigoService");//通信人加入运行环境,指定逻辑人的本事
                    host.Open();//生成运行环境
                    Console.WriteLine("按回车关闭运行环境(宿主Host)");
                    Console.ReadLine();
                }
            }
        }
    }


    客户发话了。这是个控制台应用程序

    客户
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ServiceModel;//添加引用System.ServiceModel

    namespace Client//客户
    {
        [ServiceContract(Namespace 
    = "http://www.thatindigogirl.com/samples/2006/06")]
        
    public interface IHeloIndigoService //客户指定逻辑人的本事
        {
            [OperationContract]
    //客户指定调用服务逻辑人的HelloIndigo()本事
            string HelloIndigo();
        }
        
    class Program
        {
            
    static void Main(string[] args)
            {
                Console.WriteLine(
    "按1调用服务,按0退出");
                
    string n = Console.ReadLine();

                
    if (n == "1")
                {
                    
    //客户指派客户通信人,与服务通信人接触,约定其交通工具,地址,逻辑人的本事
                    EndpointAddress ep = new EndpointAddress("net.tcp://localhost:8000/HelloIndigo/HelloIndigoService");
                    
    //获取通信人得到的数据
                    IHeloIndigoService proxy = ChannelFactory<IHeloIndigoService>.CreateChannel(new NetTcpBinding(), ep);
                    
    //使用通信人得到的数据
                    string s = proxy.HelloIndigo();
                    Console.WriteLine(s);
                    Console.ReadLine();
                }
                
    else if (n == "0")
                {

                }
            }
        }
    }


    全部编译好后,先启动运动环境,再客户。


    合乎自然而生生不息。。。
  • 相关阅读:
    如何修改自定义Webpart的标题?(downmoon)
    vs2003 和vs2005下的发送SMTP邮件
    Entity Framework 4.1 之八:绕过 EF 查询映射
    Entity Framework 4.1 之七:继承
    Entity Framework 4.1 之四:复杂类型
    Entity Framework 4.1 之三 : 贪婪加载和延迟加载
    MVC2 强类型的 HTML Helper
    EF Code First 和 ASP.NET MVC3 工具更新
    Entity Framework 4.1 之六:乐观并发
    Entity Framework 4.1 之一 : 基础
  • 原文地址:https://www.cnblogs.com/samwu/p/1872921.html
Copyright © 2011-2022 走看看