WCF CustomBehavior 中的错误 "Cannot set null or blank methods on request."

Error "Cannot set null or blank methods on request." in WCF CustomBehavior

我正在开发一个插入 BizTalk 2010 SendPort 以调用 JSON/REST 服务的 WCF CustomBehavior。 SendPort 是 WCF/Custom,Binding Type=webHttpBinding。

我需要将四个 headers 添加到请求中。

当我在 BeforeSendRequest 方法中包含此代码时:

var newHttpRequestMessageProperty = new HttpRequestMessageProperty
{
    Method = request.Headers.Action,
    QueryString = string.Empty,
    SuppressEntityBody = false,

};

newHttpRequestMessageProperty.Headers.Add("X-ST-PartnerID", partnerIDFromSSO);
newHttpRequestMessageProperty.Headers.Add("X-ST-Token", tokenIDFromSSO);
newHttpRequestMessageProperty.Headers.Add("X-ST-AuthType", "TOKEN");
newHttpRequestMessageProperty.Headers.Add("Accept-Language", "EN");

request.Properties[HttpRequestMessageProperty.Name] = newHttpRequestMessageProperty;

它给我这个错误:

System.ArgumentException: Cannot set null or blank methods on request.
Parameter name: value

Server stack trace: 
   at System.Net.HttpWebRequest.set_Method(String value)
   at System.ServiceModel.Channels.HttpOutput.WebRequestHttpOutput.PrepareHttpSend(Message message)
   at System.ServiceModel.Channels.HttpOutput.BeginSendCore(HttpResponseMessage httpResponseMessage, TimeSpan timeout, AsyncCallback callback, Object state)
   at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelAsyncRequest.SendWebRequest()
   at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelAsyncRequest.OnGetWebRequestCompleted(IAsyncResult result)
   at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelAsyncRequest.BeginSendRequest(Message message, TimeSpan timeout)
   at System.ServiceModel.Channels.RequestChannel.BeginRequest(Message message, TimeSpan timeout, AsyncCallback callback, Object state)
   at System.ServiceModel.Dispatcher

当我遗漏该代码时,我从合作伙伴的 Web 服务返回了 415,因此我知道我至少做到了。

我所做的与此类似post:

从同事那里,我更改了:

     //Method = request.Headers.Action,
     Method = "POST",

它奏效了……

原因是,当你有一个无效的 "OperationName" 时,request.Headers.Action 有来自 BizTalk 发送端口的整个 CustomAction 字符串:

<BtsActionMapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Operation Name="DELETE" Action="DELETE" />
  <Operation Name="GET" Action="GET" />
  <Operation Name="POST" Action="POST" />
</BtsActionMapping>

我正在根据此处的示例对我的代码进行建模: https://biztalkrest.codeplex.com/discussions/657185

在上述修复之后,我突然想到要在编排的逻辑端口中匹配 "Operation Name",果然,我有 "Post" 而不是 "POST"。 呃……所以很明显,当 OperationName 与 CustomAction 操作不匹配时,BizTalk 只是通过……发送整个 CustomAction XML 块……太神奇了……

下图是我从"Post"改成"POST"的地方。