WCF概要

WCF:Windows Communication Foundation

一、特点概述:

1.统一ASMX, .NET Remoting, 与EnterpriseServices的开发模型:①为各种应用提供单一的编程模型;②基于配置驱动的协议选择,消息格式,进程分配等等

2.面向服务:①构建面向服务的系统设计② 简化实现SOA的方法

3.松耦合:①并没有限制在特定的协议,编码格式,或者主机环境上②所有的选项都可配置

4.可交互:支持Web Services的核心标准

5.已经批准和还未被批准的内容:在可扩展性方面能够快速适用新的协议和更新

6.整合性: 整合Microsoft早期的技术如:COM, Enterprise Services, MSMQ

二、原理示意图:


实现代码:

代码结构


简单的业务逻辑类:HelloIndigoService.cs

using System;
using System.ServiceModel;

namespace HelloIndigo
{
    [ServiceContract(Namespace="http://www.monkeyfu.net")]
    public interface IHelloIndigoService
    {
        [OperationContract]
        string HelloIndigo(string message);
    }

    public class HelloIndigoService : IHelloIndigoService
    { 
        #region IHelloIndigoService Members 
        public string HelloIndigo(string message)
        {
            return string.Format("Receivied message at{0}:{1}", DateTime.Now, message);
        } 
        #endregion
    }
} 

服务端:

using System;
using System.Collections.Generic;
using System.Text; 

//WCF使用到的命名空间
using System.ServiceModel;
using System.ServiceModel.Dispatcher;

namespace Host
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(HelloIndigo.HelloIndigoService)))
            {
                host.AddServiceEndpoint(typeof(HelloIndigo.IHelloIndigoService), new NetTcpBinding(), "net.tcp://localhost:9000/HelloIndigo");
                host.Open();
                Console.ReadLine();
            }
        }
    }
}

客户端:

using System;
using System.Collections.Generic;
using System.Text;

using System.ServiceModel;
using Client.localhost;


namespace Client
{
    [ServiceContract(Namespace = "http://www.monkeyfu.net")]
    public interface IHelloIndigoService
    {
        [OperationContract]
        string HelloIndigo(string message);
    }

    class Program
    {
        static void Main(string[] args)
        {
             IHelloIndigoService proxy = ChannelFactory<IHelloIndigoService>.CreateChannel(new NetTcpBinding(), new EndpointAddress("net.tcp://localhost:9000/HelloIndigo"));
             string s = proxy.HelloIndigo("Hello from client...");
            Console.WriteLine(s);
            Console.ReadLine();
        }
    }
}

原理概述:客户端和服务器端保存相同的接口(契约),至于这个契约可以由服务器端直接发布为服务,这个需要在Config里进行配置:

 <endpoint binding="mexHttpBinding" contract="IMetadataExchange" address="mex"/>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值