Serialization of nested Action<T> for service bus Queue - c#

The general consensus from reading other posts is that serializing anonymous delegates and actions etc is a bad idea and brittle etc. The lifetime of these serialized actions will be very short lived but i'm open to better ideas about how to accomplish what i'm doing.
I have a web role and a worker role. The user can upload massive amounts of data that I need to split down and send to external wcf services. I need to queue this data and I just wanted a generic interface. I'd like to be able to deserialize it and call Execute() on it without having custom logic in the worker role.
I have 4 external services (may grow) each with multiple calls and there own unique parameters. I wanted a method of constructing the parameters in the web role, declaring which serviceInvoker class and interface I want to construct (generates a ChannelFactory with client credentials etc), and executing the child method with said parameters.
// Code
var queueMessage = new WorkOutMessage<IService1>(User.agent.Client.Id,
new Action<ServiceInvoker<IService1>>(serviceInvoker =>
serviceInvoker.InvokeService(proxy =>
{
using (new OperationContextScope((IContextChannel)proxy))
{
OperationContext.Current.OutgoingMessageHeaders.ReplyTo = new EndpointAddress("https://someAddress.com/");
OperationContext.Current.OutgoingMessageHeaders.MessageId = new System.Xml.UniqueId(head.CorrelationID);
proxy.SomeChildFunction(envelope);
}
})));
var stream = new MemoryStream();
var formatter = new BinaryFormatter();
formatter.Serialize(stream, queueMessage;
var brokeredMessage = new BrokeredMessage(stream) { CorrelationId = correlationId };
// Formatter.Serialize throws an exception
Type '<MyController>+<>c__DisplayClassc' in Assembly '<MyDll>, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
// Classes
public interface IQueueProxyMessage
{
int Attempts();
bool Execute();
}
[Serializable]
public class WorkOutMessage<T> : IQueueProxyMessage where T : class
{
public Guid clientId { get; set; }
public Action<ServiceInvoker<T>> action { get; set; }
public int attempts { get; set; }
public WorkOutMessage(Guid clientId, Action<ServiceInvoker<T>> action)
{
this.clientId = clientId;
this.action = action;
this.attempts = 0;
}
public int Attempts()
{
return this.attempts;
}
public bool Execute()
{
this.attempts++;
try
{
var config = InfrastructureConfiguration.Instance;
var storage = new AzureStorageService();
var db = new DbContext(config.azure.dbConnectionString,
DbValidationMode.Enabled,
DbLazyLoadingMode.Disabled,
DbAutoDetectMode.Enabled);
var client = db.Client.Include(x => x.Organisation)
.Include(x => x.Certificate)
.Where(x => x.Active
&& x.Organisation.Active
&& x.Certificate != null
&& x.Id == this.clientId)
.FirstOrDefault();
if(client != null)
{
X509Certificate2 clientCert;
string clientCertContentType;
byte[] clientCertContent;
if (storage.GetBlob(AzureBlobType.Certificate, client.Certificate.StorageId.ToString(), out clientCertContentType, out clientCertContent))
{
var base64EncodedBytes = Encoding.Unicode.GetString(clientCertContent);
clientCert = new X509Certificate2(Convert.FromBase64String(base64EncodedBytes), "test", X509KeyStorageFlags.PersistKeySet);
if(clientCert != null)
{
var serviceInvoker = new ServiceInvoker<T>(client.Id, "test", clientCert, config.publicCertificate);
if(serviceInvoker != null)
{
this.action.Invoke(serviceInvoker);
}
// error
}
// error
}
// error
}
else
{
}
return true;
}
catch (Exception ex)
{
return false;
}
}
Obviously my serialization knowledge is pretty non-existant. which is why I thought this would work in the first place. Is there any way to re-architect this so I can still specify what serviceInvoker to use, what method and pass the parameters?
Edit: the code where I try to use BinaryFormatter and Exception
Edit2: This is a bad idea, i'm going to pass the data and reconstruct the class on the worker.

Related

Proper way to rise an event from one class and handle it in other class

I am building api for one application. what i want is when I get a new order, i have to insert it in DB and I want to rise an event (without blocking my api call may be async). so that latter in other class i can handle that event and do the other work like send the new order notification to account and send order update to user using pubnub.
public class OrderService : IOrderService
{
public delegate void OrderEventHandler(Order order,bool isNewOrder);
public static event OrderEventHandler OrderEvents = delegate { };
public OrderService()
{
OrderEventListener listener = new OrderEventListener();
OrderEvents += new OrderEventHandler(listener.HandleOrderEvents);
}
#region Methods
public void Test()
{
INPRODataFacade facade = new NPRODataFacade();
var unitOfWork = facade.GetUnitOfWork();
var order = unitOfWork.OrderRepository.Find(o => o.OrderID == 1).FirstOrDefault();
unitOfWork.Commit();
facade.ReturnUnitOfWork();
RiseEvent(order,true);
}
private async System.Threading.Tasks.Task RiseEvent(Order order,bool isNewOrder)
{
if (order == null)
{
return;
}
OrderEvents(order,isNewOrder);
}
}
public class OrderEventListener
{
PubNub.PubNub pubNub = null;
const string ChanelPrifix = "fasttract";
public OrderEventListener()
{
pubNub = new PubNub.PubNub(ConfigurationSettings.PubNub_SubscribeKey, ConfigurationSettings.PubNub_PublishKey, ConfigurationSettings.PubNub_SecretKey, ConfigurationSettings.PubNub_SSlOn);
}
public void HandleOrderEvents(Order order, bool isNewOrder)
{
string chanelName = isNewOrder ? string.Format("{0}_{1}", ChanelPrifix, order.AccountID)
: string.Format("{0}_{1}_{2}", ChanelPrifix, order.AccountID, order.OrderID);
var channel = pubNub.Channel(chanelName);
channel.Publish(new PubNubMessageModel { Message = GetMessage( order, isNewOrder) });
}
public string GetMessage(Order order,bool isNewOrder)
{
string message = string.Empty;
if(isNewOrder)
{
message = "You have recieved a new order #" + order.OrderID;
return message;
}
message = order.Status.ToString();
return message;
}
}
Here have noticed that it is blocking my api call. means I am not getting api response back until event handler finished its execution. is this the right way to handle events ? I think there should be a way that i just add events while getting new order and then in listener i process that events anytime without blocking api call.

Rhino Mocks - Arg<T> to ommit some parameters

I am using Rhino Mocks to stub out the functionality of a SOAP endpoint and for the most part it works. But, the interface is quite quirky and I am struggling to get the following to work (i have renamed the classes for simplicity)
public interface IWebService
{
void CopyFile(Request request);
}
public interface IService
{
void CopyFile(string filename, byte[] data);
}
public class Request
{
public string Filename { get; set; }
public byte[] Data { get; set; }
}
public class Service : IService
{
IWebService _service;
public Service(IWebService service)
{
_service = service;
}
public void CopyFile(string filename, byte[] data)
{
_service.CopyFile(new Request() {Filename = filename,Data = data });
}
}
Now, in my test I have something like this
[TestCase]
public void TestFileCopyFailsIfFilenameIsMissing()
{
IWebService serviceMock = MockRepository.GenerateMock<IWebService>();
serviceMock.Expect(x => x.CopyFile(Arg<Request>.Is.Equal(new Request() { Filename = Arg<string>.Is.Null, Data = Arg<byte[]>.Is.Anything }))).Throw(new Exception());
Service service = new Service(serviceMock);
service.CopyFile(null, new byte[] { });
}
Which throws the exception:
An exception of type 'System.InvalidOperationException' occurred in Rhino.Mocks.dll but was not handled in user code
Additional information: Use Arg ONLY within a mock method call while recording. 1 arguments expected, 3 have been defined.
I have tried the all possibilities in the world on this one, but cant get it right. If i dont use Arg and use
Expect(null, new byte[]{});
it will always pass no matter what
I suggest to use WhenCalled and in this method check Request object.
bool isCorrectParam = false;
IWebService serviceMock = MockRepository.GenerateMock<IWebService>();
serviceMock.Expect(x => x.CopyFile(null))
.IgnoreArguments()
.WhenCalled(x =>
{
Request req = x.Arguments[0] as Request;
if (req.Data.Count() == 0 && req.Filename == null)
{
isCorrectParam = true;
}
});
Service service = new Service(serviceMock);
service.CopyFile(null, new byte[] { });
Assert.IsTrue(isCorrectParam);
you can also use Matches...
serviceMock.Expect(x => x.CopyFile(Arg<Request>.Matches(r => r.FileName==null))).Throw(new Exception());

Caching WebAPI 2

EDIT: For each request, a new instance of controller is created. However, this is not true with Attribute classes. Once they are created, it is used for multiple requests. I hope it helps.
I wrote my own WebAPI (using latest version of WebAPI and .net framework) caching action filter. I am aware about CacheCow & this. However, i wanted mine anyways.
However, there is some issue with my code because i don't get exepected output when i use it in my project on live server. On local machine everything works fine.
I used below code in my blog RSS generator and i cache the data for each category. There are around 5 categories (food, tech, personal etc).
Issue: When i navigate to say api/GetTech it returns me the rss feed items from personal blog category. When i navigate to say api/GetPersonal , it returns me api/Food
I am not able to find the root cause but I think this is due to use of static method/variable. I have double checked that my _cachekey has unique value for each category of my blog.
Can someone point out any issues with this code esp when we have say 300 requests per minute ?
public class WebApiOutputCacheAttribute : ActionFilterAttribute
{
// Cache timespan
private readonly int _timespan;
// cache key
private string _cachekey;
// cache repository
private static readonly MemoryCache _webApiCache = MemoryCache.Default;
/// <summary>
/// Initializes a new instance of the <see cref="WebApiOutputCacheAttribute"/> class.
/// </summary>
/// <param name="timespan">The timespan in seconds.</param>
public WebApiOutputCacheAttribute(int timespan)
{
_timespan = timespan;
}
public override void OnActionExecuting(HttpActionContext ac)
{
if (ac != null)
{
_cachekey = ac.Request.RequestUri.PathAndQuery.ToUpperInvariant();
if (!_webApiCache.Contains(_cachekey)) return;
var val = (string)_webApiCache.Get(_cachekey);
if (val == null) return;
ac.Response = ac.Request.CreateResponse();
ac.Response.Content = new StringContent(val);
var contenttype = (MediaTypeHeaderValue)_webApiCache.Get("response-ct") ?? new MediaTypeHeaderValue("application/rss+xml");
ac.Response.Content.Headers.ContentType = contenttype;
}
else
{
throw new ArgumentNullException("ac");
}
}
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
if (_webApiCache.Contains(_cachekey)) return;
var body = actionExecutedContext.Response.Content.ReadAsStringAsync().Result;
if (actionExecutedContext.Response.StatusCode == HttpStatusCode.OK)
{
lock (WebApiCache)
{
_wbApiCache.Add(_cachekey, body, DateTime.Now.AddSeconds(_timespan));
_webApiCache.Add("response-ct", actionExecutedContext.Response.Content.Headers.ContentType, DateTimeOffset.UtcNow.AddSeconds(_timespan));
}
}
}
}
The same WebApiOutputCacheAttribute instance can be used to cache multiple simultaneous requests, so you should not store cache keys on the instance of the attribute. Instead, regenerate the cache key during each request / method override. The following attribute works to cache HTTP GET requests.
using System;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
using Newtonsoft.Json;
// based on strathweb implementation
// http://www.strathweb.com/2012/05/output-caching-in-asp-net-web-api/
public class CacheHttpGetAttribute : ActionFilterAttribute
{
public int Duration { get; set; }
public ILogExceptions ExceptionLogger { get; set; }
public IProvideCache CacheProvider { get; set; }
private bool IsCacheable(HttpRequestMessage request)
{
if (Duration < 1)
throw new InvalidOperationException("Duration must be greater than zero.");
// only cache for GET requests
return request.Method == HttpMethod.Get;
}
private CacheControlHeaderValue SetClientCache()
{
var cachecontrol = new CacheControlHeaderValue
{
MaxAge = TimeSpan.FromSeconds(Duration),
MustRevalidate = true,
};
return cachecontrol;
}
private static string GetServerCacheKey(HttpRequestMessage request)
{
var acceptHeaders = request.Headers.Accept;
var acceptHeader = acceptHeaders.Any() ? acceptHeaders.First().ToString() : "*/*";
return string.Join(":", new[]
{
request.RequestUri.AbsoluteUri,
acceptHeader,
});
}
private static string GetClientCacheKey(string serverCacheKey)
{
return string.Join(":", new[]
{
serverCacheKey,
"response-content-type",
});
}
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (actionContext == null) throw new ArgumentNullException("actionContext");
var request = actionContext.Request;
if (!IsCacheable(request)) return;
try
{
// do NOT store cache keys on this attribute because the same instance
// can be reused for multiple requests
var serverCacheKey = GetServerCacheKey(request);
var clientCacheKey = GetClientCacheKey(serverCacheKey);
if (CacheProvider.Contains(serverCacheKey))
{
var serverValue = CacheProvider.Get(serverCacheKey);
var clientValue = CacheProvider.Get(clientCacheKey);
if (serverValue == null) return;
var contentType = clientValue != null
? JsonConvert.DeserializeObject<MediaTypeHeaderValue>(clientValue.ToString())
: new MediaTypeHeaderValue(serverCacheKey.Substring(serverCacheKey.LastIndexOf(':') + 1));
actionContext.Response = actionContext.Request.CreateResponse();
// do not try to create a string content if the value is binary
actionContext.Response.Content = serverValue is byte[]
? new ByteArrayContent((byte[])serverValue)
: new StringContent(serverValue.ToString());
actionContext.Response.Content.Headers.ContentType = contentType;
actionContext.Response.Headers.CacheControl = SetClientCache();
}
}
catch (Exception ex)
{
ExceptionLogger.Log(ex);
}
}
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
try
{
var request = actionExecutedContext.Request;
// do NOT store cache keys on this attribute because the same instance
// can be reused for multiple requests
var serverCacheKey = GetServerCacheKey(request);
var clientCacheKey = GetClientCacheKey(serverCacheKey);
if (!CacheProvider.Contains(serverCacheKey))
{
var contentType = actionExecutedContext.Response.Content.Headers.ContentType;
object serverValue;
if (contentType.MediaType.StartsWith("image/"))
serverValue = actionExecutedContext.Response.Content.ReadAsByteArrayAsync().Result;
else
serverValue = actionExecutedContext.Response.Content.ReadAsStringAsync().Result;
var clientValue = JsonConvert.SerializeObject(
new
{
contentType.MediaType,
contentType.CharSet,
});
CacheProvider.Add(serverCacheKey, serverValue, new TimeSpan(0, 0, Duration));
CacheProvider.Add(clientCacheKey, clientValue, new TimeSpan(0, 0, Duration));
}
if (IsCacheable(actionExecutedContext.Request))
actionExecutedContext.ActionContext.Response.Headers.CacheControl = SetClientCache();
}
catch (Exception ex)
{
ExceptionLogger.Log(ex);
}
}
}
Just replace the CacheProvider with your MemoryCache.Default. In fact, the code above uses the same by default during development, and uses azure cache when deployed to a live server.
Even though your code resets the _cachekey instance field during each request, these attributes are not like controllers where a new one is created for each request. Instead, the attribute instance can be repurposed to service multiple simultaneous requests. So don't use an instance field to store it, regenerate it based on the request each and every time you need it.

