WCF终结点和寻址之--AddressHead信息匹配代码

Contracts契约

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel; namespace Artech.WcfServices.Contracts
{
[ServiceContract(Name="CalculatorService",Namespace="http://www.artech.com/")]
public interface ICalculator
{ [OperationContract]
double Add(double x, double y); [OperationContract]
double Subtract(double x, double y); [OperationContract]
double Multiply(double x, double y); [OperationContract]
double Divide(double x, double y);
}
}

Services服务,

完全匹配属性:[ServiceBehavior(AddressFilterMode=AddressFilterMode.Any)]

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Artech.WcfServices.Contracts;
using System.ServiceModel; namespace Artech.WcfServices.Services
{
[ServiceBehavior(AddressFilterMode=AddressFilterMode.Any)]
public class CalculatorService : ICalculator
{
public double Add(double x, double y)
{
return x + y;
} public double Subtract(double x, double y)
{
return x - y;
} public double Multiply(double x, double y)
{
return x * y;
} public double Divide(double x, double y)
{
return x / y;
}
}
}

Hosting主机注册,

需要匹配的信息

string headevalue = "licensed user";
string headerNmae = "userType";
string headNamespace = "http://www.artech.com/";

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using Artech.WcfServices.Contracts;
using Artech.WcfServices.Services;
using System.ServiceModel.Channels; namespace Artech.WcfServices.Hosting
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost servicehost = new ServiceHost(typeof(CalculatorService),
new Uri("http://127.0.0.1:9999/CalculatorService")))
{
string headevalue = "licensed user";
string headerNmae = "userType";
string headNamespace = "http://www.artech.com/"; AddressHeader header = AddressHeader.CreateAddressHeader(headerNmae, headNamespace, headevalue); EndpointAddress endpointAddress = new EndpointAddress
(new Uri("http://127.0.0.1:9999/CalculatorService"), header); ServiceEndpoint serviceEndpoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(ICalculator)), new
WSHttpBinding(), endpointAddress); servicehost.Description.Endpoints.Add(serviceEndpoint); ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
behavior.HttpGetEnabled = true;
servicehost.Description.Behaviors.Add(behavior); servicehost.Opened += delegate
{
Console.WriteLine("Service begin to listen via the Address:{0}",
servicehost.BaseAddresses[].ToString());
}; servicehost.Open(); Console.ReadLine(); } }
}
}

客户端调用

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<client>
<endpoint name="CalculatorService" address="http://127.0.1.1:9999/CalculatorService" binding ="wsHttpBinding"
contract="Artech.WcfServices.Contracts.ICalculator" >
<headers>
<userType xmlns="http://www.artech.com/">chenJian</userType>
</headers>
</endpoint>
</client>
</system.serviceModel>
</configuration>

客户端-App.Config配置

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Artech.WcfServices.Contracts;
using System.ServiceModel;
using System.ServiceModel.Channels; namespace ClientConsole
{
class Program
{
static void Main(string[] args)
{
using (ChannelFactory<ICalculator> channelFactory = new
ChannelFactory<ICalculator>("CalculatorService"))
{
ICalculator proxy = channelFactory.CreateChannel();
using (proxy as IDisposable)
{
Console.WriteLine("x+y={0}", proxy.Add(, ));
Console.WriteLine("x-y={0}", proxy.Subtract(, )); Console.ReadLine(); }
}
}
}
}

