We have developed an ASP.NET web application that utilizes WCF services. The current architecture of the system is structured as follows:
In AjaxWebService.svc.cs:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Multiple)]
[ServiceContract(Namespace = "", SessionMode = SessionMode.Allowed)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
private Service m_Service;
private Service Service
{
get
{
if (m_Service == null)
{
m_Service = new Service();
}
return m_Service;
}
}
[OperationContract]
internal Users[] getUsers()
{
return this.Service.getUsers();
}
[OperationContract]
internal products[] getproducts()
{
return this.Service.getproducts();
}
In Service.cs:
private IServiceClient m_ServiceGateway;
private IServiceClient ServiceGateway
{
get
{
if (m_ServiceGateway == null)
{
m_ServiceGateway = new IServiceClient();
}
return m_ServiceGateway;
}
}
internal Users[] getUsers()
{
return this.ServiceGateway.getUsers(this.RoleId, this.Token);
}
internal products[] getproducts()
{
return this.ServiceGateway.getproducts(this.RoleId, this.Token);
}
In IServiceGateway.cs:
[OperationContract]
List<getUsers> getUsers(int RoleId, string Token);
[OperationContract]
List<products> getProducts(int RoleId, string Token);
Challenge: We are looking for a solution to send custom HTTP headers (such as User-Agent and Accept) with our WCF service calls without altering the current method signatures (e.g., return this.ServiceGateway.getUsers(this.RoleId, this.Token); and return this.ServiceGateway.getproducts(this.RoleId, this.Token);).
We attempted to set and pass headers within the ServiceGateway property like this:
private IServiceClient m_ServiceGateway;
private IServiceClient ServiceGateway
{
get
{
if (m_ServiceGateway == null)
{
m_ServiceGateway = new IServiceClient();
using (new OperationContextScope(m_ServiceGateway.InnerChannel))
{
// Set HTTP headers
HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty();
httpRequestProperty.Headers["User-Agent"] = "mrt/1.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)";
httpRequestProperty.Headers["Accept"] = "application/json";
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
}
}
return m_ServiceGateway;
}
}
Despite these efforts, the headers are not being transmitted correctly to the WCF service.
Query: Is there a method to include these headers in the WCF service without modifying the existing method calls like return this.ServiceGateway.getUsers(this.RoleId, this.Token);? How can we implement customized headers universally for all WCF service calls without making changes to every API?