您的位置:知识库 »

WCF专题系列(2):深入WCF寻址Part 2—自定义寻址报头

作者: TerryLee  来源: 博客园  发布时间: 2008-10-27 13:10  阅读: 2210 次  推荐: 0   原文链接   [收藏]  
[1] WCF专题系列(1):深入WCF寻址Part 2
[2] WCF专题系列(1):深入WCF寻址Part 2

 

元数据中终结点地址

终结点地址在WSDL中表示为对应终结点的 wsdl:port元素内的终结点引用(EndpointReference)元素。终结点引用包含终结点的地址以及所有的地址属性,如下示例代码所示:

<wsdl:service name="CalculatorService">
  <wsdl:port name="WSHttpBinding_ICalculator" binding="tns:WSHttpBinding_ICalculator">
    <soap12:address location="http://localhost:8887/Calculator" />
    <wsa10:EndpointReference>
      <wsa10:Address>http://localhost:8887/Calculator</wsa10:Address>
      <Identity xmlns="http://schemas.xmlsoap.org/ws/2006/02/addressingidentity">
        <Upn>TerryLee-PC\TerryLee</Upn>
      </Identity>
    </wsa10:EndpointReference>
  </wsdl:port>
</wsdl:service>

自定义寻址报头

在本文的终结点定义一节,我们提到了寻址报头,在某些情况下,我们可能希望通过自定义寻址报头来解决一些复杂的问题,如根据根据传入的寻址报头中是否包含某些信息,将其转发到不同的终结点,通过自定义寻址报头,可以实现SOAP消息的无限扩展,放置任何希望的控制信息到SOAP消息。如下面的代码:

using (ServiceHost calculatorServiceHost =
    new ServiceHost(typeof(CalculatorService),
    new Uri("http://localhost:8887/CalculatorService")))
{
    calculatorServiceHost.Opened += delegate
    {
        Console.WriteLine("Service begin to listen via the Address:{0}",
            calculatorServiceHost.BaseAddresses[0].ToString());
    };

    AddressHeader header =
        AddressHeader.CreateAddressHeader("basic",
        "http://www.cnblogs.com/terrylee", "Terrylee");

    EndpointAddress ea = new EndpointAddress(
        new Uri("http://localhost:8887/CalculatorService"), header);

    calculatorServiceHost.Description.Endpoints.Add(
        new ServiceEndpoint(
            ContractDescription.GetContract(typeof(ICalculator)),
            new WSHttpBinding(),
            ea));

    ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
    behavior.HttpGetEnabled = true;
    calculatorServiceHost.Description.Behaviors.Add(behavior);

    calculatorServiceHost.Open();
    Console.Read();
}

我们在WSDL中可以看到该自定义的报头,它作为终结点引用的引用参数:

<wsdl:service name="CalculatorService">
  <wsdl:port name="WSHttpBinding_ICalculator" binding="tns:WSHttpBinding_ICalculator">
    <soap12:address location="http://localhost:8887/CalculatorService" />
    <wsa10:EndpointReference>
      <wsa10:Address>http://localhost:8887/CalculatorService</wsa10:Address>
      <wsa10:ReferenceParameters>
        <basic xmlns="http://www.cnblogs.com/terrylee">Terrylee</basic>
      </wsa10:ReferenceParameters>
      <Identity xmlns="http://schemas.xmlsoap.org/ws/2006/02/addressingidentity">
        <Upn>TerryLee-PC\TerryLee</Upn>
      </Identity>
    </wsa10:EndpointReference>
  </wsdl:port>
</wsdl:service>

截获到SOAP消息可以看到,在消息报头中添加了basic这样的信息,如下代码所示:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <basic xmlns="http://www.cnblogs.com/terrylee">Terrylee</basic>
    <To s:mustUnderstand="1">http://localhost:8887/CalculatorService</To>
    <Action s:mustUnderstand="1">http://tempuri.org/ICalculator/Add</Action>
  </s:Header>
  <s:Body>
    <Add xmlns="http://tempuri.org/">
      <x>1</x>
      <y>2</y>
    </Add>
  </s:Body>
</s:Envelope>

当然我们也可以通过配置的方式来自定义寻址报头,如下代码所示:

<service name="TerryLee.WCFAddressing.Service.CalculatorService"
         behaviorConfiguration="calculatorBehavior">
  <host>
    <baseAddresses>
      <add baseAddress="http://localhost:8887/Calculator"/>
    </baseAddresses>
  </host>
  <endpoint address=""
            binding ="wsHttpBinding"
            contract="TerryLee.WCFAddressing.Contract.ICalculator">
    <headers>
      <basic xmlns="http://www.cnblogs.com/terrylee">Terrylee</basic>
    </headers>
  </endpoint>
</service>

结束语

本文相对于WCF专题系列(1):深入WCF寻址Part1来说,注重于实际的使用,介绍了指定终结点地址、元数据中的终结点地址、自定义消寻址报头等,在下一篇中,我们将继续深入WCF寻址,探讨消息筛选器等问题。

[第1页][第2页]
0
0
标签:WCF 寻址

热门文章

    最新文章

      最新新闻

        热门新闻