完全使用接口方式调用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 作者这篇博文写得很全面. 根据不同的情况,我们可以用不同的方法 ...
随机推荐
- 删除安装的 cocoapods 的缓存方法
清除 Cocoapods 本地缓存 特殊情况下,由于网络或者别的原因,通过 cocoapods 下载的文件可能会有问题.这时候可以删除 Cocoapods 的缓存(~/Library/Caches/C ...
- 51nod1072(wythoff 博弈)
题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1072 题意: 中文题诶~ 思路: 博弈套路是有的, 找np局 ...
- CSS3颜色渐变模式
1.线性渐变:linear-gradient 语法:<linear-gradient> = linear-gradient([ [ <angle> | to <si ...
- SQL 关于apply的两种形式cross apply 和 outer apply(转)
转载链接:http://www.cnblogs.com/shuangnet/archive/2013/04/02/2995798.html apply有两种形式: cross apply 和 oute ...
- Angular.element和$document的使用方法分析,代替jquery
AngularJs是不直接操作DOM的,但是在平时的开发当中,我们有的时候还是需要操作一些DOM的,如果使用原生的JS的话操作过于麻烦,所以大家一般都是使用jQuery,jQuery虽然好用,但是An ...
- Unity4.0的使用
最近公司用到了Unity,自己就研究了一下. 新建一个ASP.NET MVC基本项目,在NuGet上引入Unity4.0.1最新版. 因为我使用的项目为ASP.NET MVC,所以又添加一个Unity ...
- 简单的网络引导安装CentOS7
实验室有几台电脑,里边装有windows,因为实验需求要给其装入CentOS7.但是这几个电脑无法用U盘引导系统的安装,虽然带有光驱,但是又不想麻烦去买碟片,所以便想到用网络引导系统的安装. 1. 软 ...
- codeforces泛做..
前面说点什么.. 为了完成日常积累,傻逼呵呵的我决定来一发codeforces 挑水题 泛做.. 嗯对,就是泛做.. 主要就是把codeforces Div.1的ABCD都尝试一下吧0.0.. 挖坑0 ...
- 一言不合敲代码(1)——DIV+CSS3制作哆啦A梦头像
先展示一下我的头像吧. 作为一个前端ER,我的头像当然不能是绘画工具画出来的.没错,这个玩意是由HTML+CSS代码实现的,过年的某一天晚上无聊花了一个小时敲出来的.来看看它原本的样子: 为什么会变成 ...
- 【JAVA】FOR UPDATE 和 FOR UPDATE NOWAIT 区别 (转)
1.for update 和 for update nowait 的区别:首先一点,如果只是select 的话,Oracle是不会加任何锁的,也就是Oracle对 select 读到的数据不会有任何限 ...