0

有人可以帮我解决这个问题。我无法从出错的地方进行调试。基本上,我在 Azure 中使用 WCF Relay 创建了一个 WCF Rest API WebService,以便在本地和 Azure 之间建立混合连接。此外,如果我忽略任何证书验证,则该端点用于内部部署,因为它是自签名证书,但是在进行 API 调用时,我在 Azure 中发布它时使用 WCF Relay 提供的 base64 编码公钥。

WCF 合同和实施:

合约接口

执行

我成功地在 WCF 服务中获取了“validationToken”,并且在 5 秒以下立即返回了相同的验证令牌。返回后,它总是错误地显示此消息。

用于发送 HTTP 请求的 Postman 客户端

错误响应且未创建订阅

编辑

WCF 合同

[OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "webhookForConservation?validationToken={validationToken}", 
            BodyStyle = WebMessageBodyStyle.Bare)]
        string webhookForConservation(WebhookPayload data, string validationToken);

WCF 实施:

返回 200 OK 状态码的第一种方法:

public string webhookForConservation(WebhookPayload data, string validationToken = "")
        {
            ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
            var client = new HttpClient();
            client.DefaultRequestHeaders.Add("Accept", "text/plain");
            client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
            HttpResponseMessage response = null;
            WebOperationContext ctx = WebOperationContext.Current;
            if (validationToken != null && validationToken != "")
            {
                response = client.PostAsync("http://localhost:8080/conversationWebHook/conversationSubscription?validationToken=" + validationToken, null).Result;
                var apiContent = response.Content.ReadAsStringAsync().Result;
                ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;
                ctx.OutgoingResponse.ContentType = "text/plain";
                return apiContent;
            }
            else
            {
                StringContent strContent = new StringContent(DataContractJsonSerializerHelper.SerializeJson(data));
                strContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
                response = client.PostAsync("http://localhost:8080/conversationWebHook/conversationSubscription", strContent).Result;
            }
            var result = (response != null) ? response.Content.ReadAsStringAsync().Result : "";
            ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;
            return result;
        }

返回 200 状态码的第二种方法:

public WebFaultException<string> webhookForConservation(WebhookPayload data, string validationToken="")
        {
            ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
            var client = new HttpClient();
            client.DefaultRequestHeaders.Add("Accept", "text/plain");
            client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
            HttpResponseMessage response = null;
            if (validationToken != null && validationToken != "")
            {
                response = client.PostAsync("http://localhost:8080/conversationWebHook/conversationSubscription?validationToken=" + validationToken, null).Result;
                var apiContent = response.Content.ReadAsStringAsync().Result;
                return new WebFaultException<string>(apiContent, HttpStatusCode.OK);
            }
            else
            {
                StringContent strContent = new StringContent(DataContractJsonSerializerHelper.SerializeJson(data));
                strContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
                response = client.PostAsync("http://localhost:8080/conversationWebHook/conversationSubscription", strContent).Result;
            }
            var result = (response != null) ? response.Content.ReadAsStringAsync().Result : "";
            return new WebFaultException<string>(result, HttpStatusCode.OK); ;
        }

从 WCF 服务返回 200 OK 响应代码后出现相同的错误

邮递员客户端抛出错误

使用 Postman 直接调用 Relay WCF API:

来自邮递员的 WCF API 调用

标题:

来自 Postman 的 WCF API 调用标头

预先感谢您的所有帮助。

4

1 回答 1

1

有两个问题阻止 WCF 中继正常工作:

  • 中继没有将响应内容类型设置为文本/纯文本,这已修复ctx.OutgoingResponse.ContentType = "text/plain"
  • 中继向所需的响应主体添加了一个 XML 包装器,这是通过将返回值更改为 Stream 来解决的
于 2020-03-19T16:19:04.897 回答