完全使用接口方式调用WCF 服务
客户端调用WCF服务可以通过添加服务引用的方式添加,这种方式使用起来比较简单,适合小项目使用。服务端与服务端的耦合较深,而且添加服务引用的方式生成一大堆臃肿的文件。本例探讨一种使用接口的方式使用WCF服务,克服通过服务引用方式产生的弊端。同时希望抛砖引玉,探讨更好的方式使用WCF。
1. 架构概述
解决方案
说明:
接口层:数字计算接口
服务实现层:实现数字计算接口
发布:同过IIS方式发布WCF服务
客户端:引用接口层,通过配置文件调用WCF服务
2. 接口层
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.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
2. Web.Config
<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.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
<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 服务的更多相关文章
- WCF技术剖析之二十九:换种不同的方式调用WCF服务[提供源代码下载]
原文:WCF技术剖析之二十九:换种不同的方式调用WCF服务[提供源代码下载] 我们有两种典型的WCF调用方式:通过SvcUtil.exe(或者添加Web引用)导入发布的服务元数据生成服务代理相关的代码 ...
- Post方式调用wcf服务
我们平常在PC端调用WCF服务,只要知道WCF服务的地址,客户端直接添加引用服务就可以使用了,殊不知还有其他方式,其实,我们也可以 通过HTTP POST的方式调用WCF服务,这样就不用添加引用了,在 ...
- 调用WCF服务的几种方式
首先发布了一个名为PersonService的WCF服务.服务契约如下: [ServiceContract] public interface IPersonService { ...
- 学习之路十四:客户端调用WCF服务的几种方法小议
最近项目中接触了一点WCF的知识,也就是怎么调用WCF服务,上网查了一些资料,很快就搞出来,可是不符合头的要求,主要有以下几个方面: ①WCF的地址会变动,地址虽变,但是里面的逻辑不变! ②不要引用W ...
- C# 调用WCF服务的两种方法
项目简介 之前领导布置一个做单点登录的功能给我,实际上就是医院想做一个统一的平台来实现在这个统一的平台登录后不需要在His.Emr.Lis等系统一个个登录,直接可以登录到对应的系统,然后进行相应的操作 ...
- 实现jquery.ajax及原生的XMLHttpRequest跨域调用WCF服务的方法
关于ajax跨域调用WCF服务的方法很多,经过我反复的代码测试,认为如下方法是最为简便的,当然也不能说别人的方法是错误的,下面就来上代码,WCF服务定义还是延用上次的,如: namespace Wcf ...
- 实现jquery.ajax及原生的XMLHttpRequest调用WCF服务的方法
废话不多说,直接讲解实现步骤 一.首先我们需定义支持WEB HTTP方法调用的WCF服务契约及实现服务契约类(重点关注各attribute),代码如下: //IAddService.cs namesp ...
- SharePoint 2013 调用WCF服务简单示例
内容比较简单,主要记录自己使用SharePoint 2013WCF服务遇到的小问题和小经验,分享给大家,希望能够给需要的人有所帮助.好吧,进入正题! 第一部分 SharePoint 2013调用自带W ...
- [转]学习 WCF (6)--学习调用WCF服务的各种方法
转自:http://www.cnblogs.com/gaoweipeng/archive/2009/07/26/1528263.html 作者这篇博文写得很全面. 根据不同的情况,我们可以用不同的方法 ...
随机推荐
- ajax表单提交
HTML代码: <form id="formCity" action="/SiteMap/Search" method="get" o ...
- 51nod1118(水题)
题目链接: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1118 题意: 中文题诶~ 思路: 因为机器人只能往下或者右 ...
- Linux第02天
Linux 第02天 1.Linux磁盘和文件系统 VFS————虚拟文件系统 df命令————查看已挂载的分区 df 分区名 du命令————查看文件夹大小 du 文件夹名 ln命令————符号链接 ...
- js文件上传
DOM: <form id="clueForm" class="insert-dialog" action="/xxx/xxx"met ...
- CodeForces 444C 分块
题目链接:http://codeforces.com/problemset/problem/444/C 题意:给定一个长度为n的序列a[].起初a[i]=i,然后还有一个色度的序列b[],起初b[i] ...
- Fzu2124 - 吃豆人 BFS
Description 吃豆人是一款非常经典的游戏,游戏中玩家控制吃豆人在地图上吃光所有豆子,并且避免被怪物抓住. 这道题没有怪物,将游戏的画面分成n*m的格子,每格地形可能为空地或者障碍物,吃豆人可 ...
- 分布式缓存技术redis学习系列(五)——redis实战(redis与spring整合,分布式锁实现)
本文是redis学习系列的第五篇,点击下面链接可回看系列文章 <redis简介以及linux上的安装> <详细讲解redis数据结构(内存模型)以及常用命令> <redi ...
- 《Invert》开发日志04:工具、资源和服务
这篇记录一下<Invert>用到的工具.资源和服务.秉承两个原则:一,绝不侵犯版权:二,尽量节省开支. 首先是工具.游戏引擎使用免费的Unity个人版: 编码IDE使用免费的VisualS ...
- Python for Informatics 第11章 正则表达式五(译)
注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 11.4 转义字符 之前我们在正 ...
- eclipse安装genymotion插件。
先发个我自己压缩的genymotion和VirtualBox,下载链接:http://pan.baidu.com/s/1o7wgJiU 1.在安装genymotion之后,打开eclipse,如下图操 ...