2011-04-22 87 views
1

所以,即時嘗試編寫路由服務。其思想是,每次有人呼叫路由服務時,端點都是通過WCF行爲擴展隨機選擇的。我使用來自MSDN的稍微修改過的例子DynamicReconfiguration來實現這一點。我的web.config的部分看起來像這樣WCF服務行爲擴展拋出空引用異常

<behaviors> 
    <serviceBehaviors> 
    <behavior> 
    <behavior name="behaviorWithUpdate"> 
     <updateBehavior /> 
    </behavior> 
    </serviceBehaviors> 
</behaviors> 
<extensions> 
    <behaviorExtensions> 
    <add name="updateBehavior" type="RouterService.UpdateBehavior, RouterService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/> 
    </behaviorExtensions> 
</extensions> 
<services> 

和實施的行爲和行爲擴展的

public class UpdateBehavior : BehaviorExtensionElement, IServiceBehavior 
{ 
    void IServiceBehavior.AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) 
    { 
    } 
    void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) 
    { 
     var rulesUpdateExtension = new RulesUpdateExtension(); 
     serviceHostBase.Extensions.Add(rulesUpdateExtension); 
    } 
    void IServiceBehavior.Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) 
    { 
    } 
    class RulesUpdateExtension : IExtension<ServiceHostBase> 
    { 
     ServiceHostBase _owner; 
     List<EndpointAddress> _endpoints = new List<EndpointAddress> 
                 { 
                  new EndpointAddress("http://localhost:19338/Service1.svc"), 
                  new EndpointAddress("http://localhost:20464/Service2.svc") 
                 }; 

     void IExtension<ServiceHostBase>.Attach(ServiceHostBase owner) 
     { 
      this._owner = owner; 

      UpdateRules(DateTime.Now.Second % 2 == 0 ? _endpoints[0] : _endpoints[1]); 
     } 

     void IExtension<ServiceHostBase>.Detach(ServiceHostBase owner) 
     { 
     } 

     void UpdateRules(EndpointAddress endpoint) 
     { 
      var rc = new RoutingConfiguration(); 

      var serviceEndpoint = new ServiceEndpoint(
        ContractDescription.GetContract(typeof(IService1)), 
        new BasicHttpBinding(), 
        endpoint); 
      rc.FilterTable.Add(new MatchAllMessageFilter(), new List<ServiceEndpoint> { serviceEndpoint }); 

      this._owner.Extensions.Find<RoutingExtension>().ApplyConfiguration(rc); 
     } 
    } 

    public override Type BehaviorType 
    { 
     get { return typeof(UpdateBehavior); } 
    } 

    protected override object CreateBehavior() 
    { 
     return new UpdateBehavior(); 
    } 
} 

問題是,UpdateRules最後一行方法拋出的NullReferenceException。它不能找到這個擴展,即使我把它附加在行爲。在來自MSDN的示例中,路由服務託管在控制檯應用程序中,並在此嘗試將其託管在IIS上。我在這裏丟失了一些東西...

回答

1

在示例RoutingService項目中,routing.cs中的代碼以編程方式將RoutingExtension注入到RoutingService中。這通常在配置文件中使用行爲> serviceBehaviors> behavior> routing元素來設置要使用的過濾器表。但是,由於WCF服務只能通過配置文件分配到單個服務行爲,因此示例RoutingService項目會在服務主機初始化中動態添加RoutingExtension。

要在IIS中做同樣的事情,您需要創建一個自定義服務主機工廠來執行示例項目中routing.cs代碼的相同功能。 Look at this MSDN article瞭解如何創建自定義主機。它還顯示瞭如何使用IIS進行配置。

+0

感謝提示:) – aron 2011-04-26 13:57:36