gpt4 book ai didi

c# - 需要实现 MessageHeaders.WriteHeaderContents 的示例

转载 作者:行者123 更新时间:2023-11-30 15:42:57 25 4
gpt4 key购买 nike

我有一个存储在“字符串”变量中的自定义 header ,我需要用“字符串”类型变量中的 header 替换来 self 的 WCF 客户端的传出 SOAP 请求的 header 。根据研究,我发现实现 MessageHeaders.WriteHeaderContents 可以工作,但此方法仅接受 XmlDictionaryWriterXmlWriter 类型作为输入。我有一个字符串输入。我如何在 C# 中编码 ..

最佳答案

消息 header 是一个 SOAP 概念,而 SOAP 请求是 XML 文档,因此您确实需要一些 XML-ness。但是对于您的场景,您实际上不需要覆盖 MessageHeaders,您可以使用检查器并在该点简单地替换 header ,如下例所示(转换为 XML 将完成通过 MessageHeader 类)。

public class StackOverflow_7141998
{
[MessageContract]
public class MyMC
{
[MessageHeader(Name = "MyHeader", Namespace = "http://my.namespace.com")]
public string HeaderValue { get; set; }
[MessageBodyMember(Name = "MyBody", Namespace = "http://my.namespace.com")]
public string BodyValue { get; set; }
}
[ServiceContract]
public interface ITest
{
[OperationContract]
void Process(MyMC mc);
}
public class Service : ITest
{
public void Process(MyMC mc)
{
Console.WriteLine("Header value: {0}", mc.HeaderValue);
}
}
public class MyInspector : IEndpointBehavior, IClientMessageInspector
{
public string NewHeaderValue { get; set; }

public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}

public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
clientRuntime.MessageInspectors.Add(this);
}

public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
}

public void Validate(ServiceEndpoint endpoint)
{
}

public void AfterReceiveReply(ref Message reply, object correlationState)
{
}

public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
int originalIndex = request.Headers.FindHeader("MyHeader", "http://my.namespace.com");
if (originalIndex >= 0)
{
request.Headers.Insert(originalIndex, MessageHeader.CreateHeader("MyHeader", "http://my.namespace.com", this.NewHeaderValue));
request.Headers.RemoveAt(originalIndex + 1);
}

return null;
}
}
public static void Test()
{
string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(ITest), new WSHttpBinding(), "");
host.Open();
Console.WriteLine("Host opened");

ChannelFactory<ITest> factory = new ChannelFactory<ITest>(new WSHttpBinding(), new EndpointAddress(baseAddress));
MyInspector inspector = new MyInspector { NewHeaderValue = "Modified header value" };
factory.Endpoint.Behaviors.Add(inspector);
ITest proxy = factory.CreateChannel();

proxy.Process(new MyMC { HeaderValue = "Original header value", BodyValue = "The body" });

((IClientChannel)proxy).Close();
factory.Close();

Console.Write("Press ENTER to close the host");
Console.ReadLine();
host.Close();
}
}

关于c# - 需要实现 MessageHeaders.WriteHeaderContents 的示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7141998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com