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

问题描述

我喜欢的WCF 4.0 capabality通过在配置文件中设置一个serviceActivations托管服务没有.svc文件。它工作的伟大使用默认的服务工厂,但现在我想用AutofaServiceHostFactory做相同的,所以我的依赖将被正确注射。在我尝试了所有的场景,当我尝试进入服务我仍然有此错误:服务WCFAutofacWiring.MyService配置为WCF不与Autofac容器注册。我主持我的服务在一个空的asp.net网站。下面是我所做的:

I like the WCF 4.0 capabality to host a service without an .svc file by setting a serviceActivations in the config file. It work great using the default service factory but now I'm trying to do the same using the AutofaServiceHostFactory so my dependencies will be properly injected. In all scenarios I tried, I still got this error when I try to access to service : The service 'WCFAutofacWiring.MyService' configured for WCF is not registered with the Autofac container. I host my service in an empty asp.net web site. Here's what I did :

Web.config文件:

Web.config :

<serviceHostingEnvironment>
  <serviceActivations>
    <add factory="Autofac.Integration.Wcf.AutofacServiceHostFactory"
     relativeAddress="~/WASCurrentTime.svc"
     service="WCFAutofacWiring.MyService" />
  </serviceActivations>
</serviceHostingEnvironment>



然后,我把文件放在App_Code文件夹注册我的依赖作为Autofac文档中所述( ),我证实了调试器该代码被称为在提供服务启动:

Then, I put a file in the app_code folder to register my dependency as stated in the Autofac documentation (Autofac WcfIntegration) and I confirmed with the debugger that the code is called at service start :

public static class AppStart
{
    public static void AppInitialize()
    {
        AutofacHostFactory.Container = new AutofacBootstrapper().Configure();
    }
}



最后,这里是我注册的:

Finally, here's my registrations :

public class AutofacBootstrapper
{
    public IContainer Configure()
    {
        var builder = new ContainerBuilder();

        // register types
        builder.Register<ILanguageProvider>(x => new MyLanguageProvider("en-US"));
        builder.RegisterType<MyService>();

        return builder.Build();
    }
}



我的服务,完美的作品,如果使用.svc文件的serviceActivation而是我觉得,如果唯一的原因就是设置一个工厂,我可以在我的web.config指定它浪费创建一个.svc文件。

My service works perfectly if a use an .svc file instead of serviceActivation but I find it wasteful to create an .svc file if the only reason is to setup a factory that I can specify in my web.config.

任何想法?

感谢

推荐答案

据该的,如果任一键入抛出异常与给定的字符串(从配置)或分辨率使用类型分辨率,在给定的字符串失败。

According to the Autofac source, the exception is thrown if either a keyed resolution with the given string (from config) or a typed resolution using Type.GetType for the given string fails.

在你的情况 Type.GetType 方法可能返回因为你没有指定的在config。

In your case the Type.GetType method probably returns null because you haven't specified a Type.AssemblyQualifiedName in the config.

尝试获得 typeof运算(为MyService).AssemblyQualifiedName 并插入到配置。

Try getting typeof(MyService).AssemblyQualifiedName and insert that into the config.

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

09-01 21:47