Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 在运行时将WCF服务绑定更改为https_.net_Wcf_Wcf Binding - Fatal编程技术网

.net 在运行时将WCF服务绑定更改为https

.net 在运行时将WCF服务绑定更改为https,.net,wcf,wcf-binding,.net,Wcf,Wcf Binding,我们有一个部署到许多客户端的应用程序,其中一些客户端使用http,另一些客户端使用https。设置应用程序时,web.config会自动填充WCF端点、绑定等。我们希望在应用程序启动时将绑定转换为https,而不修改.config文件。这可能吗?例如,我们的.config文件如下所示(这是一个代码片段): 从我所读到的内容来看,可以同时使用http和https绑定,但我们不希望这样-如果是https,则强制使用https 更改应该在服务启动时在c#中完成,并在服务运行时保持永久性。我知道这是

我们有一个部署到许多客户端的应用程序,其中一些客户端使用http,另一些客户端使用https。设置应用程序时,web.config会自动填充WCF端点、绑定等。我们希望在应用程序启动时将绑定转换为https,而不修改.config文件。这可能吗?例如,我们的.config文件如下所示(这是一个代码片段):


从我所读到的内容来看,可以同时使用http和https绑定,但我们不希望这样-如果是https,则强制使用https


更改应该在服务启动时在c#中完成,并在服务运行时保持永久性。

我知道这是一个老问题,但我遇到了同样的问题,在SO上没有找到答案。因为我花了几天的时间才解决了这个问题,所以我觉得把它贴在这里是值得的

这里通常有3个选项:

1) 按照以下步骤在代码中完全配置服务主机(忽略*.config)

2) 制作脚本以修改*.config或根据使用其他配置

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

选项(3)很棘手,因为您必须在创建服务主机之后但在它打开之前修改它(类似于
host.State==created
)。这是因为修改已打开主机的绑定无效。更多细节

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

<%@ ServiceHost 
    Language="C#" 
    Debug="true" 
    CodeBehind="MyService.svc.cs"
    Service="Namespace.MyService, MyService" 
    Factory="Namespace.MyWcfHostFactory, MyService"
%>
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
        },
    };
}
///这被用作委托来执行wcf主机配置-设置行为、全局错误处理程序、身份验证等
专用静态void PerformHostConfiguration(ServiceHostBase主机){
var serviceEndpoint=host.Description.Endpoints.First();
serviceEndpoint.Binding=新的BasicHttpBinding{
ReaderQuotas={MaxArrayLength=int.MaxValue},
MaxBufferSize=int.MaxValue,
MaxReceivedMessageSize=int.MaxValue,
安全性=新的基本安全性{
Mode=BasicHttpSecurityMode.Transport,//https的主设置
},
};
}

应用程序是托管服务还是与服务对话?您不希望服务有两个端点-http和https?请注意,给定的服务实例不能同时具有两个绑定—它只能具有一个绑定。如果该服务独立于应用程序,我建议设置两个端点,并让各个客户端选择合适的端点。抱歉没有说得更清楚。这是一个应用程序,客户端和服务都相互通信。当我们在内部开发它时,我们使用http,因为我们没有证书。我们的一些客户端有自己的证书,并且只希望通过https进行通信。我可以修改客户端应用程序以使用https,现在是服务器,我不知道如何修改为永远不使用http,只使用https。
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));
/// <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
        },
    };
}