如何解决将命名空间添加到 WCF 方法
我有这个 WCF 项目,我想向方法添加“tem”前缀,而不是像这些行 XML 代码那样输入
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/><soapenv:Body>
<tem:FetchServiceInfoByBillId>
<USER_NAME>username</USER_NAME>
</tem:FetchServiceInfoByBillId>
</soapenv:Body>
</soapenv:Envelope>
这是界面
[ServiceContract(Namespace ="")]
public interface IOnlineService
{
[OperationContract]
[return: MessageParameter(Name = "Error_code")]
string FetchServiceInfoByBillId(string USER_NAMES);
}
我通过这个接口得到这些 XML
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/><soapenv:Body>
<FetchServiceInfoByBillId>
<USER_NAME>username</USER_NAME>
</FetchServiceInfoByBillId>
</soapenv:Body>
</soapenv:Envelope>
如果我使用 [ServiceContract]
而不是 [ServiceContract(Namespace ="")]
我会在方法和输入之前得到 'tem' 但我只想要它在方法名称之前
解决方法
您可以使用 MessageInterceptor 自定义或修改 SOAP 消息。
这是客户端上的示例代码:
public class ClientMessageLogger : IClientMessageInspector
{
public void AfterReceiveReply(ref Message reply,object correlationState)
{
MessageHeader header1 = MessageHeader.CreateHeader("Testreply","http://Test","Test");
request.Headers.Add(header1);
}
public object BeforeSendRequest(ref Message request,IClientChannel channel)
{
MessageHeader header = MessageHeader.CreateHeader("UserAgent","http://User","User1");
reply.Headers.Add(header);
return null;
}
}
[AttributeUsage(AttributeTargets.Interface)]
public class CustomBehavior : Attribute,IContractBehavior
{
public Type TargetContract => typeof(ServiceReference1.ICalculator);
public void AddBindingParameters(ContractDescription contractDescription,ServiceEndpoint endpoint,BindingParameterCollection bindingParameters)
{
return;
}
public void ApplyClientBehavior(ContractDescription contractDescription,ClientRuntime clientRuntime)
{
clientRuntime.ClientMessageInspectors.Add(new ClientMessageLogger());
}
public void ApplyDispatchBehavior(ContractDescription contractDescription,DispatchRuntime dispatchRuntime)
{
return;
}
public void Validate(ContractDescription contractDescription,ServiceEndpoint endpoint)
{
return;
}
}
这是服务的示例代码:
public class CustomMessageInspector : IDispatchMessageInspector
{
public object AfterReceiveRequest(ref Message request,IClientChannel channel,InstanceContext instanceContext)
{
MessageHeader header = MessageHeader.CreateHeader("UserAgent","User1");
request.Headers.Add(header);
return null;
}
public void BeforeSendReply(ref Message reply,"Test");
reply.Headers.Add(header1);
}
}
[AttributeUsage(AttributeTargets.Interface)]
public class CustomBehavior : Attribute,IContractBehavior
{
public void AddBindingParameters(ContractDescription contractDescription,ClientRuntime clientRuntime)
{
return;
}
public void ApplyDispatchBehavior(ContractDescription contractDescription,DispatchRuntime dispatchRuntime)
{
dispatchRuntime.MessageInspectors.Add(new CustomMessageInspector());
}
public void Validate(ContractDescription contractDescription,ServiceEndpoint endpoint)
{
return;
}
}
要应用消息拦截器,必须添加CustomBehavior特性,使用客户端拦截器将其添加到客户端界面,并使用服务器端消息拦截器将其添加到服务器界面。
这里是参考:IClientMessageInspector IDispatchMessageInspector
,经过几天的搜索,我找到了这个 link 的解决方案。另外,也不是因为客户端的输出有问题
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。