2010-04-06 157 views
1

我在想通過代碼而不是通過配置文件來向端點添加綁定擴展。理想情況下,我希望它僅僅是我放置在服務方法上的一個屬性。通過代碼添加綁定擴展

到目前爲止,似乎唯一沒有公開暴露的是綁定擴展。

回答

0

所以我想通了,我只好先子WebServiceHostFactory:

/// <summary> 
/// RestServiceFactory extends WebServiceHostFactory and adds support for JSONP encoding  
/// </summary> 
public class RestServiceFactory : WebServiceHostFactory 
{   
    /// <summary> 
    /// Creates a ServiceHost using the first address in baseAddresses 
    /// </summary> 
    /// <param name="serviceType"></param> 
    /// <param name="baseAddresses"></param> 
    /// <returns></returns> 
    protected override System.ServiceModel.ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) 
    {    
     Uri[] defaultAddresses = new Uri[1]; 
     defaultAddresses[0] = baseAddresses[0]; 

     ServiceHost host = new RestServiceHost(serviceType, defaultAddresses); 

     // Bind up the JSONP extension    
     CustomBinding cb = new CustomBinding(new WebHttpBinding()); 
     cb.Name = "JSONPBinding"; 

     // Replace the current MessageEncodingBindingElement with the JSONP element 
     var currentEncoder = cb.Elements.Find<MessageEncodingBindingElement>(); 
     if (currentEncoder != default(MessageEncodingBindingElement)) 
     {         
      cb.Elements.Remove(currentEncoder);     
      cb.Elements.Insert(0, new JSONP.JSONPBindingElement());     
     } 

     host.AddServiceEndpoint(serviceType.GetInterfaces()[0], cb, defaultAddresses[0]); 

     return host; 
    }     
} 

然後我不得不繼承WebServiceHost修改它是如何設置的行爲:

/// <summary> 
/// RestServiceHost extends WebServiceHost to add JSONP support 
/// </summary> 
public class RestServiceHost : WebServiceHost 
{ 
    public RestServiceHost() : base() { } 
    public RestServiceHost(Type serviceType, params Uri[] baseAddresses) : base(serviceType,baseAddresses) { } 

    protected override void OnOpening() 
    { 
     base.OnOpening(); 

     if (base.Description != null) 
     {        
      foreach (ServiceEndpoint endpoint in base.Description.Endpoints) 
      { 
       if ((endpoint.Binding != null) && (endpoint.Binding.CreateBindingElements().Find<JSONP.JSONPBindingElement>() != null)) 
       { 
        if (endpoint.Behaviors.Find<WebHttpBehavior>() == null) 
        { 
         endpoint.Behaviors.Add(new WebHttpBehavior()); 
        } 
       } 
      } 
     } 
    } 
} 

此更改的原因是因爲WebServiceHost由於自定義綁定而未添加任何行爲。

完成這兩個更改後,通過正確的綁定擴展傳送請求,不需要更改web.config。

3

如果你給我更多的上下文,我可以使用你的代碼片段重寫。只要讓我知道你的綁定擴展的名字....

如果您需要使用WebServiceHostFactory

public class ServiceHostFactory : WebServiceHostFactory 
    { 

    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) 
    { 
      ServiceHost serviceHost = new ServiceHost(typeof(IService)); 
      CustomBinding customBinding= new CustomBinding(); 
      customBinding.Name = 「myCustomBinding」; 
      customBinding.Elements.Add(new SomeBindingExtensionElement()); 

      serviceHost.AddServiceEndpoint(typeof(IService), customBinding, 
      「http://localhost/service/CustomEndpoint」); 
+0

我應該說明我正在使用WebServiceHostFactory,所以綁定代碼對我來說是不可用的,儘管我可能能夠將其綁定到子類並掛鉤它。 – FlySwat 2010-04-06 02:46:39

+0

我認爲你最簡單的方法是繼承並鉤入它......見上面.. – Nix 2010-04-06 12:07:19

+0

這不適用於WebServiceHost,因爲它自動查找端點並創建它自己的綁定。您不能在CreateServiceHost中創建一個綁定。 – FlySwat 2010-04-06 17:10:42