Summary of WCF

Client to use service

  1. use ChannelFactory to create proxy to use service. Client code show below.
using System;
using System.ServiceModel;
using Artech.WcfServices.Service.Interface;
using System.ServiceModel.Channels;
namespace Artech.WcfServices.Client
{
class Program
{
static void Main(string[] args)
{
using (ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>("calculatorservice"))
{
ICalculator calculator = channelFactory.CreateChannel();
using (OperationContextScope contextScope = new OperationContextScope(calculator as IClientChannel))
{
string sn = "{DDA095DA-93CA-49EF-BE01-EF5B47179FD0}";
string ns = "http://www.artech.com/";
AddressHeader addressHeader = AddressHeader.CreateAddressHeader("sn", ns, sn);
MessageHeader messageHeader = addressHeader.ToMessageHeader();
OperationContext.Current.OutgoingMessageHeaders.Add(messageHeader);
Console.WriteLine("x + y = {2} when x = {0} and y = {1}", 1, 2, calculator.Add(1, 2));
}
}
Console.Read();
}
}
}

we put our endpoing in config file

<configuration>
<system.serviceModel>
<client>
<endpoint name="calculatorservice"
address="http://127.0.0.1:3721/calculatorservice"
binding="ws2007HttpBinding"
contract="Artech.WcfServices.Service.Interface.ICalculator"/>
</client>
</system.serviceModel>
</configuration>
  1. service Contract definition
using System.ServiceModel;
namespace Artech.WcfServices.Service.Interface
{
[ServiceContract(Name = "CalculatorService", Namespace ="http://www.artech.com/")]
public interface ICalculator
{
[OperationContract]
double Add(double x, double y);
}
}
  1. service definition and use host the service
using Artech.WcfServices.Service.Interface;
using System.ServiceModel;
namespace Artech.WcfServices.Service
{
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
public class CalculatorService : ICalculator
{
public double Add(double x, double y)
{
return x + y;
}
}
}

service host

  using (ServiceHost host = new ServiceHost(typeof(CalculatorService)))
{
host.Open();
Console.Read();
}

we put our endpoint definition in the config file

service host config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="Artech.WcfServices.Service.CalculatorService">
<endpoint address="http://127.0.0.1:3721/calculatorservice"
binding="ws2007HttpBinding"
contract="Artech.WcfServices.Service.Interface.ICalculator">
<headers>
<sn xmlns="http://www.artech.com/">{DDA095DA-93CA-49EF-BE01-EF5B47179FD0}</sn>
</headers>
</endpoint>
</service>
</services>
</system.serviceModel>
</configuration>

Linstenuri vs uri

  1. if the listenUriMode of endpoint set to Unique, three answers: 1. if http, the listenUri will add a guid after the Uri; 2. if tcp,and portshare is disable, it will use another port to take as uri. 3. if tcp and portshare is enable, it will use the same uri but add guid after it.

