【问题标题】:Add namespace to WCF method将命名空间添加到 WCF 方法
【发布时间】:2021-05-30 16:41:18
【问题描述】:

我有这个 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”,但我只想在方法名称之前使用它

【问题讨论】:

    标签: c# wcf soap web-config


    【解决方案1】:

    您可以使用 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 功能,您使用客户端拦截器将其添加到客户端界面,并使用服务器端消息拦截器将其添加到服务器界面。

    这里是参考:IClientMessageInspectorIDispatchMessageInspector

    【讨论】:

    • 我添加了这些类,但没有发现任何差异。我还在 Add AddBindingParameters 方法上添加了这个代码块:foreach (OperationDescription operation in contractDescription.Operations) { operation.DeclaringContract.Namespace = "temp"; }
    • 使用该类自定义消息,可以自定义消息头和消息。使用该类修改soap消息,需要添加自己要添加的消息,修改这部分代码:MessageHeader header = MessageHeader.CreateHeader("UserAgent", "User", "User1");回复.Headers.Add(header);
    【解决方案2】:

    经过几天的搜索,我找到了link 的解决方案。另外,也不是因为客户端的输出有问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 2017-09-21
      • 1970-01-01
      相关资源
      最近更新 更多