客户端调用WCF服务可以通过添加服务引用的方式添加,这种方式使用起来比较简单,适合小项目使用。服务端与服务端的耦合较深,而且添加服务引用的方式生成一大堆臃肿的文件。本例探讨一种使用接口的方式使用WCF服务,克服通过服务引用方式产生的弊端。同时希望抛砖引玉,探讨更好的方式使用WCF。

1. 架构概述

解决方案

说明:

接口层:数字计算接口

服务实现层:实现数字计算接口

发布:同过IIS方式发布WCF服务

客户端:引用接口层,通过配置文件调用WCF服务

2. 接口层

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace Hbb0b0.WCF.Inteface
{
/// <summary>
/// 数学计算服务
/// </summary>
[ServiceContract]
public interface IMathService
{
/// <summary>
/// 相加服务
/// </summary>
/// <param name="p1"></param>
/// <param name="p2"></param>
/// <returns></returns>
[OperationContract]
int Add(int p1,int p2);
}
}

3. 实现层

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Hbb0b0.WCF.Inteface;
namespace Hbb0b0.WCF.ServiceImp
{
/// <summary>
/// 计算服务实现
/// </summary>
public class MathService : IMathService
{
#region IMathService 成员
public int Add(int p1, int p2)
{
return p1 + p2;
}
#endregion
}
}

4. 发布层

1. SVC

<%@ServiceHost language=c# Debug="true" Service="Hbb0b0.WCF.ServiceImp.MathService" %>

2. Web.Config

<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="NewBinding0" />
</basicHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="MyServiceTypeBehaviors" name="Hbb0b0.WCF.ServiceImp.MathService">
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<endpoint address="http://localhost:1331/MathService.svc" binding="basicHttpBinding" bindingName="NewBinding0"
name="address" contract="Hbb0b0.WCF.Inteface.IMathService" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors" >
<!-- 将下列元素添加到服务行为配置中。 -->
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>

3. 调用层

1. Client
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Configuration;
using System.ServiceModel.Configuration;
using Hbb0b0.WCF.Inteface;
using System.Reflection;
using System.ServiceModel.Channels;
namespace MathServiceClient
{
class Program
{
/// <summary>
/// ServiceModel 属性
/// </summary>
static ServiceModelSectionGroup ServiceModelConfig;
/// <summary>
/// 构造函数中初始化ServiceModelConfig
/// </summary>
static Program()
{
if(ServiceModelConfig==null)
{
ServiceModelConfig = GetServiceModelSectionGroup();
}
}
static void Main(string[] args)
{
//不知道如何使用配置初始化Binding
//Binding binding = Assembly.GetCallingAssembly().CreateInstance(ServiceModelConfig.Bindings["NewBinding0"].BindingType.FullName) as Binding;
//初始化Endpoint
EndpointAddress point = new EndpointAddress(ServiceModelConfig.Client.Endpoints[0].Address);
//创建通道
IMathService service = ChannelFactory<IMathService>.CreateChannel(
new BasicHttpBinding () ,
point);
//调用
int result= service.Add(2, 3);
Console.WriteLine(string.Format("result={0}", result));
Console.Read();
}
/// <summary>
/// 获取ServiceModel配置信息
/// </summary>
/// <returns></returns>
static ServiceModelSectionGroup GetServiceModelSectionGroup()
{
Configuration conf = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
ServiceModelSectionGroup svcmod = (ServiceModelSectionGroup)conf.GetSectionGroup("system.serviceModel");
return svcmod;
}
}
}
2. AppConfig
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<client>
<endpoint address="http://localhost:1331/MathService.svc" binding="basicHttpBinding"
contract="Hbb0b0.WCF.Inteface.IMathService" name="mathService" />
</client>
</system.serviceModel>
</configuration>

