如何在 Azure 应用程序网关后面托管 WCF 服务时获取正确的 WSDL URL



我们正在将服务从本地迁移到Azure,我们现在正在托管一个旧的WCF服务Azure应用服务。要使其向后兼容现有客户端,它需要通过 {domainNameHere}.com/services 提供。

还有其他服务应该通过同一域访问,例如 {domainNameHere}.com/api .我们已设置 Azure 应用程序网关,以根据请求路径将请求路由到不同的应用服务,并将{domainNameHere}.com设置为指向应用程序网关 IP。

这适用于除 WCF 服务之外的所有服务。它可以在浏览器中访问 https://{domainNameHere}.com/services/exports.svc ,但该页面上的 WSDL URI 显示的是azurewebsites.net URI,而不是我们的自定义域。当客户端尝试访问该服务时,它们会收到以下错误:

System.ServiceModel.EndpointNotFoundException:
'There was no endpoint listening at https://{domainNameHere}.com/services/export.svc
that could accept the message. This is often caused by an incorrect address or SOAP
action. See InnerException, if present, for more details.'
Inner Exception
WebException: The remote server returned an error: (404) Not Found.

我们试图在 WCF 配置中使用useRequestHeadersForMetadataAddress但无济于事。

下面是 WCF 配置,全部在代码中。endpoint URI 是 https://{domainNameHere}.com/services/exports.svc

public static void Configure<T>(ServiceConfiguration config, Uri endpoint) where T : class
{
    // Configure service behavior
    config.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true, HttpsGetEnabled = true });
    config.Description.Behaviors.Add(new ServiceDebugBehavior { IncludeExceptionDetailInFaults = true });
    var serviceCredential = config.Description.Behaviors.Find<ServiceCredentials>();
    if (serviceCredential == null)
    {
        serviceCredential = new ServiceCredentials();
        config.Description.Behaviors.Add(serviceCredential);
    }
    serviceCredential.UserNameAuthentication.UserNamePasswordValidationMode = UserNamePasswordValidationMode.Custom;
    serviceCredential.UserNameAuthentication.CustomUserNamePasswordValidator = new CredentialsValidator();
    config.AddServiceEndpoint(typeof(T), GetBinding(), endpoint);
}

我终于想通了。应用程序网关使用 HTTP 向 WCF 服务发出请求,但 WCF 服务仅在 HTTPS 上回复。更新应用程序网关以使用 HTTPS 发出请求后,它就按预期工作。

最新更新