gpt4 book ai didi

c# - WCF 添加命名空间属性

转载 作者:太空宇宙 更新时间:2023-11-03 12:10:02 26 4
gpt4 key购买 nike

我有一个获取两个值的简单 WCF。这是我的代码:

[ServiceContract]
public interface IService
{

[OperationContract]
List<string> comunicarAreaContencaoResponse(string Result, string Obs);
}

还有这个:

public class Service : IService
{

public List<string> comunicarAreaContencaoResponse(string Result, string
Obs)
{
List<string> ListResultados = new List<string>();

if (Result != null)
{
ListResultados.Add(Result);
}

if (Obs != null)
{
ListResultados.Add(Obs);
}

return ListResultados;

}

}

在 SoapUi 中我有这个结果

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:comunicarAreaContencaoResponse>
<!--Optional:-->
<tem:Result>?</tem:Result>
<!--Optional:-->
<tem:Obs>?</tem:Obs>
</tem:comunicarAreaContencaoResponse>
</soapenv:Body>
</soapenv:Envelope>

但我需要这样:

       <soapenv:Envelope 
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:comunicarAreaContencaoResponse
xmlns="http://www.outsystems.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<tem:Result>false</tem:Result>
<tem:Obs />
</tem:comunicarAreaContencaoResponse>
</soapenv:Body>
</soapenv:Envelope>

之所以需要如此具体,是因为这条消息在发送到目的地之前要经过一个中间件。但我似乎找不到在我的消息中插入这些 namespace 的方法。如果我做不到,这将不会被发送。你能帮帮我吗?

最佳答案

根据您的描述,我认为您可以使用 WCF Message Inspector。在客户端发送消息之前。我们可以自定义消息正文。
https://learn.microsoft.com/en-us/dotnet/framework/wcf/samples/message-inspectors
根据您的代码,我制作了一个添加命名空间属性的演示。这是客户端代码。我在当前项目中添加了服务引用,所以项目中已经生成了服务契约。

客户端。

static void Main(string[] args)
{
ServiceReference1.ServiceClient client = new ServiceReference1.ServiceClient();

try
{
var result = client.comunicarAreaContencaoResponse("Hello","World");
foreach (var item in result)
{
Console.WriteLine(item);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}



public class ClientMessageLogger : IClientMessageInspector
{
public void AfterReceiveReply(ref Message reply, object correlationState)
{
string result = $"server reply message:\n{reply}\n";
Console.WriteLine(result);
}

public object BeforeSendRequest(ref Message request, IClientChannel channel)
{

// Read reply payload
XmlDocument doc = new XmlDocument();
MemoryStream ms = new MemoryStream();
XmlWriter writer = XmlWriter.Create(ms);
request.WriteMessage(writer);
writer.Flush();
ms.Position = 0;
doc.Load(ms);

// Change Body logic
ChangeMessage(doc);

// Write the reply payload
ms.SetLength(0);
writer = XmlWriter.Create(ms);

doc.WriteTo(writer);
writer.Flush();
ms.Position = 0;
XmlReader reader = XmlReader.Create(ms);
request = System.ServiceModel.Channels.Message.CreateMessage(reader, int.MaxValue, request.Version);
string result = $"client ready to send message:\n{request}\n";
Console.WriteLine(result);
return null;
}
void ChangeMessage(XmlDocument doc)
{
XmlElement element = (XmlElement)doc.GetElementsByTagName("comunicarAreaContencaoResponse").Item(0);
if (element!=null)
{
element.SetAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
element.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
element.Attributes.RemoveNamedItem("xmlns:i");
}
}
}
public class CustContractBehaviorAttribute : Attribute, IContractBehavior, IContractBehaviorAttribute
{
public Type TargetContract => typeof(IService);

public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
return;
}

public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
clientRuntime.ClientMessageInspectors.Add(new ClientMessageLogger());
}

public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
{
return;
}

public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
{
return;
}
}

将属性添加到服务契约(Contract)。

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(ConfigurationName="ServiceReference1.IService")]
[CustContractBehavior]
public interface IService {
}

结果。

enter image description here

关于c# - WCF 添加命名空间属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52854540/

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