【问题标题】:How to add header or request body using WcfCoreMtomEncoder .NET如何使用 WcfCoreMtomEncoder .NET 添加标头或请求正文
【发布时间】:2021-09-22 15:12:01
【问题描述】:

我正在创建与 WCF 端点通信并返回 MTOM 的 .NET 核心应用程序。 我可以用 HttpWebRequest 做到这一点,但我在向请求中添加其他元素时遇到了问题。

我发现 WcfCoreMtomEncoder 库可以帮助处理这种类型的响应,并且我已经实现了它,如下所示:

 [ServiceContract]
public interface IService
{
    [OperationContract]
    string Test();       
   
}     
myfunction(){
        XmlDocument body.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8""?>  <soapenv:Envelope...");
         var encoding = new MtomMessageEncoderBindingElement(new TextMessageEncodingBindingElement());
        var transport = new HttpsTransportBindingElement();
        transport.TransferMode = TransferMode.Streamed;
        //transport.UseDefaultWebProxy = false;
        transport.ProxyAuthenticationScheme = AuthenticationSchemes.Digest;``
        
        var binding = new CustomBinding(encoding, transport);
        
           
        EndpointAddress endpoint = new EndpointAddress("myEndpointUrl");
        ChannelFactory<IService> channelFactory = new ChannelFactory<IService>(binding, endpoint);
        //channelFactory.Credentials.HttpDigest.ClientCredential.UserName = username;
       //channelFactory.Credentials.HttpDigest.ClientCredential.Password = password;
        var webService = channelFactory.CreateChannel();
        
        try
        {
            Console.WriteLine(webService.Test());
        }
        catch (WebException e)
        {
            string pageContent = new StreamReader(e.Response.GetResponseStream()).ReadToEnd().ToString();
            Console.WriteLine(pageContent);
        }                                                                                     
}

问题

如何向请求添加额外的 Header 属性和soap请求正文(正文变量)?

【问题讨论】:

    标签: .net web-services wcf mtom


    【解决方案1】:

    如果您想添加额外的 Header 属性或soap 请求正文,请实现名为 IDispatchMessageInspector 的接口。 示例如下:

     public class CustomMessageInspector : IDispatchMessageInspector
    {
        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            MessageHeader header = MessageHeader.CreateHeader("Testrequest", "http://Test", "Test");
            OperationContext.Current.IncomingMessageHeaders.Add(header);
            Console.WriteLine("request"+request);
            return null;
        }
    
    public void BeforeSendReply(ref Message reply, object correlationState)
        {
            MessageHeader header = MessageHeader.CreateHeader("Testreply", "http://Test", "Test");
            OperationContext.Current.OutgoingMessageHeaders.Add(header);
            Console.WriteLine("reply"+reply);
        }
    }
    

    CustomMessageInspector 实现了IDispatchMessageInspector 接口。它在获取消息后包含自定义标头,并在发送消息之前添加自定义标头。

    [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 应用到服务中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-02
      • 1970-01-01
      • 1970-01-01
      • 2017-01-22
      • 2011-10-14
      • 1970-01-01
      • 2015-08-30
      相关资源
      最近更新 更多