简体   繁体   English

使用Configure方法时如何在代码中设置WCF基址

[英]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.

相关问题 在代码中覆盖配置文件WCF Base Addresses - Overriding config file WCF Base Addresses in code 使用“base”关键字时,基类方法中的代码不执行 - Code in base class method not executing when using “base” keyword 如何在不使用基类的情况下封装WCF服务的代码? - How to encapsulate code for WCF services without using a base class? WCF中的多个基址和多个端点 - Multiple Base Addresses and Multiple Endpoints in WCF 在WCF服务协定中公开多个方法时如何配置NServiceBus端点 - How to configure NServiceBus endpoints when exposing more than one method in WCF service contract 如何在代码中使用wsDualHttpBinding设置WCF客户端? - How to set up a WCF client using wsDualHttpBinding in code? 使用ClientBase调用WCF服务时如何设置EndpointBehaviors名称 - How to set endpointBehaviors name when calling wcf service using ClientBase 在 .NET 5 隔离进程中使用 Azure Functions v3 时,如何添加以前在 Startup.cs 中的代码 --&gt; 配置方法 - How do I add code that was previously in Startup.cs -->Configure method when using Azure Functions v3 in .NET 5 isolated process 使用X509证书时如何配置WCF客户端绑定? - How to configure WCF client binding when using X509 certificate? 使用TPC时,实体框架代码如何首先从基类集中找到类型化的实体 - How Does Entity Framework Code First Find Typed Entities From A Base-Class Set When Using TPC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM