Reputation: 77
Trying to add this header to my request in c#:
<soap:Header>
<UserCredentials soap:mustUnderstand="1" xmlns="http://test.credential.com/UserCredentials" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<CicsUsername xmlns="http://schemas.test.org/2004/07/test.Mainframe">ciscs</CicsUsername>
<TechnicalPassword "http://schemas.test.org/2004/07/test.Mainframe">password</TechnicalPassword>
<TechnicalUsername "http://schemas.test.org/2004/07/test.Mainframe">user</TechnicalUsername>
</UserCredentials>
</soap:Header>
I have tried this with no success:
Kind regards
/Rudy
Upvotes: 1
Views: 2052
Reputation: 7522
Buddy, as the aforementioned link showed, we could use the OperationContext to add custom message header. Please refer to the below example, wish it is useful to you.
Server(Console application, 10.157.13.69:3336)
class Program
{
static void Main(string[] args)
{
using (ServiceHost sh=new ServiceHost(typeof(MyService)))
{
sh.Open();
Console.WriteLine("service is ready....");
Console.ReadLine();
sh.Close();
}
}
}
[ServiceContract]
interface IService
{
[OperationContract]
void WriteMessageHeader();
}
public class MyService : IService
{
public void WriteMessageHeader()
{
OperationContext oc = OperationContext.Current;
//output the SOAP Message Header.
for (int i = 0; i < oc.IncomingMessageHeaders.Count; i++)
{
MessageHeaderInfo info = oc.IncomingMessageHeaders[i];
Console.WriteLine("Name: "+info.Name);
Console.WriteLine("Namespace: "+info.Namespace);
Console.WriteLine("Content: "+oc.IncomingMessageHeaders.GetHeader<string>(i));
}
}
}
Appconfig on the server side.
<system.serviceModel>
<services>
<service name="Server1.MyService">
<endpoint address="" binding="basicHttpBinding" contract="Server1.IService" ></endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:3336"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpsGetEnabled="true" httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
Client(Console application, invocation with ChannelFactory)
class Program
{
static void Main(string[] args)
{
BasicHttpBinding binding = new BasicHttpBinding();
Uri uri = new Uri("http://10.157.13.69:3336");
ChannelFactory<IService> factory = new ChannelFactory<IService>(binding, new EndpointAddress(uri));
IService service = factory.CreateChannel();
//without adding additional messsage header, generally invoke
service.WriteMessageHeader();
//add additional message header.
using (OperationContextScope scope=new OperationContextScope((IContextChannel)service))
{
//insert custom message header
OperationContext oc = OperationContext.Current;
MessageHeader mh = MessageHeader.CreateHeader("MyMessageHeaderName", "MyMessageHeaderNamespace", "myvaule");
oc.OutgoingMessageHeaders.Add(mh);
service.WriteMessageHeader();
}
Console.ReadLine();
}
}
[ServiceContract]
interface IService
{
[OperationContract]
void WriteMessageHeader();
}
Result.
Moreover, we could also use the IClientMessageInspector interface to create a persistent SOAP message header since the above operation that adding the soap message header works only within USING statement. Please refer to my previous reply.
How to pass winform custom user credentials to WCF services in every requests?
Feel free to let me know if there is anything I can help with.
Upvotes: 1