IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    精简版本WCF DEMO

    天下发表于 2016-06-16 01:46:00
    love 0
    using System;
    using System.ServiceModel; //添加System.ServiceModel引用
    using System.ServiceModel.Channels;

    namespace ConsoleApplication1
    {
        [ServiceContract]
        
    public interface IHelloWCF
        {
            [OperationContract]
            
    void Say(string input);
        }
        
    public sealed class HelloService : IHelloWCF
        {
            
    public void Say(string input)
            {
                Console.WriteLine(
    "Hello {0}", input);
            }
        }
        
    class Program
        {
            
    static void Main(string[] args)
            {
                Uri address 
    = new Uri("http://127.0.0.1:8733/IHelloWCF");
                BasicHttpBinding binding 
    = new BasicHttpBinding();
                ServiceHost svc 
    = new ServiceHost(typeof(HelloService));
                svc.AddServiceEndpoint(
    typeof(IHelloWCF), binding, address);
                svc.Opened 
    += delegate {
                    Console.WriteLine(
    "The HelloWCF receiving application is ready");
                };
                svc.Open();

                ChannelFactory
    <IHelloWCF> factory = new ChannelFactory<IHelloWCF>(binding,new EndpointAddress(address));
                IHelloWCF proxy 
    = factory.CreateChannel();
                proxy.Say(
    "张三");
                proxy.Say(
    "李四");
                proxy.Say(
    "王二麻子");


                Console.ReadLine();
                svc.Close();
            }
        }
    }


    天下 2016-06-16 09:46 发表评论


沪ICP备19023445号-2号
友情链接