wcf address设置

Address的组成?

Address在WCF中的用System.ServiceModel.EndpointAddress对象来表示的,它的结构如下:

 

                                                                                                                                                                                   
           

组成部分

           
           

作用

           
           

Uri

           
           

指示EndPoint的地址,是必须的

           
           

Identity

           
           

能保证地址的唯一性,当Uri一致的时候,可以用Identity来区分EndPoint,可有可无

           
           

Headers

           
           

为地址提供了一些附加信息,用于Soap Message Filter,最后会添加到Soap消息的Header中

           

此外,Address还包括IsAnonymous属性,用于指示终节点是否能匿名访问。

 

如何在配置文件中指定Address?

在配置文件中,有两种方式可以指定Address,一种是绝对地址方式,另外是相对地址方式,分别如下:

绝对地址

 <host>
          
<baseAddresses>
            
<add baseAddress = "http://localhost:8731/" />
          
</baseAddresses>
   
</host>
   
<endpoint address ="http://localhost:8731/Service" binding="basicHttpBinding" contract="Wcf_Address_Config.IService1"> </endpoint>

 

 

相对地址

 <host>
          
<baseAddresses>
            
<add baseAddress = "http://localhost:8731/" />
          
</baseAddresses>
  
</host>
  
<endpoint address ="Service1" binding="basicHttpBinding" contract="Wcf_Address_Config.IService1"></endpoint>

在网上有人说绝对地址是通过将 httpGetEnabled设置为true,并且设置httpGetUrl为绝对地址是在设置endPoint的说法有些不准确,因为在WCF中有两种 地址,一种是访问endPoint时候用到地址,另外一种是服务描述的地址,服务描述在wcf中被称作serviceMetadata,通过它能够产生代 理类,比如,我们做如下的设置:

 

 <serviceMetadata httpGetEnabled="True" httpGetUrl="http://localhost:8731/Service"/>

只代表在http://localhost:8731/Service/?wsdl上能找到服务描述,可以通过http://localhost:8731/Service/?wsdl生成代理类,而不是将endPoint的地址设置为了http://localhost:8731/Service,这点一定要注意,不要受有些说法的误导。

 

如何通过编程方式设置Address?

除了能在配置中设置Address外 ,还可以通过编程的方式来设置Address,具体方法请参参见下面的代码示例:

 static void Main(string[] args)
        
{
            EndpointAddress address 
= new EndpointAddress("http://127.0.0.1:2136/Service1");           
            Binding binding 
= new BasicHttpBinding();
            wcf.IService1 service 
= new wcf.Service1Client(binding, address);
            Console.WriteLine(service.GetData(
2));
            Console.Read();
        }

上面的代码就是将endPoint的地址设置为了http://127.0.0.1:2136/Service1

 

抄录 源地址http://www.cnblogs.com/jillzhang/archive/2008/01/30/1059169.html

 

posted @ 2014-02-27 23:07  庙小妖风  阅读(477)  评论(0编辑  收藏  举报