WCF note1的更多相关文章

  1. 【WCF全析(一)】--服务协定及消息模式

    上周微软开发布会说.NET支持完全跨平台和并开放Core源码的新闻,让我们顿时感到.NET要迎来它的春天.虽然早在几年前.NET就能开发Android和IOS,但是这次的跨平台把Linux都放到了微软 ...

  2. 【架构之路之WCF全析(一)】--服务协定及消息模式

    上周微软开公布会说.NET支持全然跨平台和并开放Core源代码的新闻,让我们顿时感到.NET要迎来它的春天.尽管早在几年前.NET就能开发Android和IOS,可是这次的跨平台把Linux都放到了微 ...

  3. WCF学习之旅—第三个示例之四(三十)

           上接WCF学习之旅—第三个示例之一(二十七)               WCF学习之旅—第三个示例之二(二十八)              WCF学习之旅—第三个示例之三(二十九)   ...

  4. 【WCF】使用“用户名/密码”验证的合理方法

    我不敢说俺的方法是最佳方案,反正这世界上很多东西都是变动的,正像老子所说的——“反(返)者,道之动”.以往看到有些文章中说,为每个客户端安装证书嫌麻烦,就直接采用把用户名和密码塞在SOAP头中发送,然 ...

  5. 【WCF】错误协定声明

    在上一篇烂文中,老周给大伙伴们介绍了 IErrorHandler 接口的使用,今天,老周补充一个错误处理的知识点——错误协定. 错误协定与IErrorHandler接口不同,大伙伴们应该记得,上回我们 ...

  6. 【WCF】自定义错误处理(IErrorHandler接口的用法)

    当被调用的服务操作发生异常时,可以直接把异常的原始内容传回给客户端.在WCF中,服务器传回客户端的异常,通常会使用 FaultException,该异常由这么几个东东组成: 1.Action:在服务调 ...

  7. [WCF]缺少一行代码引发的血案

    这是今天作项目支持的发现的一个关于WCF的问题,虽然最终我只是添加了一行代码就解决了这个问题,但是整个纠错过程是痛苦的,甚至最终发现这个问题都具有偶然性.具体来说,这是一个关于如何自动为服务接口(契约 ...

  8. 【原创经验分享】WCF之消息队列

    最近都在鼓捣这个WCF,因为看到说WCF比WebService功能要强大许多,另外也看了一些公司的招聘信息,貌似一些中.高级的程序员招聘,都有提及到WCF这一块,所以,自己也关心关心一下,虽然目前工作 ...

  9. Ajax使用WCF实现小票pos机打印源码

    通过ajax跨域方式调用WCF服务,实现小票pos机的打印,源码提供web方式,客户端方式测试,服务驻留右侧底部任务栏,可控制服务开启暂停,用户可自定义小票打印模板,配合零售录入. qq  22945 ...

随机推荐

  1. hdu 3336【Count the string】(KMP)

    一道字符串匹配的题目,仅仅借此题练习一下KMP 因为这道题目就是要求用从头开始的n个字符串去匹配原来的字符串,很明显与KMP中求next的过程很相似,所以只要把能够从头开始匹配一定个数的字符串的个数加 ...

  2. JavaScript设计模式

    -->面向对象中的23种设计模式简介 最近看了一本书,推荐给大家<JavaScript设计模式与开发实践>图灵出版社的,讲的非常棒! 详细讲解了js开发中常用的14种设计模式,有很多 ...

  3. android Tab =viewpager+fragmnet

    1.定义几个fragment 的subclass 如fragmentone,fragmenttwo; public class fragmentthree extends Fragment { pri ...

  4. ionic cordova social media sharing plugin

    https://github.com/EddyVerbruggen/SocialSharing-PhoneGap-Plugin.git 从上面的连接下载 插件 1.肯定是要加入 下面的那个文件的吖 从 ...

  5. 2016-03-10:libx265源码解析

    单步跟踪执行流程 将cli设定为启动项目,在属性->调试->命令行参数中设置如下参数: --input E:\video\pedestrian_area.yuv --fps 24 --in ...

  6. H264转成RGB24格式-2016.01.21

    #include "libavcodec/avcodec.h" #include "libswscale/swscale.h" #include "l ...

  7. windows网络编程-2015.12.29

    在windows环境下,使用netstat命令查看网络状态,具体命令如下所示: netstat -ano | findstr listenport 在windows环境下,创建udp程序接收端,具体代 ...

  8. 无法完成安装:'unsupported configuration: hda-duplex not supported in this QEMU binary'

    Linux 有问必答:如何修复"hda-duplex not supported in this QEMU binary" 编译自:http://ask.xmodulo.com/h ...

  9. WF4.0 Activities<第一篇>

    一.基元工具 1.Delay Delay用于延迟一段时间执行下面的流程.在WF中实例是单线程运行的,Delay并不是Thread.Sleep方法实现的. Delay有一个Duration属性,用于设置 ...

  10. Windows phone 8 学习笔记(1) 触控输入(转)

    Windows phone 8 的应用 与一般的Pc应用在输入方式上最大的不同就是:Windows phone 8主要依靠触控操作.因此在输入方式上引入一套全新的触控操作方式,我们需要重新定义相关的事 ...