2011-04-22 3 views
1

그래서 라우팅 서비스를 작성하려고합니다. 아이디어는 라우팅 서비스를 호출 할 때마다 끝점이 WCF 비헤이비어 확장에 따라 임의로 선택된다는 것입니다. 나는 MSDN에서 다소 수정 된 예제를 사용하여 DynamicReconfiguration을 달성했습니다. 내 Web.config의의 일부는WCF 서비스 비헤이비어 확장에서 null 참조 예외가 발생했습니다.

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이을 던지고 있다는 것이다 행동과 행동의 확장이

<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> 

및 구현처럼 보인다. 그것은 비록 내가 행동에 첨부하더라도이 확장을 찾을 수 없습니다. MSDN의 예에서 라우팅 서비스는 콘솔 응용 프로그램에서 호스팅되며 여기서는 IIS에서 호스트하려고합니다. 여기에 뭔가 빠졌습니다 ...

답변

1

예제 RoutingService 프로젝트에서 routing.cs의 코드는 프로그래밍 방식으로 RoutingExtension을 RoutingService에 삽입합니다. 이는 일반적으로 behaviors> serviceBehaviors> behavior> routing 요소를 사용하여 구성 파일에서 수행하여 사용할 필터 테이블을 설정합니다. 그러나 WCF 서비스는 구성 파일을 통해서만 단일 서비스 동작을 할당받을 수 있으므로 샘플 RoutingService 프로젝트는 서비스 호스트 초기화에 RoutingExtension을 동적으로 추가합니다.

IIS에서 동일한 작업을 수행하려면 사용자 지정 서비스 호스트 팩터 리를 만들어 샘플 프로젝트의 routing.cs 코드와 동일한 기능을 수행해야합니다. 사용자 지정 호스트를 만드는 방법은 Look at this MSDN article을 참조하십시오. 또한 IIS로 구성하는 방법을 보여줍니다.

+0

팁 주셔서 감사합니다. :) – aron