完全使用接口方式调用WCF 服务的更多相关文章

  1. WCF技术剖析之二十九:换种不同的方式调用WCF服务[提供源代码下载]

    原文:WCF技术剖析之二十九:换种不同的方式调用WCF服务[提供源代码下载] 我们有两种典型的WCF调用方式:通过SvcUtil.exe(或者添加Web引用)导入发布的服务元数据生成服务代理相关的代码 ...

  2. Post方式调用wcf服务

    我们平常在PC端调用WCF服务,只要知道WCF服务的地址,客户端直接添加引用服务就可以使用了,殊不知还有其他方式,其实,我们也可以 通过HTTP POST的方式调用WCF服务,这样就不用添加引用了,在 ...

  3. 调用WCF服务的几种方式

    首先发布了一个名为PersonService的WCF服务.服务契约如下: [ServiceContract]     public interface IPersonService     {     ...

  4. 学习之路十四:客户端调用WCF服务的几种方法小议

    最近项目中接触了一点WCF的知识,也就是怎么调用WCF服务,上网查了一些资料,很快就搞出来,可是不符合头的要求,主要有以下几个方面: ①WCF的地址会变动,地址虽变,但是里面的逻辑不变! ②不要引用W ...

  5. C# 调用WCF服务的两种方法

    项目简介 之前领导布置一个做单点登录的功能给我,实际上就是医院想做一个统一的平台来实现在这个统一的平台登录后不需要在His.Emr.Lis等系统一个个登录,直接可以登录到对应的系统,然后进行相应的操作 ...

  6. 实现jquery.ajax及原生的XMLHttpRequest跨域调用WCF服务的方法

    关于ajax跨域调用WCF服务的方法很多,经过我反复的代码测试,认为如下方法是最为简便的,当然也不能说别人的方法是错误的,下面就来上代码,WCF服务定义还是延用上次的,如: namespace Wcf ...

  7. 实现jquery.ajax及原生的XMLHttpRequest调用WCF服务的方法

    废话不多说,直接讲解实现步骤 一.首先我们需定义支持WEB HTTP方法调用的WCF服务契约及实现服务契约类(重点关注各attribute),代码如下: //IAddService.cs namesp ...

  8. SharePoint 2013 调用WCF服务简单示例

    内容比较简单,主要记录自己使用SharePoint 2013WCF服务遇到的小问题和小经验,分享给大家,希望能够给需要的人有所帮助.好吧,进入正题! 第一部分 SharePoint 2013调用自带W ...

  9. [转]学习 WCF (6)--学习调用WCF服务的各种方法

    转自:http://www.cnblogs.com/gaoweipeng/archive/2009/07/26/1528263.html 作者这篇博文写得很全面. 根据不同的情况,我们可以用不同的方法 ...

随机推荐

  1. [ubuntu]--vim命令

  2. October 17th 2016 Week 43rd Monday

    You only live once, but if you do it right, once is enough. 人生只有一次,但如果活对了,一次也就够了. Whether you do it ...

  3. xml dtd 定义元素

    ANY 如果需要定义某个元素的值可以是任意类型,可采用如下语法 <!ELEMENT 元素名 ANY> DTD必须定义XML文档中允许出现的所有元素,所以下面这样是不行的,因为<hel ...

  4. Knockout.js随手记(2)

    计算属性 konckout.js的API文档,写的极为详细和生动,透过MVVM的运作原理,开发时只需专注于定义ViewModel逻辑,不需耗费心力处理TextBox.Select的onchange.o ...

  5. [译]:Orchard入门——导航与菜单

    原文链接:Navigation and Menus 文章内容基于Orchard1.8版本.同时包含Orchard 1.5之前版本的导航参考 Orchard有许多不同的方法来创建菜单.本文将介绍两种较为 ...

  6. Android版:验证手机号码的正则表达式 (转)

    /**  * 验证手机格式  */  public static boolean isMobileNO(String mobiles) {      /*     移动:134.135.136.137 ...

  7. 好用的px转rem的插件

    一个CSS的px值转rem值的Sublime Text 3自动完成插件. 下载地址: https://github.com/flashlizi/cssrem 安装 下载本项目,比如:git clone ...

  8. Android之UI编程(一):线性布局

    package com.example.fk_layout; import android.app.Activity; import android.os.Bundle; public class L ...

  9. STM32之EXTI——外部中断

    互联网的广大网友,大家早上中午晚上好.EXTI...故名思义..EX表外,出..I表示Intrrupt..所以合起来就是外部中断...说到这..我觉得我最近的六级水平(背单词)又进了一步,稍微自夸了下 ...

  10. gVim的pathogen

    1.安装gVim 1.选择Full模式 2. 在vimfiles 文件夹下,会有许多空文件夹.将它们全删掉. 3.将vim74文件夹下的"autoload"文件夹 剪切 到 vim ...