首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用一个WcfFacility托管多个服务

如何使用一个WcfFacility托管多个服务
EN

Stack Overflow用户
提问于 2012-03-16 23:01:53
回答 1查看 2.2K关注 0票数 3

我尝试使用一个WcfFacility和IIS托管多个服务,但我看到了一些令人困惑的结果。

下面是我的配置:

代码语言:javascript
复制
        var baseUri = new Uri(HttpContext.Current.Request.Url.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped));

        container.AddFacility<WcfFacility>(f => { f.CloseTimeout = TimeSpan.Zero; }).Register(
            Component.For<IAttributeService>()                  
                .ImplementedBy<AttributeService>()
                .AsWcfService(
                    new DefaultServiceModel()
                        .Hosted()                           
                        .AddEndpoints(
                            WcfEndpoint.ForContract<IAttributeService>().BoundTo(new BasicHttpBinding()).At("Soap11"),
                            WcfEndpoint.ForContract<IAttributeService>().BoundTo(new WSHttpBinding()).At("Soap12")
                        )
                .AddBaseAddresses(new Uri(baseUri, "AttributeService.svc"))                 
                ),                      
            Component.For<ISessionService>()
                .ImplementedBy<SessionService>()
                .AsWcfService(
                    new DefaultServiceModel()
                        .Hosted()                           
                        .AddEndpoints(
                            WcfEndpoint.ForContract<ISessionService>().BoundTo(new BasicHttpBinding()).At("Soap11"),
                            WcfEndpoint.ForContract<ISessionService>().BoundTo(new WSHttpBinding()).At("Soap12")
                        )
                .AddBaseAddresses(new Uri(baseUri, "SessionService.svc"))
                ),          
            Component.For<ISampleService>()
                .ImplementedBy<SampleService>()
                .AsWcfService(
                    new DefaultServiceModel()
                        .Hosted()
                        .AddEndpoints(
                            WcfEndpoint.ForContract<ISampleService>().BoundTo(new BasicHttpBinding()).At("Soap11"),
                            WcfEndpoint.ForContract<ISampleService>().BoundTo(new WSHttpBinding()).At("Soap12")
                        )
                .AddBaseAddresses(new Uri(baseUri, "SampleService.svc"))
                )
        );

当我使用WCF Test client来检查这一点时,似乎每个服务下可用的方法都是该服务和我之前mex‘’ed的所有服务的组合。示例:

我做错了吗?您不能多次添加WcfFacility,并在internets上四处查看,我似乎找不到有人在一个设施中托管多个服务的示例。

有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-16 23:31:02

我已经弄明白了。之前,我使用以下代码在MetaData上启用HttpGet:

代码语言:javascript
复制
var metadata = new ServiceMetadataBehavior { HttpGetEnabled = true };
container.Register(Component.For<IServiceBehavior>().Instance(metadata));

它遵循this github example中的代码。

似乎这种方法会导致WcfFacility在任何get请求上共享所有服务的MetaData。

解决方案很简单。首先,去掉那些东西。其次,以这种方式配置每个服务组件

代码语言:javascript
复制
Component.For<IAttributeService>()                  
.ImplementedBy<AttributeService>()
.AsWcfService(
    new DefaultServiceModel()
        .Hosted()
        .PublishMetadata(x => x.EnableHttpGet())
        .AddBaseAddresses(new Uri(baseUri, "AttributeService.svc"))             
        .AddEndpoints(
            WcfEndpoint.ForContract<IAttributeService>().BoundTo(new BasicHttpBinding()).At("Soap11"),
            WcfEndpoint.ForContract<IAttributeService>().BoundTo(new WSHttpBinding()).At("Soap12")
        )                               
),

具体地说,诀窍是在每个组件中添加此代码.PublishMetadata(x => x.EnableHttpGet())

现在,我看到了每个服务的预期行为。

编辑:一旦我让它工作了,我就开始删除可能需要也可能不需要的东西--我喜欢约定而不是配置。这就是结果,似乎没有任何其他东西可以去掉。这样做的好处是,我可以进一步重构为所有服务的通用注册,而不是每个服务都需要一个注册。只是分享商品而已。

代码语言:javascript
复制
        Component.For<IAttributeService>()
            .ImplementedBy<AttributeService>()
            .AsWcfService(
                new DefaultServiceModel()
                    .Hosted()
                    .PublishMetadata(x => x.EnableHttpGet())
                    .AddEndpoints(
                        WcfEndpoint.BoundTo(new BasicHttpBinding()).At("Soap11"),
                        WcfEndpoint.BoundTo(new WSHttpBinding()).At("Soap12")
                    )
        ),

这是通用注册。

代码语言:javascript
复制
Classes.FromThisAssembly()
.Where(t => Attribute.IsDefined(t, typeof(StandardServiceAttribute)))
.WithService.Select((t, _) => t.GetInterfaces().Where(i => Attribute.IsDefined(i, typeof(ServiceContractAttribute),false)))
.Configure
(cr => 
    cr.AsWcfService(
        new DefaultServiceModel()
            .Hosted()
            .PublishMetadata(x => x.EnableHttpGet())
            .AddEndpoints(
                WcfEndpoint.BoundTo(new BasicHttpBinding()).At("Soap11"),
                WcfEndpoint.BoundTo(new WSHttpBinding()).At("Soap12")                       
            )
        )
)
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9739668

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档