将Autofac与启用AJAX的WCF服务一起使用 [英] Using Autofac with AJAX-enabled WCF Service

查看:88
本文介绍了将Autofac与启用AJAX的WCF服务一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正拼命地使它工作.以前,我一直使用WCF服务通过JQuery进行AJAX.

I am desperately trying to get this to work. I have used WCF services to do AJAX with JQuery before without any hustle.

现在我正在使用Autofac进行DI,到目前为止,它运行的非常好,直到我希望UI项目中的WCF服务成为AJAX的基础.在我的常规Web应用程序中,我正在使用Global.asax'Application_Start'注册我的容器.这是通过在我的"MyBootstrapper"项目中调用Boot()方法来完成的. Boot()方法只注册我所有的模块.

Now I am using Autofac for DI, and so far it is going quiet good, until I want a WCF service in my UI project to be the base for AJAX. In my regular Webapplication I am using Global.asax 'Application_Start' to register my containers. This is done by calling a Boot() method in my 'MyBootstrapper' project. The Boot() method simply registers all my modules.

现在我注意到拥有WCF服务并使用启用了AJAX的WCF服务"模板,一旦调用该服务,就不会调用Global.asax.

Now I noticed that having a WCF Service and by using the 'AJAX-enabled WCF Service' template, the Global.asax is not called once the service is invoked.

为此,我制作了一个简单的MyServiceHostFactory,它带有一个void host_Opening方法,该方法在Bootstrapper中调用Boot()方法.这似乎进展顺利.虽然什么都没设置.

For this I have made a simple MyServiceHostFactory with a void host_Opening method that calls my Boot() method in my Bootstrapper. This seems to go well. Although nothing is set up.

这是我的WCF代码:

    [ServiceContract(Namespace = "MyWebApp.UI.Ajax")]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class MyAsyncService
    {
        public IPersonProvider PersonManager { get; set; }

        [OperationContract]
        [WebGet(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        public List<Person> GetPersons()
        {
            var persons = PersonManager.GetPersons();
            return persons;
        }
    }

MyAsyncService的标记

Markup of MyAsyncService

<%@ ServiceHost Language="C#" Debug="true" Service="MyWebApp.UI.Ajax.MyAsyncService" CodeBehind="MyAsyncService.svc.cs" Factory="MyWebApp.UI.Ajax.MyAsyncServiceHostFactory"%>

MyAsyncServiceHostFactory:

MyAsyncServiceHostFactory:

public class MyAsyncServiceHostFactory : ServiceHostFactory, IContainerProviderAccessor
    {
        static IContainerProvider _containerProvider;

        public IContainerProvider ContainerProvider
        {
            get { return _containerProvider; }
        }

        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            var host = base.CreateServiceHost(serviceType, baseAddresses);
            host.Opening += host_Opening;
            return host;
        }

        void host_Opening(object sender, EventArgs e)
        {
            // Setup Autofac stuff.
            var container = MyBootLoader.Boot();
            _containerProvider = new ContainerProvider(container);
        }
    }

和MyBootLoader:

And MyBootLoader:

 public static class MyBootLoader
    {
        public static IContainer Boot()
        {
            var builder = ConfigureContainer();
            return builder.Build();
        }

        private static ContainerBuilder ConfigureContainer()
        {
            var builder = new ContainerBuilder();
            builder.RegisterType<PersonManager>().As<IPersonProvider>().InstancePerHttpRequest();
            return builder;
        }
    }

我的web.config:

My web.config:

  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="MyWebApp.UI.Ajax.MyAsyncServiceAspNetAjaxBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <services>
      <service name="MyWebApp.UI.Ajax.MyAsyncService">
        <endpoint address="" behaviorConfiguration="MyWebApp.UI.Ajax.MyAsyncServiceAspNetAjaxBehavior"
          binding="webHttpBinding" contract="MyWebApp.UI.Ajax.MyAsyncService" />
      </service>
    </services>
  </system.serviceModel>

请问我想念什么? 一切似乎都能解决,但MyAsyncService中的PersonManager为NULL.

What am I missing, please? Everything seems to get through, but my PersonManager is NULL in MyAsyncService.

推荐答案

您是否已按照Wiki上有关IIS托管服务的说明进行操作?

Have you followed the instruction on the wiki for IIS hosting services?

https://code.google.com/p/autofac/wiki/WcfIntegration#IIS_Hosted_Services

通常,您在Application_Start中配置容器,并使用新建的容器设置AutofacHostFactory.Container属性.然后将.svc文件中的工厂设置为AutofacWebServiceHostFactory.如果出于某种原因需要自定义工厂,则应从现有的Autofac工厂实现中获得一种.这些将行为添加到负责依赖项注入的端点.

You normally configure your container in Application_Start and set the AutofacHostFactory.Container property with the newly built container. Then set the factory in your .svc file to AutofacWebServiceHostFactory. If you need to customise the factory for some reason you should derive from one of the existing Autofac factory implementations. These add the behavior to the endpoint that is responsible for the dependency injection.

这篇关于将Autofac与启用AJAX的WCF服务一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