How should I report a duplicated business entity/object on service layer?

I am trying to code with best practices and I have a doubt here. I am testing this on WebForms.
I have a UserService Layer where I have a method to pass a user to the RepositoryLayer:
public AddUserResponse AddUserResponse(AddUserRequest addUserRequest)
{
AddUserResponse response = new AddUserResponse();
User objUser = new User();
objUser.Names = addUserRequest.Names;
objUser.LastName = addUserRequest.LastName;
objUser.Email = addUserRequest.Email;
objUser.Alias = addUserRequest.Alias;
objUser.Profile.IdProfile = addUserRequest.Profile.IdProfile;
objUser.Password = addUserRequest.Password;
objUser.Active = addUserRequest.Active;
short OperationState=_userRepository.Add(objUser);
if (OperationState==0)
{
response.State=true;
response.Message="User inserted";
}
else if (OperationState==2)
{
response.State=false;
response.Message="Alias or Email already exist. Cannot insert User";
}
else
{
response.State=false;
response.Message="Error in User insertion";
}
return response;
}
Then I have a UserRepository Layer where I have a function that Adds a user comming from my service layer:
public short Add(User objUser)
{ ... return OperationState }
As showed this function relays on a stored procedure call to insert the user record. If the user email or alias doesn't exist then it inserts and returns 0, if it does returns 2, and if the operation fails returns 1.
I perform the checking and insertion in one call to save database round trips.
Am I performing the checking in a correct way on my service and repository classes?, or if am not, How should I abstract the logic to let the system determine when is a duplicated user?. Should I use the model or service to put the validation logic and raise a custom exception when that happens?
Thanks a lot for your insight.
UPDATE
For general interest I am posting now how I am implementing this on my App, once I get the Jason's IoC solution gonna make an update on this as well.
Model Class:
using ABC.DEF.Infrastructure.Domain;
namespace ABC.DEF.Model
{
public class AliasOrEmailAreUnique
{
private readonly IRepository<User, int> repository;
public AliasOrEmailAreUnique(IRepository<User,int> repository)
{
this.repository = repository;
}
//If the user is added has Id 0 so search among all the existing users for one that could have the alias or email registered already
//else if the user is being edit then search among all the user except the user with such Id(itself)
public bool IsBroken(User model)
{
if (model.IdUser == 0)
{
return (
repository.List().Where(x => x.Alias == model.Alias).Any()
|| repository.List().Where(x => x.Email == model.Email).Any()
);
}
else
{
return (
repository.List().Where(x => x.Alias == model.Alias && x.IdUser != model.IdUser).Any()
|| repository.List().Where(x => x.Email == model.Email && x.IdUser != model.IdUser).Any()
);
}
}
public ErrorMessage ErrorMessage
{
get { return new ErrorMessage { Property = "AliasEmail", Message = "Alias or Email exists already" }; }
}
}
}
Service Class:
using ABC.DEF.Repository;
using ABC.DEF.Model;
using ABC.DEF.Service.Messaging.User;
namespace ABC.DEF.Service
{
public class UsuarioService
{
public AddUserResponse AddUserResponse(AddUserRequest addUserRequest)
{
AddUserResponse response = new AddUserResponse();
User objUser = new User();
objUser.Names = addUserRequest.Names;
objUser.LastName = addUserRequest.LastName;
objUser.Email = addUserRequest.Email;
objUser.Alias = addUserRequest.Alias;
objUser.Profile.IdProfile = addUserRequest.Profile.IdProfile;
objUser.Password = addUserRequest.Password;
objUser.Active = addUserRequest.Active;
//Determine if the Alias or Email are unique
Model.AliasOrEmailAreUnique aliasOrEmailAreUnique = new Model.AliasOrEmailAreUnique(_userRepository);
if (!aliasOrEmailAreUnique.IsBroken(objUser))
{
_usuarioRepository.Add(objUser);
response.State = true;
response.Message = "User added succesfully";
}
else
{
response.State = false;
response.Message = aliasOrEmailAreUnique.ErrorMessage.Message;
}
return response;
}
}
}
I like to validate the input at the beginning of a unit of work. For a web application the request is the unit of work. before the controller action is fired I validate the user input. the action itself is the "happy path". if it makes it this far I know my operation will succeed. at the end the request (the response) I commit any changes back to the database.
I also like to keep my operation explicit so a call to add an entity would be different than call to edit an entity vs. deleting an entity.
in your scenario you have a service layer rather than controller actions, but the process is still the same. validate the model before calling the service layer. then pass the model to the service layer to preform what operations you want.
...UPDATE 1...
in response to your comment below.....
I have been calling my repositories only in the service layer
Becareful not to fall into the trap of thinking there is a linear patterns for making calls. through the application. instead think of it as an onion or sphere with multiple layers.
The model is just a POCO/DTO. there would be other components responsible for validating the model. typically I have a business rules engine that looks something like this... written off the top of my head.
interface IRule<T>
{
bool IsBroken(T model);
ErrorMessage Message {get;}
}
interface IRulesEngine
{
IEnumerable<ErrorMessage> Validate<T>(T model);
}
class ErrorMessage
{
public string Property {get;set;}
public string Message {get;set;}
}
class RulesEngine : IRulesEngine
{
private readonly IContainer container;
public RulesEngine(IContainer container)
{
this.container = container;
}
public IEnumerable<ErrorMessage> Validate<T>(T model)
{
return container
.GetInstances<IRule<T>>()
.Where(rule => rule.IsBroken(model))
.Select(rule => rule.Message);
}
}
this implementation assumes an IoC container, but can be implemented without one. The a rule may look like this
class NameIsUnique<MyClass> : IRule<MyClass>
{
private readonly IRepository<TheEntity> repository;
public NameIsUnique<MyClass>(IRepository<TheEntity> repository)
{
this.repository = repository;
}
public bool IsBroken(MyClass model)
{
return repository.Where(x => x.Name == model.Name).Any();
}
public ErrorMessage
{
get { return new ErrorMessage { Property = "Name", Message = "Name is not unique" }; }
}
}
finally, how this can be used.
var errors = engine.Validate(model);
LoadErrorsInToView(errors);
if(errors.Any()) return;
//call service to process the happy path...
...UPDATE 2...
first we refactor our interfaces
//this is just a marker interface. don't directly imeplement this.
interface IRule
{
}
interface IRule<T> : IRule
{
bool IsBroken(T model);
ErrorMessage Message {get;}
}
class RulesEngine : IRulesEngine
{
public reasdonly ICollection<IRule> Rules = new List<IRule>();
public IEnumerable<ErrorMessage> Validate<T>(T model)
{
return Rules
.Where(x => typeof(IRule<T>).IsAssignableFrom(x.GetType()))
.Cast<IRule<T>>()
.Where(rule => rule.IsBroken(model))
.Select(rule => rule.Message);
}
}

