C#WCF GetWebRequest:如何在标题中设置属性
重写GetWebRequest和访问标头工作得很好。现在我需要设置一个属性。我的目标看起来像C#WCF GetWebRequest:如何在标题中设置属性,c#,wcf,soapheader,C#,Wcf,Soapheader,重写GetWebRequest和访问标头工作得很好。现在我需要设置一个属性。我的目标看起来像 <soapenv:Envelope xmlns:urn="urn:company:sap:ds:sales" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Header> <messageId xmlns="http://www.company.com/foo/bar/soap/f
<soapenv:Envelope xmlns:urn="urn:company:sap:ds:sales" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header>
<messageId xmlns="http://www.company.com/foo/bar/soap/features/messageId/">urn:uuid:123c155c-3ab4-19ca-a045-02003b1bb7f5</messageId>
</soapenv:Header>
<soapenv:Body>
...
</soapenv:Body>
</soapenv:Envelope>
但我觉得这有点不对劲。请参考下面的代码片段,希望它对您有用
class Program
{
static void Main(string[] args)
{
ServiceReference1.ServiceClient client = new ServiceClient();
using (new OperationContextScope(client.InnerChannel))
{
UserInfo userInfo = new UserInfo();
userInfo.FirstName = "John";
userInfo.LastName = "Doe";
MessageHeader aMessageHeader = MessageHeader.CreateHeader("UserInfo", "http://tempuri.org", userInfo);
MessageHeader bheader = MessageHeader.CreateHeader("messageId", "http://www.company.com/foo/bar/soap/features/messageId/", "urn:uuid:123c155c-3ab4-19ca-a045-02003b1bb7f5");
OperationContext.Current.OutgoingMessageHeaders.Add(aMessageHeader);
OperationContext.Current.OutgoingMessageHeaders.Add(bheader);
var result = client.Test();
Console.WriteLine(result);
}
}
}
public class UserInfo
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
结果。这是客户端上的一个实现,您还可以使用
IClientMessageInspector
或IDispatchMessageInspector
界面在客户端和服务器端添加自定义头。如果有什么我可以帮忙的,请随时告诉我。SOAP头和HTTP头不是一回事。您是如何创建SOAP消息的?您在哪里重写GetWebRequest?我不知道WCF中有一个扩展点允许您这样做,但我准备更正。这里如何涉及WCF?我有一个描述目标服务的WSDL文件,还有一个XML文件作为示例。我使用Add->Web服务引用导入了WSDL。这在我的项目中创建了SoapHttpClientProtocol派生的服务(作为部分类实现)。我正在重写此服务GetWebRequest。
SoapHttpClientProtocol
不是WCF,这是来自旧的XML Web服务工具集,因此我感到困惑。从粗略的谷歌搜索来看,修改标题在这里似乎相当困难,而公认的答案提供了一种很好的方法,使用IClientMessageInspector
-有什么理由不使用WCF客户端而选择ASMX web客户端吗?嗯,这不是解决我问题的直接方法,但它有帮助。非常感谢:)
class Program
{
static void Main(string[] args)
{
ServiceReference1.ServiceClient client = new ServiceClient();
using (new OperationContextScope(client.InnerChannel))
{
UserInfo userInfo = new UserInfo();
userInfo.FirstName = "John";
userInfo.LastName = "Doe";
MessageHeader aMessageHeader = MessageHeader.CreateHeader("UserInfo", "http://tempuri.org", userInfo);
MessageHeader bheader = MessageHeader.CreateHeader("messageId", "http://www.company.com/foo/bar/soap/features/messageId/", "urn:uuid:123c155c-3ab4-19ca-a045-02003b1bb7f5");
OperationContext.Current.OutgoingMessageHeaders.Add(aMessageHeader);
OperationContext.Current.OutgoingMessageHeaders.Add(bheader);
var result = client.Test();
Console.WriteLine(result);
}
}
}
public class UserInfo
{
public string FirstName { get; set; }
public string LastName { get; set; }
}