【问题标题】:Change WCF Service binding to https at runtime在运行时将 WCF 服务绑定更改为 https
【发布时间】:2017-07-07 09:46:20
【问题描述】:

我们有一个部署到许多客户端的应用程序,其中一些使用 http,另一些使用 https。在设置我们的应用程序时,web.config 会自动填充 WCF 端点、绑定等。我们希望在应用程序启动时将绑定更改为 https - 而无需修改 .config 文件。这可能吗?例如我们的 .config 文件看起来像(这是一个 sn-p):

  <service behaviorConfiguration="Figment.Services.Business.ObjectInfo.ObjectInfoServiceBehavior" name="Figment.Services.Business.ObjectInfo.ObjectInfoService">
    <endpoint address="" binding="basicHttpBinding" bindingNamespace="http://Figment.com/webservices/" contract="Figment.Business.Core.ObjectInfo.IObjectInfoService" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  </service>

根据我的阅读,可以同时拥有 http 和 https 绑定,但我们不希望这样 - 如果是 https,则强制它使用 https。

更改应在服务启动时在 c# 中完成,并在服务运行时永久生效。

【问题讨论】:

  • 应用程序是托管服务还是与服务通信?您不想为服务提供两个端点 - http 和 https 吗?请注意,给定的服务实例不能同时有两个绑定 - 它只能有一个。如果服务独立于应用程序,我建议设置两个端点并让各个客户端选择合适的端点。
  • 抱歉没有更清楚。这是一个具有相互通信的客户端和服务的应用程序。当我们在内部开发它时,我们使用 http,因为我们没有证书。我们的一些客户拥有自己的证书,并希望它仅通过 https 进行通信。我可以修改客户端应用程序以使用 https,现在我不明白如何修改服务器以从不使用 http,只使用 https。

标签: .net wcf wcf-binding


【解决方案1】:

我知道这是一个老问题,但我遇到了同样的问题,在 SO 上找不到答案。由于我花了几天时间挖掘解决问题,我认为值得在这里发布。

这里通常有 3 个选项:

1) 按照here,在代码中完全配置您的服务主机(忽略 *.config)

2) 根据here编写脚本来修改 *.config 或使用其他配置

3) 同时使用 *.config 和编程绑定配置

选项 (3) 很棘手,因为您必须在创建服务主机之后对其进行修改,但 在此之前它将是打开的(类似于host.State == Created)。这是因为修改已经打开的主机的绑定没有效果。更多详情here

要获得 (3) 工作,您必须使用自定义主机工厂。示例标记:

<%@ ServiceHost 
    Language="C#" 
    Debug="true" 
    CodeBehind="MyService.svc.cs"
    Service="Namespace.MyService, MyService" 
    Factory="Namespace.MyWcfHostFactory, MyService"
%>

MyWcfHostFactory 应该继承 ServiceHostFactory 并扩展/覆盖 CreateServiceHost 方法。 我正在使用 DI 框架 (Autofac),它稍微简化了一些事情。所以MyWcfHostFactory 只是继承AutofacHostFactory。启动代码(从Global.asaxApplication_StartMyWcfHostFactory的静态构造函数调用):

var builder = new ContainerBuilder();

// Register your service implementations.
builder.RegisterType< Namespace.MyService>();

// Set the dependency resolver.
var container = builder.Build();
AutofacHostFactory.Container = container;
AutofacHostFactory.HostConfigurationAction = (host => PerformHostConfiguration(host));

PerformHostConfiguration 中,您可以覆盖或修改绑定。下面的代码采用第一个注册端点并将其绑定替换为 https 一个:

/// <summary> This is used as a delegate to perform wcf host configuration - set Behaviors, global error handlers, auth, etc </summary>
private static void PerformHostConfiguration(ServiceHostBase host) {
    var serviceEndpoint = host.Description.Endpoints.First();
    serviceEndpoint.Binding = new BasicHttpBinding {
        ReaderQuotas = {MaxArrayLength = int.MaxValue},
        MaxBufferSize = int.MaxValue,
        MaxReceivedMessageSize = int.MaxValue,
        Security = new BasicHttpSecurity {
            Mode = BasicHttpSecurityMode.Transport, //main setting for https
        },
    };
}

【讨论】:

    猜你喜欢
    • 2011-10-22
    • 2011-09-07
    • 1970-01-01
    • 1970-01-01
    • 2011-04-02
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    • 1970-01-01
    相关资源
    最近更新 更多