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. 关闭窗体后,利用StreamWriter保存控件里面的数据

    以保存DataGridView里面的数据为例: 通过窗体增加的数据,没有用数据库保存,可以使用StreamWriter将数据存在临时文件里面,再次打开窗体时写入即可. private void For ...

  2. Socket连接

    socket中TCP的三次握手建立连接详解 我们知道tcp建立连接要进行“三次握手”,即交换三个分组.大致流程如下: 客户端向服务器发送一个SYN J 服务器向客户端响应一个SYN K,并对SYN J ...

  3. 感受机房管理化繁为简-新款KVM使用心得

    感受机房管理化繁为简-新款KVM使用心得 一. 背景 随着网络应用的不断增多,各地机房服务器数量也随之增加,利用多传统主机切换器的方式已经无法满足目前这种区域广.设备多人员紧缺的现状,而且即使是使用了 ...

  4. iptable防火墙配置

    /etc/sysconfig/iptables /etc/init.d/iptables {start|stop|restart|condrestart|status|panic|save} ipta ...

  5. 理解python可变类型vs不可变类型,深拷贝vs浅拷贝

    核心提示: 可变类型 Vs 不可变类型 可变类型(mutable):列表,字典 不可变类型(unmutable):数字,字符串,元组 这里的可变不可变,是指内存中的那块内容(value)是否可以被改变 ...

  6. markdown语法学习源码

    __注: 结合markdown官方文档 其中大部分例子和说明文字都摘自官方文档__官方链接:[Markdown: Basics (快速入门)](http://wowubuntu.com/markdow ...

  7. leetcode 8

    string类型转换为int类型,需要考虑不同的转换情况. “   04”  转换结果   4: “   4   43”  转换结果  4: “a@12 ”   转换结果    0: “12a”   ...

  8. Solaris的vi

    进入输入模式i: 在光标之前插入a: 在光标之后插入o: 在下面新建一行输入I: 光标移动到本行首插入A: 光标移动到本行末尾插入O: 在上面新建一行输入 移动光标M:移到屏幕中间一行的行首L:移到屏 ...

  9. 对C语言中va_list,va_start,va_arg和va_end的一点理解

    这几个函数和变量是针对可变参数函数的,什么是可变参数函数呢,最经典的莫过于printf和scanf,这两个函数的声明如下: int printf(const char *format, ...); i ...

  10. 后缀为inc的是什么文件?C#中如何包含inc文件?

    在项目Web页面文件中,发现这么一句话: <!-- 页面字符集设置 begin--><!-- #INCLUDE FILE="http://www.cnblogs.com/C ...