【问题标题】:Is it possible to configure a WCF service using castle windsor fluent configuration without config or svc files?是否可以使用没有配置或 svc 文件的城堡 Windsor fluent 配置来配置 WCF 服务?
【发布时间】:2012-01-11 15:34:54
【问题描述】:

我有一个托管在 IIS 上的 ASP .Net MVC 3.0 Web 应用程序,我使用的是 Castle Windsor 3.0 版。

我想做的是使用 webHttpBinding 注册 WCF 服务,而 web.config 中没有任何条目或具有 .svc 文件。这可能吗?

我在我的 IWindsorInstaller 实现中尝试了这个:

container.AddFacility<WcfFacility>(f => f.CloseTimeout = TimeSpan.Zero);

container.Register(
    Component
        .For<IMyService>()
        .ImplementedBy<MyService>()
        .AsWcfService(new DefaultServiceModel()
                          .AddBaseAddresses("http://localhost/WebApp/Services")
                          .AddEndpoints(WcfEndpoint
                                    .BoundTo(new WebHttpBinding())
                                    .At("MyService.serv"))
                          .Hosted()
                          .PublishMetadata()));

我忽略了在全局 asax 的 RegisterRoutes 方法中完成 serv 的任何内容:

routes.IgnoreRoute("{resource}.serv/{*pathInfo}");

如果我将浏览器指向 http://localhost/WebApp/Services/MyService.serv,我会得到 404。

我做错了什么或者我想做一些愚蠢的事情(或不可能或两者兼而有之!)?

【问题讨论】:

  • 只有当您使用特殊的ServiceRoute 为您的服务注册 URL 路由时才有可能,但我不知道它如何与 Castle Windsor 一起使用。
  • 感谢 Ladislav,这给了我一个开始寻找的好地方!

标签: c# asp.net wcf castle-windsor


【解决方案1】:

感谢 Ladislav 关于使用 ServiceRoute 的建议,我已经想出了如何做到这一点,但我不确定它是否理想,这就是我所做的(以防 Google 员工找到它并可以改进它等):

像这样在 ComponentRegistration 上创建了一个扩展方法:

public static ComponentRegistration<T> AddServiceRoute<T>(
    this ComponentRegistration<T> registration, 
    string routePrefix, 
    ServiceHostFactoryBase serviceHostFactory, 
    string routeName) where T : class
{
    var route = new ServiceRoute("Services/" + routePrefix + ".svc",
                                 serviceHostFactory,
                                 registration
                                     .Implementation
                                     .GetInterfaces()
                                     .Single());
    RouteTable.Routes.Add(routeName, route);           
    return registration;
}   

这样做是添加一个服务路由,将服务放置在 Services 文件夹下并添加 .svc 扩展名(我可能会删除它)。请注意,我假设该服务仅实现一个接口,但在我的情况下这很好,我认为无论如何都是很好的做法。

我不确定这是拥有此扩展方法的最佳位置,或者即使实际上根本需要扩展方法 - 也许我应该与服务主机构建器或其他东西一起做,我不知道!

然后在 MapRoute 调用中,我确保根据问题 MVC2 Routing with WCF ServiceRoute: Html.ActionLink rendering incorrect links! 将其添加到约束参数中

new { controller = @"^(?!Services).*" }

这只会使启动服务的任何内容都无法匹配为控制器。我不太喜欢这个,因为我必须将它添加到我的所有路由中 - 我宁愿将服务文件夹全局放入服务解析器或其他东西(我似乎对 MVC 了解不够!)。

最后在我的 Windsor 安装程序中,我像这样注册服务:

container.AddFacility<WcfFacility>(
    f =>
        {
            f.Services.AspNetCompatibility = 
                AspNetCompatibilityRequirementsMode.Allowed;
            f.CloseTimeout = TimeSpan.Zero;
        });

container.Register(
    Component
        .For<IMyService>()
        .ImplementedBy<MyService>()
        .AsWcfService(new DefaultServiceModel()
                         .AddEndpoints(WcfEndpoint.BoundTo(new WebHttpBinding()))
                         .Hosted()
                         .PublishMetadata())
        .AddServiceRoute("MyService", new DefaultServiceHostFactory(), null));

之后我可以浏览到服务,它被拾取并构建得很好!

正如我所暗示的,这可能不是最好的方法,但它确实有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-08
    • 1970-01-01
    • 1970-01-01
    • 2011-08-31
    • 1970-01-01
    • 1970-01-01
    • 2015-12-13
    相关资源
    最近更新 更多