WCF技术剖析 Two的更多相关文章

  1. WCF技术剖析之三十:一个很有用的WCF调用编程技巧[下篇]

    原文:WCF技术剖析之三十:一个很有用的WCF调用编程技巧[下篇] 在<上篇>中,我通过使用Delegate的方式解决了服务调用过程中的异常处理以及对服务代理的关闭.对于<WCF技术 ...

  2. WCF技术剖析之三十:一个很有用的WCF调用编程技巧[上篇]

    原文:WCF技术剖析之三十:一个很有用的WCF调用编程技巧[上篇] 在进行基于会话信道的WCF服务调用中,由于受到并发信道数量的限制,我们需要及时的关闭信道:当遇到某些异常,我们需要强行中止(Abor ...

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

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

  4. WCF技术剖析之二十七: 如何将一个服务发布成WSDL[基于HTTP-GET的实现](提供模拟程序)

    原文:WCF技术剖析之二十七: 如何将一个服务发布成WSDL[基于HTTP-GET的实现](提供模拟程序) 基于HTTP-GET的元数据发布方式与基于WS-MEX原理类似,但是ServiceMetad ...

  5. WCF技术剖析之二十八:自己动手获取元数据[附源代码下载]

    原文:WCF技术剖析之二十八:自己动手获取元数据[附源代码下载] 元数据的发布方式决定了元数据的获取行为,WCF服务元数据架构体系通过ServiceMetadataBehavior实现了基于WS-ME ...

  6. WCF技术剖析之二十七: 如何将一个服务发布成WSDL[基于WS-MEX的实现](提供模拟程序)

    原文:WCF技术剖析之二十七: 如何将一个服务发布成WSDL[基于WS-MEX的实现](提供模拟程序) 通过<如何将一个服务发布成WSDL[编程篇]>的介绍我们知道了如何可以通过编程或者配 ...

  7. WCF技术剖析之二十七: 如何将一个服务发布成WSDL[编程篇]

    原文:WCF技术剖析之二十七: 如何将一个服务发布成WSDL[编程篇] 对于WCF服务端元数据架构体系来说,通过MetadataExporter将服务的终结点导出成MetadataSet(参考< ...

  8. WCF技术剖析之二十六:如何导出WCF服务的元数据(Metadata)[扩展篇]

    原文:WCF技术剖析之二十六:如何导出WCF服务的元数据(Metadata)[扩展篇] 通过<实现篇>对WSDL元素和终结点三要素的之间的匹配关系的介绍,我们知道了WSDL的Binding ...

  9. WCF技术剖析之二十六:如何导出WCF服务的元数据(Metadata)[实现篇]

    原文:WCF技术剖析之二十六:如何导出WCF服务的元数据(Metadata)[实现篇] 元数据的导出就是实现从ServiceEndpoint对象向MetadataSet对象转换的过程,在WCF元数据框 ...

  10. WCF技术剖析之二十五: 元数据(Metadata)架构体系全景展现[元数据描述篇]

    原文:WCF技术剖析之二十五: 元数据(Metadata)架构体系全景展现[元数据描述篇] 在[WS标准篇]中我花了很大的篇幅介绍了WS-MEX以及与它相关的WS规范:WS-Policy.WS-Tra ...

随机推荐

  1. 【jQuery】手机验证码倒计时效果

    <ul class="ulist"> <li class="group"> <label class="label&qu ...

  2. Linux(CentOS6.5 x64)下版本安装及升级kangle+EasyPanel

    说明:(easypanel集成了kangle web 服务器和mysql,仅支持centos 5和centos 6) .执行下面的命令即可,安装程序将自动安装或者升级: yum -y install  ...

  3. hive连接MySQL报错

    错误如下: [root@awen01 /usr/local/apache-hive-1.2.1-bin]#./bin/hive Logging initialized using configurat ...

  4. 1014-32-首页13-cell的结构分析---导航栏中间title位置的按钮的尺寸设置---setFrame----

    #import "HWTitleButton.h" #define HWMargin 5 @implementation HWTitleButton - (id)initWithF ...

  5. Social Media Addiction【社交媒体上瘾】

    Social Media Addiction Children as young as ten are becoming dependent on social media for their sen ...

  6. c++ function和bind

    bind 定义在头文件 functional 里 template<typename _Func, typename... _BoundArgs> inline typename _Bin ...

  7. 中国剩余定理算法详解 + POJ 1006 Biorhythms 生理周期

    转载请注明出处:http://exp-blog.com/2018/06/24/pid-1054/ #include <iostream> #include <cstdio> u ...

  8. P2285 [HNOI2004]打鼹鼠

    P2285 [HNOI2004]打鼹鼠 题目描述 鼹鼠是一种很喜欢挖洞的动物,但每过一定的时间,它还是喜欢把头探出到地面上来透透气的.根据这个特点阿牛编写了一个打鼹鼠的游戏:在一个n*n的网格中,在某 ...

  9. android stadio svn 使用技巧

    有时候有这样的需求: 就是我一次要改很多的需求,然后代码要分开提交,那么怎么办? 提交的时候一个一个的点开看? 比如:这次改的还没有提上去,又来了一个需求,怎么区分呢 新建一个active的变化列表 ...

  10. javascript类式继承模式#1——默认模式

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...