我尝试使用一个WcfFacility和IIS托管多个服务,但我看到了一些令人困惑的结果。
下面是我的配置:
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上四处查看,我似乎找不到有人在一个设施中托管多个服务的示例。
有什么想法吗?
发布于 2012-03-16 23:31:02
我已经弄明白了。之前,我使用以下代码在MetaData上启用HttpGet:
var metadata = new ServiceMetadataBehavior { HttpGetEnabled = true };
container.Register(Component.For<IServiceBehavior>().Instance(metadata));它遵循this github example中的代码。
似乎这种方法会导致WcfFacility在任何get请求上共享所有服务的MetaData。
解决方案很简单。首先,去掉那些东西。其次,以这种方式配置每个服务组件
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())。
现在,我看到了每个服务的预期行为。

编辑:一旦我让它工作了,我就开始删除可能需要也可能不需要的东西--我喜欢约定而不是配置。这就是结果,似乎没有任何其他东西可以去掉。这样做的好处是,我可以进一步重构为所有服务的通用注册,而不是每个服务都需要一个注册。只是分享商品而已。
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")
)
),这是通用注册。
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")
)
)
)https://stackoverflow.com/questions/9739668
复制相似问题