Validating model properties WCF Web APi

I have a set of services hosted with WCF Web Api, what I need to do is validate the properties inside the models of the app.
In MVC 3 for example I decorate properties in the model like this:
[StringLength(30)]
public string UserName { get; set; }
and then in the controller I proceed like this to verify os the model has met the validation parameters:
[HttpPost]
ActionResult Create(Model myModel)
{
if(ModelState.IsValid(){
Post the model
}
else
{
Don't post the model
}
}
Is there a way to do something similar in WCF Web Api?
Ok I finally managed to get validations for my models working. I wrote a validation handler and a couple of extensions methods. First thing the validation handler:
public class ValidationHandler<T> : HttpOperationHandler
{
private readonly HttpOperationDescription _httpOperationDescription;
public ValidationHandler(HttpOperationDescription httpOperationDescription)
{
_httpOperationDescription = httpOperationDescription;
}
protected override IEnumerable<HttpParameter> OnGetInputParameters()
{
return _httpOperationDescription.InputParameters
.Where(prm => prm.ParameterType == typeof(T));
}
protected override IEnumerable<HttpParameter> OnGetOutputParameters()
{
return _httpOperationDescription.InputParameters
.Where(prm => prm.ParameterType == typeof(T));
}
protected override object[] OnHandle(object[] input)
{
var model = input[0];
var validationResults = new List<ValidationResult>();
var context = new ValidationContext(model, null, null);
Validator.TryValidateObject(model, context, validationResults,true);
if (validationResults.Count == 0)
{
return input;
}
else
{
var response = new HttpResponseMessage()
{
Content = new StringContent("Model Error"),
StatusCode = HttpStatusCode.BadRequest
};
throw new HttpResponseException(response);
}
}
}
Notice how the Handler receives a T object, this is mainly because I would like to validate all the model types within the API. So the OnGetInputParameters specifies that the handler needs to receive a T type object, and the OnGetOutputParameters specifies that the handler needs to return an object with the same T type in case validations policies are met, if not, see how the on handle method throws an exception letting the client know that there's been a validation problem.
Now I need to register the handler, for this I wrote a couple of extensions method, following an example of a Pedro Felix's blog http://pfelix.wordpress.com/2011/09/24/wcf-web-apicustom-parameter-conversion/ (this blog helped me a lot, there are some nice explanations about the whole handler operations thing). So these are the extensions methods:
public static WebApiConfiguration ModelValidationFor<T>(this WebApiConfiguration conf)
{
conf.AddRequestHandlers((coll, ep, desc) =>
{
if (desc.InputParameters.Any(p => p.ParameterType == typeof(T)))
{
coll.Add(new ValidationHandler<T>(desc));
}
});
return conf;
}
so this methos checks if there is a T type parameter in the operations, and if so, it adds the handler to that specific operation.
This one calls the other extension method AddRequestHandler, and that method add the new handler without removing the previous registered ones, if the exist.
public static WebApiConfiguration AddRequestHandlers(
this WebApiConfiguration conf,
Action<Collection<HttpOperationHandler>,ServiceEndpoint,HttpOperationDescription> requestHandlerDelegate)
{
var old = conf.RequestHandlers;
conf.RequestHandlers = old == null ? requestHandlerDelegate :
(coll, ep, desc) =>
{
old(coll, ep, desc);
};
return conf;
}
The last thing is to register the handler:
var config = new WebApiConfiguration();
config.ModelValidationFor<T>(); //Instead of passing a T object pass the object you want to validate
routes.SetDefaultHttpConfiguration(config);
routes.MapServiceRoute<YourResourceObject>("SomeRoute");
So this is it.. Hope it helps somebody else!!
I am currently working on a HttpOperationHandler that does exactly what you need. It's not done by now, but this psuedo code might give you an idea of how you can do it.
public class ValidationHandler : HttpOperationHandler
{
private readonly HttpOperationDescription _httpOperationDescription;
private readonly Uri _baseAddress;
public ValidationHandler(HttpOperationDescription httpOperationDescription, Uri baseAddress)
{
_httpOperationDescription = httpOperationDescription;
_baseAddress = baseAddress;
}
protected override IEnumerable<HttpParameter> OnGetInputParameters()
{
return new[] { HttpParameter.RequestMessage };
}
protected override IEnumerable<HttpParameter> OnGetOutputParameters()
{
var types = _httpOperationDescription.InputParameters.Select(x => x.ParameterType);
return types.Select(type => new HttpParameter(type.Name, type));
}
protected override object[] OnHandle(object[] input)
{
var request = (HttpRequestMessage)input[0];
var uriTemplate = _httpOperationDescription.GetUriTemplate();
var uriTemplateMatch = uriTemplate.Match(_baseAddress, request.RequestUri);
var validationResults = new List<ValidationResult>();
//Bind the values from uriTemplateMatch.BoundVariables to a model
//Do the validation with Validator.TryValidateObject and add the results to validationResults
//Throw a exception with BadRequest http status code and add the validationResults to the message
//Return an object array with instances of the types returned from the OnGetOutputParmeters with the bounded values
}
}
The OnGetInputParameters value tells what's expected into the OnHandle method, and the OnGetOutputParameters tells what's the expected output from the OnHandle method (which later on is injected into the method in the service).
You can then add the handler to the routing with a HttpConfiguration as follows:
var httpConfiguration = new HttpConfiguration
{
RequestHandlers = (collection, endpoint, operation) => collection.Add(new ValidationHandler(operation, endpoint.Address.Uri))
};
RouteTable.Routes.MapServiceRoute<MyResource>("MyResource", httpConfiguration);
There is an example of this posted on MSDN of creating a behavior for this that should work. You could also call the validators manually with Validator.ValidateObject (or wrap it as an extension method) and return the validation errors, which is essentially what that behavior is doing.
Firstly I should say awesome question+answer Daniel
However, I've taken it a little further, refined it and added to it.
ValidationHander
I've refined this a little. It is now based on a generic HttpOperationHandler so it can take the HttpRequestMessage. The reason for this is so that I can return error messages formatted using the correct media type (from the accept header).
public class ValidationHandler<TResource> : HttpOperationHandler<TResource, HttpRequestMessage, HttpRequestMessage>
{
public ValidationHandler() : base("response") { }
protected override HttpRequestMessage OnHandle(TResource model, HttpRequestMessage requestMessage)
{
var results = new List<ValidationResult>();
var context = new ValidationContext(model, null, null);
Validator.TryValidateObject(model, context, results, true);
if (results.Count == 0)
{
return requestMessage;
}
var errorMessages = results.Select(x => x.ErrorMessage).ToArray();
var mediaType = requestMessage.Headers.Accept.FirstOrDefault();
var response = new RestValidationFailure(errorMessages);
if (mediaType != null)
{
response.Content = new ObjectContent(typeof (string[]), errorMessages, mediaType);
}
throw new HttpResponseException(response);
}
}
Extension Methods
The 2 you provided stay virtually the same about from the desc paramter no longer being needed when adding the ValidationHandler in the ModelValidationFor method
I've added an extra extension method. This is to make sure that all "Resource" classes are validated. This is mainly me being lazy and forgetful. I am forever forgetting to add some class to a list somewhere. (It's why I write generic windsor installers!)
public static void ValidateAllResourceTypes(this WebApiConfiguration config, string assemblyFilter = "MyCompany*.dll")
{
var path = Path.GetDirectoryName((new Uri(Assembly.GetExecutingAssembly().CodeBase)).AbsolutePath);
var dc = new DirectoryCatalog(path, assemblyFilter);
var assemblies = dc.LoadedFiles.Select(Assembly.LoadFrom).ToList();
assemblies.ForEach(assembly =>
{
var resourceTypes = assembly.GetTypes()
.Where(t => t.Namespace != null && t.Namespace.EndsWith("Resources"));
foreach (var resourceType in resourceTypes)
{
var configType = typeof(Extensions);
var mi = configType.GetMethod("ModelValidationFor");
var mi2 = mi.MakeGenericMethod(resourceType);
mi2.Invoke(null, new object[] { config });
}
});
}
I made use of the System.ComponentModel.Composition.Hosting namespace (formerly known as MEF) for the DirectoryCatalog class. In this case I've just used the namespace ending with "Resources" to find my "Resource" classes. It wouldn't take much work to change it to use a custom attribute or whatever other way you might prefer to identify which classes are your "Resources".
RestValidationFailure
This is a little helper class I made to allow consistent behaviour for validation failure responses.
public class RestValidationFailure : HttpResponseMessage
{
public RestValidationFailure(string[] messages)
{
StatusCode = HttpStatusCode.BadRequest;
foreach (var errorMessage in messages)
{
Headers.Add("X-Validation-Error", errorMessage);
}
}
}
So, now I get a nice list (in my preferred mediatype) of all the validation errors.
Enjoy! :)

Categories

Resources