简体   繁体   English

WCF服务行为扩展抛出空引用异常

[英]WCF service behavior extension throwing null reference exception

So, im trying to write routing service. 因此,即时通讯试图编写路由服务。 The idea is that, every time someone calls routing service, endpoint is randomly selected by WCF behavior extension. 这个想法是,每次有人呼叫路由服务时,WCF行为扩展都会随机选择端点。 I used slightly modified example from MSDN called DynamicReconfiguration to achieve that. 我使用了来自MSDN的经过稍微修改的示例,称为DynamicReconfiguration,以实现该目标。 Part of my web.config looks like this 我的web.config的一部分看起来像这样

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

and implementation of behavior and behavior extension 和行为扩展与实现

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();
    }
}

Problem is that last line of UpdateRules method is throwing NullReferenceException . 问题是UpdateRules方法的最后一行抛出NullReferenceException It cant find this extension even though i attach it in behavior. 即使我将其附加为行为,也无法找到此扩展名。 In example from MSDN, routing service is hosted in console application and here im trying to host it on IIS. 在MSDN的示例中,路由服务托管在控制台应用程序中,在这里我试图将其托管在IIS上。 Im missing something here... 我在这里错过了一些东西...

In the sample RoutingService project, the code in the routing.cs programmatically injects the RoutingExtension into the RoutingService. 在示例RoutingService项目中,routing.cs中的代码以编程方式将RoutingExtension注入到RoutingService中。 This is normally done in the config file using the behaviors>serviceBehaviors>behavior>routing element to set up the filter table to use. 通常这是在配置文件中使用behaviors> serviceBehaviors> behavior> routing元素设置要使用的过滤器表来完成的。 However, since a WCF service can only be assigned a single service behavior through the config file, the sample RoutingService project dynamically adds the RoutingExtension in the service host initialization. 但是,由于只能通过配置文件为WCF服务分配单个服务行为,因此示例RoutingService项目将在服务主机初始化中动态添加RoutingExtension。

To do the same thing in IIS, you need to create a custom service host factory to perform the same function of the routing.cs code in the sample project. 若要在IIS中执行相同的操作,您需要创建一个自定义服务主机工厂,以执行示例项目中的routing.cs代码的相同功能。 Look at this MSDN article for how to create a custom host. 查看此MSDN文章 ,了解如何创建自定义主机。 It also shows how you would configure it with IIS. 它还显示了如何使用IIS配置它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM