I want to add the client address header (IP address of the client) to SOAP Header on each web request from client/user.
I use WCF services, and my implementation class is as follows:
public class Service : IService
{
public Guid UserId()
{
if (MemUser != null)
return (Guid)MemUser.ProviderUserKey;
else
return Guid.Empty;
}
public void SystemLog(Nullable<Guid> Customer, ResultCode Result, FacilityCode Facility, int Code, string Info)
{
// get IP address out of the SOAP header, if any
string clientAddress = string.Empty;
if ((ClientAddress != null) && !String.IsNullOrEmpty(ClientAddress.IPAddress))
clientAddress = ClientAddress.IPAddress;
else if ((HttpContext.Current != null) && (HttpContext.Current.Request != null) && !String.IsNullOrEmpty(HttpContext.Current.Request.UserHostAddress))
clientAddress = HttpContext.Current.Request.UserHostAddress;
SystemLog(Customer, Result, Facility, Code, Info, clientAddress); //writes into database
}
//other methods
}
My interface looks like this:
[ServiceContract(Name = "ServiceSoap", Namespace = "http://www.example.com/webservice"), XmlSerializerFormat]
public interface IService
{
[OperationContract(Action = "http://www.example.com/webservice/UserId")]
Guid UserId();
//other implementations
}
If i have a class for ClientAddressHeader inherited by SoapHeader like
public class ClientAddressHeader : SoapHeader
{
public string IPAddress;
public ClientAddressHeader(){}
}
I can easily pass the SOAP header (ClientAddress) in wse using [WebMethod] like:
[WebService(Namespace = "http://www.example.com/webservice")]
public class Service : System.Web.Services.WebService
{
public ClientAddressHeader ClientAddress;
[WebMethod, SoapHeader("ClientAddress", Direction = SoapHeaderDirection.In)] //client request
public Guid UserId()
{
if (MemUser != null)
return (Guid)MemUser.ProviderUserKey;
else
return Guid.Empty;
}
}
So does anyone know how do I pass the SoapHeader when I use WCF??
I really appreciate if someone explain me clearly how to achieve this.
PS: I have also read some blogs in msdn and Agent Message Inspector none of them are really useful for me or i did not understand that very well.
Related
I have created WCF service. It is working fine but our client want response in some specific form. I have shared 2 responses
My Code :
[ServiceContract]
public interface IService
{
[OperationContract]
MyClass GetMyData();
// TODO: Add your service operations here
}
// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract]
public class MyClass
{
int _id ;
string _name ;
[DataMember]
public int ID
{
get { return _id; }
set { _id = value; }
}
[DataMember]
public string Name
{
get { return _name; }
set { _name = value; }
}
}
Your "Client Expectation" is a XSD file. They are meant to validate SOAP responses, not to be one.
There are plenty of services which can validate your XML response based on your XSD.
If you client wants a XSD response, maybe you'll have to look for a tool to do that. I think this might help.
I have a lot of businesses services already implemented, and I´m exposing them as services by WCF.
I don´t like the idea to have one endpoint to each service..... it could be a problem to maintain in the future as my repository grows.......
I´d like to know wcf´s experts opinions if the code below would be a good approach an them I can move ahead with this solution.
Business Service A:
[ServiceContract]
public interface IServiceA
{
[OperationContract]
object AddA(object a);
[OperationContract]
object Update();
}
Business Service B:
[ServiceContract]
public interface IServiceB
{
[OperationContract]
object AddB(object b);
[OperationContract]
object Update();
}
Concrete implementation for Service A
public class ConcreteServiceA : IServiceA
{
public object AddA(object a)
{
Console.WriteLine("ConcreateServiceA::AddA");
return null;
}
public object Update()
{
Console.WriteLine("ConcreateServiceA::Update");
return null;
}
}
Concrete implementation for Service B
public class ConcreteServiceB : IServiceB
{
public object AddB(object b)
{
Console.WriteLine("ConcreateServiceB::AddB");
return null;
}
public object Update()
{
Console.WriteLine("ConcreateServiceB::Update");
return null;
}
}
My single service is partial to separate concerns to each service.
Note that it´s constructors depends on both business services above, will be injection using IoC
Partial for constructors
public partial class WCFService
{
IServiceA _a;
IServiceB _b;
public WCFService()
: this(new ConcreteServiceA(), new ConcreteServiceB())
{
}
public WCFService(IServiceA serviceA, IServiceB serviceB)
{
_a = serviceA;
_b = serviceB;
}
}
Partial class implementing only IServiveA
public partial class WCFService : IServiceA
{
object IServiceB.AddB(object b)
{
return _b.AddB(b);
}
object IServiceB.Update()
{
return _b.Update();
}
}
Partial class implementing only IServiceB
public partial class WCFService : IServiceB
{
object IServiceA.AddA(object a)
{
return _a.AddA(a);
}
object IServiceA.Update()
{
return _a.Update();
}
}
And in the client side, I using like that:
var endPoint = new EndpointAddress("http://localhost/teste");
ChannelFactory<IServiceA> _factoryA = new ChannelFactory<IServiceA>(new BasicHttpBinding(), endPoint);
IServiceA serviceA = _factoryA.CreateChannel();
serviceA.Update();
var netTcpEndPoint = new EndpointAddress("net.tcp://localhost:9000/teste");
ChannelFactory<IServiceB> _factoryB = new ChannelFactory<IServiceB>(new NetTcpBinding(), netTcpEndPoint);
IServiceB serviceB = _factoryB.CreateChannel();
serviceB.Update();
I really appreciate any opinion or other suggestions.
There's nothing wrong with multiple endpoints - it's part of the process. What is wrong, however, is duplicating functionality over multiple endpoints. How many "UpdateThis's" or "AddThat's" developers need? This can get out of control and makes for a maintenance headache. Just look at your constructor, it will grow and grow as you add new services and consolidate them into one service.
Think coarse-grained not fine-grained.
As an alternative, maybe you can try passing request objects as a parameter and returning response objects. This approach may streamline your code and help you avoid the maintenance issues you mention in your post and gives you a suggestion.
So, it looks something like this:
// Your service will return a very generic Response object
public interface IService
{
Response YourRequest(Request request);
}
// Your service implementation
public partial class WCFService : IService
{
Response IService.YourRequest(Request request)
{
//inspect the Request, do your work based on the values
//and return a response object
}
}
// Your request object
public class Request()
{
object YourClass{get;set;}
DoWhat Action{get;set;} //enum, constants, string etc.
int ID {get; set;}
}
// Your response object
public class Response()
{
bool Success {get; set;}
}
// Create Request object
var request = new Request(){YourClass = YourClassName , Action DoWhat.Update(), ID=1};
// Your service call
var endPoint = new EndpointAddress("http://localhost/teste");
ChannelFactory<IService> _factory = new ChannelFactory<IService>(new BasicHttpBinding(), endPoint);
IService service = _factory.CreateChannel();
var response = service.YourRequest(request);
So, now you've removed the fine-grained approach and replaced it with course-grained one. Let me know if you'd like more detail.
I have a WCF Service like following:
public class Service1 : IService1
{
public string GetData(Person person)
{
if (person != null)
{
return "OK";
}
return "Not OK!";
}
Here is my Person class:
[DataContract]
public class Person
{
[DataMember]
public int Age { get; set; }
[DataMember]
public string Name { get; set; }
}
And I'm calling service like that:
BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
IChannelFactory<IRequestChannel> factory = binding.BuildChannelFactory<IRequestChannel>(new BindingParameterCollection());
factory.Open();
EndpointAddress address = new EndpointAddress(url);
IRequestChannel irc = factory.CreateChannel(address);
using (irc as IDisposable)
{
irc.Open();
string soapMessage = "<GetData><person><Age>24</Age><Name>John</Name></person></GetData>";
XmlReader reader = XmlReader.Create(new StringReader(soapMessage));
Message m = Message.CreateMessage(MessageVersion.Soap11,"http://tempuri.org/IService1/GetData", reader);
Message ret = irc.Request(m);
reader.Close();
return ret.ToString();
}
When I try to send complex type like Person as a parameter to GetData method, person object is coming null. But I have no problem when I send known type like integer, string etc. as a parameter.
How can I manage to send complex type as a parameter to the service method?
I ran into a similar situation, and we ended up changing the interface of the service to be the equivalent of:
public string GetData(string person)
And we did our own object serialization before calling the web service. Immediately within the web service method we would deserialize it, and proceed as normal.
I have a WCF service which has methods that depend on reading values (OData) from the http request's querystring. I'm trying to write unit tests which inject in mock values into the querystring, then when I call the method it would use these mock values rather than erroring due to the request context not being available.
I've tried using WCFMock (which is based on Moq) however I don't see a way to set or get the querystring from the WebOperationContext that it provides.
Any ideas?
I ended up using the IOC pattern to solve this, creating an IQueryStringHelper interface that is passed into the constructor of the service. If it isn't passed in then it'll default to use the "real" QueryStringHelper class. When running test cases, it'll use an overloaded service constructor to pass in the TestQueryStringHelper instance, which lets you set a mock value for the querystring.
Here is the querystring helper code.
public interface IQueryStringHelper {
string[] GetParameters();
}
public class QueryStringHelper : IQueryStringHelper {
public string[] GetParameters() {
var properties = OperationContext.Current.IncomingMessageProperties;
var property = properties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
string queryString = property.QueryString;
return queryString.Split('&');
}
}
public class TestQueryStringHelper : IQueryStringHelper {
private string mockValue;
public TestQueryStringHelper(string value) {
mockValue = value;
}
public string[] GetParameters() {
return mockValue.Split('&');
}
}
And the service implementation:
public partial class RestService : IRestService {
private IAuthenticator _auth;
private IQueryStringHelper _queryStringHelper;
public RestService() : this(new Authenticator(), new QueryStringHelper()) {
}
public RestService(IAuthenticator auth, IQueryStringHelper queryStringHelper = null) {
_auth = auth;
if (queryStringHelper != null) {
_queryStringHelper = queryStringHelper;
}
}
}
And how to consume it from a test case:
string odata = String.Format("$filter=Id eq guid'{0}'", "myguid");
var service = new RestService(m_auth,new TestQueryStringHelper(odata));
var entities = service.ReadAllEntities();
Hopefully this helps someone else.
When I try to use WebOperationContext.Current in a WCF project, the Current is null. Below is the example. Could anyone please shed a light on it?
WebForm - default.aspx:
ServiceClient sc = new ServiceClient();
Response.Write(sc.DoWork(1) + "<br />");
WebOperationContext c = WebOperationContext.Current; --Current is null
//WCF Interface
[ServiceContract]
public interface IService
{
[OperationContract]
[WebGet]
int DoWork(int num);
}
//WCF Implementation
public class Service : IService
{
public int DoWork(int num)
{
return num;
}
}
System Settings:
ASP.NET 3.5
Thank you in advance.
The description of my question is changed to below:
When I try to use WebOperationContext.Current in a WCF project, the Current is null. Below is the example. Could anyone please shed a light on it?
What I need is a transparent way (or a way that changes as little to the existing code) to make the existing code to do a specified work based on a token. This is whay HttpModule is used here, show below:
//HttpModule: Insert a token on which works in the pipneline can be based on. As mentioned, HttpModule is used to make minimum changes to existing code.
public class ImageIntercept : IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}
void context_BeginRequest(object sender, EventArgs e)
{
WebOperationContext.Current.IncomingRequest.Headers.Add("image", "true"); //a token on which works will be based
}
public void Dispose()
{ }
}
//WCF Interface
[ServiceContract]
public interface IService
{
[OperationContract]
[WebGet]
int DoWork(int num);
}
//WCF Implementation
public class Service : IService
{
public int DoWork(int num)
{
string isImage = WebOperationContext.Current.IncomingRequest.Headers["image"];
if(isImage == "true")
{
//this is what I need to do something here
}
return num;
}
}
System Settings: ASP.NET 3.5
Thank you in advance.