WCF Endpoint作用体现

 

WCF Endpoint作用体现

来源:http://developer.51cto.com/art/201002/184802.htm   出处还有很多有价值的文章

WCF Endpoint可以帮助我们地址,绑定以及契约这三方面紧密的联系起来,帮助客户端进行调用。在这里将会对此一一介绍。

WCF是由微软公司开发的一种功能强大的开发插件,主要应用于.NET Framework 3.5,可以帮助我们实现许多特定的功能需求。在这里我们竟会为大家详细介绍一下有关WCF Endpoint的相关应用方法。

·       WCF事件通知具体实现方法详解

·       WCF Bug解决方案详解

·       六步骤打造WCF客户端

·       WCF Session基本应用特点解析

·       WCF体系结构详细内容剖析

每一个 WCF 服务都会关系到地址(Address)、绑定(Binding)和契约(Contract),而 WCF 则通过 Endpoint 将 ABC 三个方面联系在一起。每一个 Endpoint 都必须包括 ABC 三个方面,缺一不可,而 host 进程会提供WCF Endpoint供客户端调用。每个 Endpoint 都对应一个唯一地址,但是多个 Endpoint 可以共享相同的绑定和契约,每个服务又可以提供多个 Endpoint 供客户端掉用。

使用配置文件

再次体现 Microsoft 的傻瓜式编程。唯一值得注意的地方是在 service 节点中添加了 behaviorConfiguration 属性。

<?xml version="1.0"?>

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">

  <system.serviceModel>

    <services>

      <service name="Anrs.Service.AnrsService" behaviorConfiguration="returnFaults">

        <endpoint contract = "Anrs.Service.IAnrsServiceContract1"

        binding = "wsHttpBinding"

        address = "http://localhost:4021/AnrsServiceByIIS/AnrsService/" />

      </service>

    </services>

    <behaviors>

      <serviceBehaviors>

        <behavior name="returnFaults" >

          <serviceMetadata httpGetEnabled="true"></serviceMetadata>

          <serviceDebug includeExceptionDetailInFaults="true" />

        </behavior>

      </serviceBehaviors>

    </behaviors>

    </system.serviceModel>  

  <system.web>

    <compilation debug="true"/>

  </system.web>

</configuration>

使用配置文件的好处自不待言,无论是修改了服务的地址、绑定还是契约,都不需要重新编译甚至部署。配置完成后,就能在浏览器中看到如下的画面了。

编程控制WCF Endpoint

相对于配置文件的简单,编程控制 Endpoint 也不会多几行代码。下面的代码就相当于上面的配置文档。

using System;

using System.ServiceModel;

using System.ServiceModel.Channels;

namespace Anrs.Service

{

    class Program

    {

        static void Main(string[] args)

        {

            ServiceHost sh = new ServiceHost(typeof(AnrsService));

            Binding wsHttpBinding = new WSHttpBinding();

            sh.AddServiceEndpoint(typeof(IAnrsServiceContract1), wsHttpBinding, new Uri("http://localhost:8086/AnrsService/"));

            sh.Open(); Console.Write("Press any key to exit");

            Console.ReadLine();

            sh.Close();

        }

    }

}

WCF Endpoint的相关内容就为大家介绍到这里。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值