[英]How to set WCF base addresses in code when using the Configure method
For pure code-based WCF configuration, in .NET 4.0 one could configure a base address for a service when creating an instance of a ServiceHost (assuming a self-hosted scenario, which is suitable for my needs). 对于基于代码的纯WCF配置,在.NET 4.0中,可以在创建ServiceHost实例时为服务配置基址(假设自托管方案适合我的需要)。 While one could still do this with .NET 4.5, MSDN implies that using the new
Configure()
method is a better practice because it makes such code-based configuration a bit easier for self-hosted and a lot easier for web hosted scenarios (see Configuring WCF Services in Code ). 虽然人们仍然可以使用.NET 4.5执行此操作,但MSDN意味着使用新的
Configure()
方法是一种更好的做法,因为它使基于代码的配置对于自托管更容易,并且对于Web托管方案更容易(参见在代码中配置WCF服务 )。
The problem, however, is that though the object you have to work with inside the Configure method (a System.ServiceModel.ServiceConfiguration) has a BaseAddresses
property it is read-only. 但问题是,虽然您必须在Configure方法(System.ServiceModel.ServiceConfiguration)中使用的对象具有
BaseAddresses
属性,但它是只读的。 Surely there must be a way to set the base addresses in code? 当然必须有一种方法来设置代码中的基地址? My web searches have turned up not only nothing about this specific question, but no articles or posts about this Configure method at all, except for the single MSDN page I have referenced!
我的网络搜索不仅没有提到这个特定的问题,而且根本没有关于这个Configure方法的文章或帖子,除了我引用的单个MSDN页面!
Looking in reflector what BaseAddresses
property does: 查看反射器
BaseAddresses
属性的作用:
// System.ServiceModel.ServiceConfiguration
public ReadOnlyCollection<Uri> BaseAddresses
{
get
{
return this.host.BaseAddresses;
}
}
I was unable to find any other method in ServiceConfiguration working with ServiceHost.BaseAddresses 我无法在ServiceConfiguration中找到使用ServiceHost.BaseAddresses的任何其他方法
Instance of ServiceConfiguration
is created using ServiceHost
instance: ServiceConfiguration
实例是使用ServiceHost
实例创建的:
// System.ServiceModel.ServiceConfiguration
internal ServiceConfiguration(ServiceHost host)
{
ServiceConfiguration.CheckArgument<ServiceHost>(host, "host");
this.host = host;
}
This means that ServiceConfiguration
is just an extension to standard way of configuring ServiceHost
. 这意味着
ServiceConfiguration
只是配置ServiceHost
标准方式的扩展。 So I would suggest any old way of setting BaseAddress
is still considered as the best practice (programmatic or using configuration). 所以我建议任何旧的设置
BaseAddress
方法仍然被认为是最佳实践(程序化或使用配置)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.