Reputation: 1
I'm creating .NET core application that communicates with WCF endpoint and returns MTOM. I was able to do it with HttpWebRequest but i have a problem with adding additional elements to request.
I've found WcfCoreMtomEncoder lib that helps with that type of responses and I've implemented it like below:
[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);
}
}
QUESTION
How can I add additional Header attributes and a soap request body (body variable) to request?
Upvotes: 0
Views: 240
Reputation: 578
If you want to add additional Header attributes or a soap request body,implement the interface called IDispatchMessageInspector. Here the example:
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 implements the interface called IDispatchMessageInspector.It contains a custom header to the message after getting it,and also adds a custom header before sending the message.
[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;
}
}
Next we add the interceptor to the behavior of the service.
Finally we apply CustomBehavior to the service.
Upvotes: 1