配置服务终结点

一.终结点

每个终结点包含一个指示可在何处找到此终结点的地址、一个指定客户端如何与此终结点进行通信的绑定和一个标识可用方法的协定。 

(1).在后台代码添加终结点

代码
 ServiceHost host = new ServiceHost(typeof(Fun), new Uri ("http://localhost:8285/Service"));
            
/*
             * 添加终结点
             
*/
            host.AddServiceEndpoint(
typeof(IFun), new WSHttpBinding(),"");
代码
 // 摘要:
        
//     使用指定的协定、绑定和终结点地址将服务终结点添加到承载服务中。
        
//
        
// 参数:
        
//   implementedContract:
        
//     所添加终结点的协定的 System.Type。
        
//
        
//   binding:
        
//     所添加终结点的 System.ServiceModel.Channels.Binding。
        
//
        
//   address:
        
//     所添加终结点的地址。
        
//
        
// 返回结果:
        
//     添加到承载服务中的 System.ServiceModel.Description.ServiceEndpoint。
        
//
        
// 异常:
        
//   System.ArgumentNullException:
        
//     implementedContract 或 binding 或 address 为 null。
        public ServiceEndpoint AddServiceEndpoint(Type implementedContract, Binding binding, string address);

(2).配置文件

代码
<configuration>
  
<system.serviceModel>
    
<services>
      
<service name="UE.Samples.HelloService"
               behaviorConfiguration
="HelloServiceBehavior">
        
<endpoint address="/Address1"
                  binding
="basicHttpBinding" 
                  contract
="UE.Samples.IHello"/>

        
<endpoint address="mex"
                  binding
="mexHttpBinding"
                  contract
="IMetadataExchange" />
      
</service>
    
</services>
    
<behaviors>
      
<serviceBehaviors>
        
<behavior name="HelloServiceBehavior">
          
<serviceMetadata httpGetEnabled="true" />
        
</behavior>
      
</serviceBehaviors>
    
</behaviors>
  
</system.serviceModel>
</configuration>

二.终结点地址的定义
例如,“http://www.fabrikam.com:322/mathservice.svc/secureEndpoint”这个 URI 具有以下四个部分:
方案:http
计算机:www.fabrikam.com
端口:322
路径/mathservice.svc/SecureEndpoint

三.消息标头

如何在客户端设置当前上下文中的消息标头

代码
OperationContextScope scope = new OperationContextScope(wcfClient.InnerChannel)

 MessageHeader header
      
= MessageHeader.CreateHeader(
      
"Service-Bound-CustomHeader",
      
"http://Microsoft.WCF.Documentation",
      
"Custom Happy Value."
      );
    OperationContext.Current.OutgoingMessageHeaders.Add(header);
原文地址:https://www.cnblogs.com/tongly/p/1854116.html