我有一个简单的 wcf 消费者客户端,它向基于 Java 的第 3 方生产者发送“回显”请求。 我已经努力让 WS-Security 正常运行一段时间,但现在可以发送请求和接收回复。 只有当回复到达我的客户时,该值为空。
我已经使用 IClientMessageInspector 来验证我有正确的回复,但我对该服务的调用总是返回 null。
这是我的客户端代码; (对“MyEndPointBehavior”的调用只是添加消息检查器)
string echoString = EchoTesttextbox.Text;
string url = "https://somewhere.com/uk-producer/UKService";
// create the endpoint address
EndpointAddress endPointAddress = new EndpointAddress(new Uri(url), EndpointIdentity.CreateDnsIdentity("TestProducer"));
PortBindingClient client = new PortBindingClient("Port12", endPointAddress);
client.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.Root, X509FindType.FindByThumbprint, ".....");
client.ClientCredentials.ServiceCertificate.SetDefaultCertificate(StoreLocation.LocalMachine, StoreName.TrustedPeople, X509FindType.FindByThumbprint, ".....");
client.Endpoint.Behaviors.Add(new MyEndpointBehavior());
string response = String.Empty;
response = client.echo(echoString);
MessageBox.Show(response);
还有我的消息检查器代码;
public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
{
// Implement this method to inspect/modify messages after a message
// is received but prior to passing it back to the client
// make a copy of the reply that we can play with
MessageBuffer buffer = reply.CreateBufferedCopy(Int32.MaxValue);
// the message variable holds a reference to a copy of the initial reply
Message message = buffer.CreateMessage();
// assign a copy back to the ref received so it can continue undisturbed
reply = buffer.CreateMessage();
StringWriter stringWriter = new StringWriter();
XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter);
message.WriteBody(xmlTextWriter);
xmlTextWriter.Flush();
xmlTextWriter.Close();
string body = stringWriter.ToString();
}
我的问题是 IClientMessageInspector.AfterReceiveReply 和试图在我的 MessageBox 中显示响应之间发生了什么? 如果我可以尝试了解正在发生的事情,我或许可以尝试找出响应未通过的原因。
最佳答案
那真是令人沮丧。 我收到空值的原因完全与命名空间有关。
在 IClientMessageInspector 中,我可以检查 SOAP 主体并看到它
<soapenv:Body wsu:Id="Id-1555290177" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<me:EchoResponse xmlns:me="http://somewhere.com/uk-producer/UKService">test11</me:EchoResponse>
</soapenv:Body>
WSDL 和 reference.cs(我使用来自第 3 方 WSDL 的 VS2012 的“添加服务引用”功能生成了我的代理)显示了这一点;
[System.ServiceModel.MessageBodyMemberAttribute(Name="EchoResponse", Namespace="http://somewhere.com/uk-producer/UKService/", Order=0)]
public string EchoResponse1;
很长一段时间,一切看起来都很好。 但是后来,出于沮丧,我尝试将 reference.cs 更改为此
[System.ServiceModel.MessageBodyMemberAttribute(Name="EchoResponse", Namespace="http://somewhere.com/uk-producer/UKService", Order=0)]
public string EchoResponse1;
我只删除了命名空间的最后一个正斜杠
这一切都奏效了。
关于c# - WCF 客户端接收空响应值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18698249/