将命名空间添加到 WCF 方法
Add namespace to WCF method
我有这个 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, 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;
}
}
这是服务的示例代码:
public class CustomMessageInspector : IDispatchMessageInspector
{
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
MessageHeader header = MessageHeader.CreateHeader("UserAgent", "http://User", "User1");
request.Headers.Add(header);
return null;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
MessageHeader header1 = MessageHeader.CreateHeader("Testreply", "http://Test", "Test");
reply.Headers.Add(header1);
}
}
[AttributeUsage(AttributeTargets.Interface)]
public class CustomBehavior : Attribute, IContractBehavior
{
public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
return;
}
public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
return;
}
public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
{
dispatchRuntime.MessageInspectors.Add(new CustomMessageInspector());
}
public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
{
return;
}
}
要应用消息拦截器,您必须添加 CustomBehavior 功能,您可以使用客户端拦截器将其添加到客户端界面,并使用服务器端消息拦截器将其添加到服务器界面。
经过几天的搜索,我找到了这个 的解决方案。另外,也不是因为客户端的输出有问题
我有这个 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, 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;
}
}
这是服务的示例代码:
public class CustomMessageInspector : IDispatchMessageInspector
{
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
MessageHeader header = MessageHeader.CreateHeader("UserAgent", "http://User", "User1");
request.Headers.Add(header);
return null;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
MessageHeader header1 = MessageHeader.CreateHeader("Testreply", "http://Test", "Test");
reply.Headers.Add(header1);
}
}
[AttributeUsage(AttributeTargets.Interface)]
public class CustomBehavior : Attribute, IContractBehavior
{
public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
return;
}
public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
return;
}
public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
{
dispatchRuntime.MessageInspectors.Add(new CustomMessageInspector());
}
public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
{
return;
}
}
要应用消息拦截器,您必须添加 CustomBehavior 功能,您可以使用客户端拦截器将其添加到客户端界面,并使用服务器端消息拦截器将其添加到服务器界面。
经过几天的搜索,我找到了这个