【问题标题】:ASP.NET MVC Application with hosted WCF and Windsor: HttpContext.Current is null具有托管 WCF 和 Windsor 的 ASP.NET MVC 应用程序:HttpContext.Current 为 null
【发布时间】:2012-08-24 07:48:22
【问题描述】:

我已经构建了一个托管 WCF 服务的 ASP.NET MVC 3 应用程序。执行实际工作的服务类驻留在类库中。我正在尝试使用 Windsor WCF 设施根据服务请求进行连接。

这是我的温莎集装箱工厂:

public class WindsorContainerFactory
{
    private static IWindsorContainer container;
    private static readonly object sync = new Object();

    public static IWindsorContainer Current()
    {
        if(container == null) {
            lock (sync)
            {
                if (container == null)
                {
                    container = new WindsorContainer();
                    container.Install(new ControllerInstaller());
                    container.Install(new NHibernateSessionInstaller());
                    container.Install(new RepositoryInstaller());
                    container.Install(new ServiceInstaller());
                }
            }
        }
        return container;
    }
}

在 Global.asax 中,我调用 WindsorContainerFactory.Current() 一次以确保工厂正在建造中:

    protected void Application_Start()
    {
        WindsorContainerFactory.Current();
        ControllerBuilder.Current.SetControllerFactory(typeof(WindsorControllerFactory));
        ...

我通过以下类安装我的服务:

public class ServiceInstaller : IWindsorInstaller
{
    public void Install(Castle.Windsor.IWindsorContainer container, Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store)
    {
        container.Kernel.AddFacility<WcfFacility>();
        container.Kernel.Register(
            Component
            .For<ICustomerService>()
            .ImplementedBy<CustomerService>()
            .Named("CustomerService"));
    }
}

然后我在项目中添加了一个新的 WCF 服务,删除了代码隐藏文件并修改了 svc 标记如下:

<%@ ServiceHost Language="C#" Debug="true"    
Factory="Castle.Facilities.WcfIntegration.DefaultServiceHostFactory, 
Castle.Facilities.WcfIntegration" Service="CustomerService" %>

如您所见,windsor 中还安装了其他组件(存储库、控制器等),但这似乎运行良好。

我的问题是,WCF 客户端(控制台应用程序)收到错误: HttpContext.Current 为空。 PerWebRequestLifestyle 只能在 ASP.Net 中使用

客户端代码:

        CustomerServiceClient c = new Services.Customers.CustomerServiceClient();
        CustomerResponse resp = c.GetCustomerBySurname("Lolman");
        c.Close();            

谁能指出我正确的方向? 提前谢谢!

【问题讨论】:

    标签: wcf castle-windsor


    【解决方案1】:

    尝试启用 AspNetCompatibility。 检查此链接以获取help

    【讨论】:

    • 太好了,就是这样,谢谢。我会投票给你,但我目前不允许
    • 我们将 WCF 服务托管为 Windows 服务。当我启用 AspNetCompatibility 时,出现以下异常。 “此服务需要 ASP.NET 兼容性,并且必须托管在 IIS 中。”在这种情况下,您有什么建议?在此先感谢...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-11
    • 1970-01-01
    • 2014-09-02
    • 2015-01-29
    • 1970-01-01
    • 2014-11-02
    • 2023-03-09
    相关资源
    最近更新 更多