如何开始使用与 Castle Windsor 2.5.0.1 集成的 WCF

发布于 2024-12-13 06:03:29 字数 208 浏览 1 评论 0原文

我正在尝试将 Castle Windsor 2.5.0.1 与 IIS 上的 WCF 集成(在 XP/windows 2008 上)。但我发现只有 Castle Windsor 3.0.0 可以与 Caste.Facilities.WCFIntegration 配合使用。我简直是绞尽脑汁地想知道如何做 WCF+Castle 2.5.0.1。我用谷歌搜索了很多来找到一个可行的例子,但失败了。请指导我。

I'm trying to integrate Castle Windsor 2.5.0.1 with WCF on IIS (on XP/windows 2008). but I see that only Castle Windsor 3.0.0 works fine with the Caste.Facilities.WCFIntegration. I'm literally pulling my hair out to find out how to do WCF+Castle 2.5.0.1. I googled a lot to find a working example but failed. Please guide me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

大姐,你呐 2024-12-20 06:03:29

你可以这样做:

web.config wcf 配置:

<behaviors>
  <serviceBehaviors>

    <behavior name="defaultBehaviour">

      <createWithIoC />

    </behavior>

  </serviceBehaviors>
</behaviors>

<extensions>
  <behaviorExtensions>
    <add
      name="createWithIoC"
      type="Namespace.CreateWithIocBehaviour, Assembly.Namespace" />
  </behaviorExtensions>
</extensions>


public class CreateWithIocBehaviour : BehaviorExtensionElement
{
    protected override object CreateBehavior()
    {
        return new IocServiceProviderBehaviour();
    }

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

public class IocServiceProviderBehaviour : IServiceBehavior
{
    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
    }

    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        foreach (ChannelDispatcherBase cdb in serviceHostBase.ChannelDispatchers)
        {
            var cd = cdb as ChannelDispatcher;
            if (cd != null)
            {
                foreach (EndpointDispatcher ed in cd.Endpoints)
                {
                    ed.DispatchRuntime.InstanceProvider =
                        new IocServiceProvider(serviceDescription.ServiceType);
                }
            }
        }
    }
}

public class IocServiceProvider : IInstanceProvider
{
    private readonly Type _serviceType;

    public IocServiceProvider(Type serviceType)
    {
        _serviceType = serviceType;
    }

    public object GetInstance(InstanceContext instanceContext)
    {
        return GetInstance(instanceContext, null);
    }

    public object GetInstance(InstanceContext instanceContext, Message message)
    {
        return IoC.Resolve<object>(_serviceType);
    }

    public void ReleaseInstance(InstanceContext instanceContext, object instance)
    {
        IoC.Release(instance);
    }
}

You can do this:

web.config wcf config:

<behaviors>
  <serviceBehaviors>

    <behavior name="defaultBehaviour">

      <createWithIoC />

    </behavior>

  </serviceBehaviors>
</behaviors>

<extensions>
  <behaviorExtensions>
    <add
      name="createWithIoC"
      type="Namespace.CreateWithIocBehaviour, Assembly.Namespace" />
  </behaviorExtensions>
</extensions>


public class CreateWithIocBehaviour : BehaviorExtensionElement
{
    protected override object CreateBehavior()
    {
        return new IocServiceProviderBehaviour();
    }

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

public class IocServiceProviderBehaviour : IServiceBehavior
{
    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
    }

    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        foreach (ChannelDispatcherBase cdb in serviceHostBase.ChannelDispatchers)
        {
            var cd = cdb as ChannelDispatcher;
            if (cd != null)
            {
                foreach (EndpointDispatcher ed in cd.Endpoints)
                {
                    ed.DispatchRuntime.InstanceProvider =
                        new IocServiceProvider(serviceDescription.ServiceType);
                }
            }
        }
    }
}

public class IocServiceProvider : IInstanceProvider
{
    private readonly Type _serviceType;

    public IocServiceProvider(Type serviceType)
    {
        _serviceType = serviceType;
    }

    public object GetInstance(InstanceContext instanceContext)
    {
        return GetInstance(instanceContext, null);
    }

    public object GetInstance(InstanceContext instanceContext, Message message)
    {
        return IoC.Resolve<object>(_serviceType);
    }

    public void ReleaseInstance(InstanceContext instanceContext, object instance)
    {
        IoC.Release(instance);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文