第一个 WCF项目 与 Ajax

WCF(Windows Communication Foundation)是作为.Net framework 3.0发布的,所以只有2008及其以上的版本才可以创建wcf应用程序

WCF是对现有分布式通信技术的整合,其中包括Com/DCom、.Net Remoting、Web服务及其WSE(web服务的升级版本)、MSMQ。

现在决定 学习WCF ,所以在博客园里参考 http://www.cnblogs.com/jiagoushi/archive/2013/03/15/2962351.html  亲手练习了一番

1.创建wcf的项目。我们按照在项目中会实际用到的项目结构来组织。

我来解释一下这些项目的结构。

  1. ConsoleHosting 是一个控制台应用程序,用来承载Wcf服务。你会说什么叫承载?说白了就是可以访问到wcf服务,因为创建了一个wcf应用程序,就相当于一台机器人,但是不给他电池,他就没办法走路,承载就相当于给他动力,他就可以运行。wcf的承载方式可以分为自承载(Self Hosting) 和IIS承载(在IIS中 通过像访问web网站一样访问)。
  2. Contracts 项目是一个类库项目,他是用来存放wcf的契约,就是一些接口
  3. Services也是一个类库项目,他是用来存放实现了契约的服务,就是一些实现了接口的类
  4. WebClient 是一个web项目,采用的是MVC 4.用来作为调用wcf服务的客户端存在。
  5. WebHosting 是一个web项目,采用的是MVC4,用来作为承载wcf服务。可能你会说有两个承载wcf的项目,多余,是的,我在这里的目的就是为了说明,wcf可以有多种承载方式。

首先在Contracts 项目中添加一个接口,然后添加两个计算数值的方法。

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

using System.ServiceModel;


namespace Contracts
{
    /*
        契约类
     */
    
    // 定义WCF服务协定
    [ServiceContract(Name="ICal",Namespace ="http://www.Chinaer.com")]
    public interface ICal
    {
        [OperationContract]
        int add (int x, int y);

        int Sub (int x, int y);
    }
}

注意:在方法中Add上面有OperationContract 操作契约,但是在Sub方法上没有添加这个Attribute。

在服务类中实现这个契约,在Services中添加一个类,实现这个接口。

using Contracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Services
{
    /*
        服务类
     */
    public class CalServices:ICal
    {
        public int add (int x, int y)
        {
            int num = x + y;
            return num;
        }

        public int Sub (int x, int y)
        {
            return x - y;
        }
    }
}

实现了服务类,下面就需要承载wcf服务,这个wcf相对比较简单,没有使用配置文件,通过编程方式来承载。当然我们一般在实际项目中不推荐这么做。

首先通过Console 控制台承载,其实控制台承载和Web承载的编程代码是一样的,只是项目类型不同而已。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

using System.ServiceModel.Description;
using Contracts;
using Services;

namespace ConsoleHosting
{
    class Program
    {
        static void Main (string[] args)
        {
            using( ServiceHost host = new ServiceHost(typeof(CalServices), new Uri("http://127.0.0.1:8081")) ) 
            {
                //  ServiceEndPoint 总结点 包含 Address 地址  Bingding 绑定  Contract 契约 简称ABC
                host.AddServiceEndpoint(typeof(ICal), new WSHttpBinding(), "calService");
                
                // 添加服务总结点
                if( host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null ) 
                {
                    // 判断是否在配置文件中定义了元素总结点
                    ServiceMetadataBehavior metadata = new ServiceMetadataBehavior();
                    metadata.HttpGetEnabled = true;
                    metadata.HttpGetUrl = new Uri("http://127.0.0.1:8081/CalService/MetaData");
                    
                    // 添加元数据总结点
                    host.Description.Behaviors.Add(metadata);
                }

                host.Opened += delegate { Console.WriteLine("WCF已经启动,请按任意键结束!!"); };

                if( host.State != CommunicationState.Opened ) 
                {
                    host.Open();
                }
                Console.Read();
            }
        }
    }
}

添加了宿主以后就可以通过浏览器查看服务元数据。要查看元数据,首先要启动控制台程序。

在浏览器中输入服务元数据地址 就可以访问到元数据,如果代码正确可以看到如下结果。

出现了上面的元数据结果,就表示wcf是可以正常调用的。wcf服务是通过元数据的方式对外发布的,ServiceMedataBehavior是元数据发布的Behavior。

既然服务已经发布成功,那么我们就在客户端调用看是否可以得到结果。

 

可以看到调用wcf服务成功。我把在MVC中的一些简单处理介绍一下。

首先我在HomeController中添加了一个Add方法(这里会根据请求的方式来调用对应的方法)用来调用Wcf服务

        [HttpPost]
        public ActionResult Add (string first,string second)
        {
            CalServices client = new CalServices();
            int number = client.add(Convert.ToInt32(first),Convert.ToInt32(second));
            return Json(number,JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public int Add ()
        {
            return 0;
        }

界面的布局和 使用Ajax的使用

 效果如下(样式就没有贴出来了)

主页面


这样,我们的一个简单的访问wcf的程序就完成了。wcf的范围很广,以后我们一起来交流。如果要下载源码 :http://pan.baidu.com/share/link?shareid=458336&uk=1610729480

转载于:https://www.cnblogs.com/liqie/p/3840639.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值