在运行时将 WCF 服务绑定更改为 https [英] Change WCF Service binding to https at runtime

查看:32
本文介绍了在运行时将 WCF 服务绑定更改为 https的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We have an application that is deployed to many clients, some of which use http others https. When setting up our application the web.config is automatically populated with the WCF endpoints, bindings, etc. We would like to change over the binding to https upon application startup - without modifying the .config file. Is this possible? For example our .config file looks like (this is a snippet):

  <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.

From what I have read it is possible to have an http and https binding at the same time, but we do not want this - if it is https then force it over to use https.

更改应该在服务启动时在 c# 中完成,并且在服务运行时是永久性的.

The change should be done in c# on service startup and be permanent while the service is running.

推荐答案

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

I know it is an old question, but I have had the same problem and found no answer on the SO. As it took me a couple days of digging to solve the problem, I think it is worth posting it here.

这里通常有 3 个选项:

You generally have 3 options here:

1) 按照 这里

2) 根据此处

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

3) Use both *.config and programmatic binding configuration

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

Option (3) is tricky as you have to modify your service host after it has been created but before it would be open (something like host.State == Created). This because modification of bindings of an already open host has no effect. More details here.

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

To get (3) work you have to use custom host factory. Sample markup:

<%@ 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.asax Application_StartMyWcfHostFactory 的静态构造函数调用):

MyWcfHostFactory should inherit ServiceHostFactory and extend/override CreateServiceHost method. I am using DI framework (Autofac) which simplifies things a bit. So MyWcfHostFactory just inherits AutofacHostFactory. Startup code (called from either Global.asax Application_Start or static constructor of MyWcfHostFactory):

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 :

In PerformHostConfiguration you can overwrite or modify bindings. Code below takes 1st registered endpoint and replaces its binding with https one:

/// <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
        },
    };
}

这篇关于在运行时将 WCF 服务绑定更改为 https的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