DelegateEndpointDefinition IsTemporary - masstransit

I have a simple ConsumerDefinition:
public class HealthCheckConsumerDefinition : ConsumerDefinition<LoopbackConsumer>
{
public HealthCheckConsumerDefinition(IOptions<HealthCheckOptions> options)
{
EndpointName = options.Value.HostName;
Endpoint(configurator => configurator.Temporary = true); // not work
}
protected override void ConfigureConsumer(IReceiveEndpointConfigurator endpointConfigurator, IConsumerConfigurator<LoopbackConsumer> consumerConfigurator)
{
endpointConfigurator.ConfigureConsumeTopology = false;
base.ConfigureConsumer(endpointConfigurator, consumerConfigurator);
}
}
I expect the Temporary=true property to configure an endpoint with the AutoDelete = true and Durable = false properties, because this code is in the ApplyEndpointDefinition method of the RabbitMqHostConfiguration class:
https://github.com/MassTransit/MassTransit/blob/5fb6b4e31582970b0571e9fe6ac77793a0b3242a/src/MassTransit.RabbitMqTransport/Configuration/Configuration/RabbitMqHostConfiguration.cs#L60
public void ApplyEndpointDefinition(IRabbitMqReceiveEndpointConfigurator configurator, IEndpointDefinition definition)
{
if (definition.IsTemporary)
{
configurator.AutoDelete = true;
configurator.Durable = false;
}
...
}
But in the ConfigureEndpoints method of the class, the ConsumerDefinition registry is converted to the DelegateEndpointDefinition, which does not override the IsTemporary property based on the ConsumerDefinition object passed to the constructor:
https://github.com/MassTransit/MassTransit/blob/89ba77036230a15be108e8ade3a0e6fe5309a94d/src/MassTransit/Configuration/Registration/Registration.cs#L178
How to get around this problem and declare Temporary endpoint within the ConsumerDefinition?

I have confirmed that this works as expected in the upcoming MassTransit v7 release. Using this syntax, the temporary flag is now passed through to the transport.
Note that this would be overridden by using .Endpoint() following the AddConsumer<T>() container configuration method.
class RequestConsumerDefinition :
ConsumerDefinition<RequestConsumer>
{
public RequestConsumerDefinition()
{
Endpoint(e => e.Temporary = true);
}
}

Related

Disable Wrapping of Controller Results

I am currently using v3.2.5 of Abp.AspNetCore.
I am trying to integrate an Alpha package of Microsoft.AspNetCore.OData into the project which is so far looking ok.
However when i try and query the metadata controller http://localhost:51078/odata/v1/$metadata the result is wrapped.
Now this was an issue for the ODataControllers as well, but i could simply add
the [DontWrapResult] attribute.
I dont have direct access to the MetadataController so i am unable to add the attribute. Is there anyway to disable wrapping for an Abp project?
Thanks
Edit
Here is the current ConfigureServices method
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services
.AddMvc()
.AddJsonOptions(options => { options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; });
services
.AddAuthentication()
.AddCsDeviceAuth(options => { });
services
.AddOData();
//Configure Abp and Dependency Injection
var provider = services.AddAbp<PortalWebODataModule>(options =>
{
//Configure Log4Net logging
options.IocManager.IocContainer.AddFacility<LoggingFacility>(
f => f.LogUsing<Log4NetLoggerFactory>().WithConfig("log4net.config")
);
});
services.Configure<MvcOptions>(options =>
{
var abpResultFilter = options.Filters.First(f => f is AbpResultFilter);
options.Filters.Remove(abpResultFilter);
options.Filters.AddService(typeof(ODataResultFilter));
});
return provider;
}
You can implement IResultFilter and set WrapOnSuccess to false:
public class ResultFilter : IResultFilter, ITransientDependency
{
private readonly IAbpAspNetCoreConfiguration _configuration;
public ResultFilter(IAbpAspNetCoreConfiguration configuration)
{
_configuration = configuration;
}
public void OnResultExecuting(ResultExecutingContext context)
{
if (context.HttpContext.Request.Path.Value.Contains("odata"))
{
var methodInfo = context.ActionDescriptor.GetMethodInfo();
var wrapResultAttribute =
GetSingleAttributeOfMemberOrDeclaringTypeOrDefault(
methodInfo,
_configuration.DefaultWrapResultAttribute
);
wrapResultAttribute.WrapOnSuccess = false;
}
}
public void OnResultExecuted(ResultExecutedContext context)
{
// No action
}
private TAttribute GetSingleAttributeOfMemberOrDeclaringTypeOrDefault<TAttribute>(MemberInfo memberInfo, TAttribute defaultValue = default(TAttribute), bool inherit = true)
where TAttribute : class
{
return memberInfo.GetCustomAttributes(true).OfType<TAttribute>().FirstOrDefault()
?? memberInfo.DeclaringType?.GetTypeInfo().GetCustomAttributes(true).OfType<TAttribute>().FirstOrDefault()
?? defaultValue;
}
}
Then, in Startup class, add the filter in ConfigureServices method:
services.AddMvc(options =>
{
options.Filters.AddService(typeof(ResultFilter));
});
References:
AbpResultFilter.OnResultExecuting
ReflectionHelper.GetSingleAttributeOfMemberOrDeclaringTypeOrDefault
Alternative solution; to completely disable WrapResult behavior within the system ( at the Core module registration):
var abpAspNetCoreConfiguration = Configuration.Modules.AbpAspNetCore();
abpAspNetCoreConfiguration.DefaultWrapResultAttribute.WrapOnSuccess = false;
abpAspNetCoreConfiguration.DefaultWrapResultAttribute.WrapOnError = false;
abpAspNetCoreConfiguration
.CreateControllersForAppServices(
typeof(AccessApplicationModule).GetAssembly()
);
WrapOnSuccess and WrapOnError flags can be set to false values.
ABP v6.5 and later
Implement IWrapResultFilter and add it to WrapResultFilters in the module's PreInitialize method.
See https://stackoverflow.com/questions/70947461/how-to-control-response-wrapping-in-abp-on-a-per-route-basis/70955045#70955045 for more details.
Before ABP v6.5
...including ABP v3.2.5 mentioned in the question.
Subclass AbpResultFilter:
using Abp.AspNetCore.Configuration;
using Abp.AspNetCore.Mvc.Results;
using Abp.AspNetCore.Mvc.Results.Wrapping;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
namespace AbpODataDemo.Web.Host.Filters
{
public class ODataResultFilter : AbpResultFilter
{
public ODataResultFilter(IAbpAspNetCoreConfiguration configuration, IAbpActionResultWrapperFactory actionResultWrapperFactory)
: base(configuration, actionResultWrapperFactory)
{
}
public override void OnResultExecuting(ResultExecutingContext context)
{
if (context.HttpContext.Request.Path.Value.StartsWith("/odata", StringComparison.InvariantCultureIgnoreCase))
{
return;
}
base.OnResultExecuting(context);
}
}
}
Replace AbpResultFilter with it in the Startup ConfigureServices method:
services.PostConfigure<MvcOptions>(options =>
{
var index = options.Filters.IndexOf(new ServiceFilterAttribute(typeof(AbpResultFilter)));
if (index != -1)
{
options.Filters.RemoveAt(index);
options.Filters.Insert(index, new ServiceFilterAttribute(typeof(ODataResultFilter)));
}
});
Reference: https://github.com/aspnetboilerplate/sample-odata/pull/16

Nest 2.x - Custom JsonConverter

I want to use the IsoDateTimeConverter from Newtonsoft to format the json version of my DateTime properties.
However, I cant figure out how this is done in Nest 2.x.
Here is my code:
var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var settings = new ConnectionSettings(connectionPool, s => new MyJsonNetSerializer(s));
var client = new ElasticClient(settings);
public class MyJsonNetSerializer : JsonNetSerializer
{
public MyJsonNetSerializer(IConnectionSettingsValues settings) : base(settings) { }
protected override void ModifyJsonSerializerSettings(JsonSerializerSettings settings)
{
settings.NullValueHandling = NullValueHandling.Ignore;
}
protected override IList<Func<Type, JsonConverter>> ContractConverters => new List<Func<Type, JsonConverter>>()
{
type => new Newtonsoft.Json.Converters.IsoDateTimeConverter()
};
}
I'm getting this exception:
message: "An error has occurred.",
exceptionMessage: "Unexpected value when converting date. Expected DateTime or DateTimeOffset, got Nest.SearchDescriptor`1[TestProject.DemoProduct].",
exceptionType: "Elasticsearch.Net.UnexpectedElasticsearchClientException"
Any help is appreciated
with the Func<Type, JsonConverter>, you need to check that the type is the right one for the converter that you want to register; if it is, return the converter instance, otherwise return null
public class MyJsonNetSerializer : JsonNetSerializer
{
public MyJsonNetSerializer(IConnectionSettingsValues settings) : base(settings) { }
protected override void ModifyJsonSerializerSettings(JsonSerializerSettings settings)
{
settings.NullValueHandling = NullValueHandling.Ignore;
}
protected override IList<Func<Type, JsonConverter>> ContractConverters => new List<Func<Type, JsonConverter>>()
{
type =>
{
return type == typeof(DateTime) ||
type == typeof(DateTimeOffset) ||
type == typeof(DateTime?) ||
type == typeof(DateTimeOffset?)
? new Newtonsoft.Json.Converters.IsoDateTimeConverter()
: null;
}
};
}
NEST uses the IsoDateTimeConverter for those types by default, so you won't need to register a converter for them unless you would like to change other settings on the converter.

Get the api controllers constructor value within an AuthorizeFilter

When the user is authenticated I want to prevent that he updates/deletes/reads data created from other accounts... by telling him you do not have the permission 403!
What is the best way to get an instance of the ISchoolyearService to invoke its HasUserPermission() method?
I know I could new up the SchoolyearService here but that would defeat the reason using an IoContainer at all in my app.
public class UserActionsSchoolyearAuthorizationFilter : AuthorizationFilterAttribute
{
public override void OnAuthorization(HttpActionContext actionContext)
{
if (actionContext != null)
{
bool canUserExecuteAction = false;
if (actionContext.Request.Method == HttpMethod.Put)
{
int schoolyearId = Convert.ToInt32(actionContext.Request.GetRouteData().Values["Id"]);
int userId = actionContext.Request.Content.ReadAsAsync<SchoolyearEditRequest>().Result.Schoolyear.UserId;
//var schoolyearService = actionContext.ControllerContext.Controller.GetContstructorParameterServiceInstance();
//canUserExecuteAction = schoolyearService.HasUserPermission(userId, schoolyearId);
if (canUserExecuteAction)
{
base.OnAuthorization(actionContext);
}
else
{
actionContext.Response = new HttpResponseMessage(HttpStatusCode.Forbidden);
}
}
// Removed for brevity
private readonly ISchoolyearService _service;
public SchoolyearController(ISchoolyearService service)
{
_service = service;
}
If you made the _service parameter public on your SchoolyearController you could try something like this in the OnAuthorization method:
var schoolyearController = actionContext.ControllerContext.Controller as SchoolyearController;
canUserExecuteAction = schoolyearController._service.HasUserPermission(userId, schoolyearId);
Ok finally I found it out how to get the ISchoolyearService from the current request:
Grab the registered service from the DependencyScope!
Now this Attribute should be put on the controller directly. Its not needed to put it on the action due to the if/else on the http verbs which I do.
bool canUserExecuteAction = false;
if (actionContext.Request.Method == HttpMethod.Put)
{
int targetId = Convert.ToInt32(actionContext.Request.GetRouteData().Values["Id"]);
int userId = actionContext.Request.Content.ReadAsAsync<SchoolyearEditRequest>().Result.Schoolyear.UserId;
var requstScope = actionContext.ControllerContext.Request.GetDependencyScope();
var service = requstScope.GetService(typeof(ISchoolyearService)) as ISchoolyearService;
canUserExecuteAction = service.HasUserPermission(userId, targetId);
if (canUserExecuteAction)
{
base.OnAuthorization(actionContext);
}
else
{
actionContext.Response = new HttpResponseMessage(HttpStatusCode.Forbidden);
}
}

DI with parameters in Castle Windsor

I'm trying to resolve a dependency like this:
controller.ActionInvoker = kernel.Resolve<IActionInvoker>(controller.GetType());
It was previously registered in this way:
container.Register(
Component
.For<IActionInvoker>()
.ImplementedBy<WindsorActionInvoker>()
.UsingFactoryMethod(metho)
.LifestylePerWebRequest()
);
internal IActionInvoker metho(IKernel kernel,ComponentModel model, CreationContext context)
{
// here just for debugging and watching the variables in the factory method,
// I would instance WindsorActionInvoker passing the filters to inject.
throw new InvalidOperationException();
}
But I can't figure out how to get the parameter I passed to the resolve call in the factory method.
I need the Type I'm passing as parameter to pass it to one of the dependencies injected into the constructor of the concrete type.
What am I doing wrong?
If you must know, the purpose of this is to inject action filters directly into the action invoker (and therefore the controllers), instead of requiring them decorate a controller or the base controller, additionally, this lets me to inject parameters dynamically, which I can't do with attributes.
public class WindsorActionInvoker : ControllerActionInvoker
{
private readonly IList<IActionFilter> actionFilters;
private readonly IList<IAuthorizationFilter> authorizationFilters;
private readonly IList<IExceptionFilter> exceptionFilters;
private readonly IList<IResultFilter> resultFilters;
public WindsorActionInvoker(IList<IActionFilter> actionFilters, IList<IAuthorizationFilter> authorizationFilters, IList<IExceptionFilter> exceptionFilters, IList<IResultFilter> resultFilters)
{
if (actionFilters == null)
{
throw new ArgumentNullException("actionFilters");
}
if (authorizationFilters == null)
{
throw new ArgumentNullException("authorizationFilters");
}
if (exceptionFilters == null)
{
throw new ArgumentNullException("exceptionFilters");
}
if (resultFilters == null)
{
throw new ArgumentNullException("resultFilters");
}
this.actionFilters = actionFilters;
this.authorizationFilters = authorizationFilters;
this.exceptionFilters = exceptionFilters;
this.resultFilters = resultFilters;
}
protected override FilterInfo GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
{
FilterInfo filterInfo = base.GetFilters(controllerContext, actionDescriptor);
foreach (IActionFilter filter in actionFilters)
{
filterInfo.ActionFilters.Add(filter);
}
foreach (IAuthorizationFilter filter in authorizationFilters)
{
filterInfo.AuthorizationFilters.Add(filter);
}
foreach (IExceptionFilter filter in exceptionFilters)
{
filterInfo.ExceptionFilters.Add(filter);
}
foreach (IResultFilter filter in resultFilters)
{
filterInfo.ResultFilters.Add(filter);
}
return filterInfo;
}
}
Solved, I needed to pass either a dictionary or an anonymous type instead of just any object.
Replacing:
controller.ActionInvoker = kernel.Resolve<IActionInvoker>(controller.GetType());}
With
controller.ActionInvoker = kernel.Resolve<IActionInvoker>(new { loggerType = controller.GetType() });
Fixed it.
:)

.NET MVC 3 Programmatically set layout

In a .NET Razor Web Application i'm trying to programmatically set the Layout. I can not use _ViewStart.cshtml and don't wont to set the #{ Layout = "..." } on every page. This is what I have come up with:
A base WebViewPage class:
public abstract class SitePage<T> : System.Web.Mvc.WebViewPage<T>
{
private object _layout;
public new dynamic Layout { get { return _layout; } }
public override void InitHelpers()
{
base.InitHelpers();
_layout = "~/Themes/" + Settings.Theme + "/Views/_Layout.cshtml";
}
}
And in the application web.config I specify all view to use this base page. But the Layout is never used it seems. What could be wrong here?
The WebViewPage class inherits from WebPageBase that has a property named Layout like:
public override string Layout { get; set; }
You can override the Layout property, or change your _layout logic to achieve your purpose. For example:
public abstract class SitePage<T> : System.Web.Mvc.WebViewPage<T> {
// set this modifier as protected, to make it accessible from view-pages
protected string _layout{
get {
return base.Layout;
}
set {
base.Layout = value;
}
}
public override void InitHelpers() {
base.InitHelpers();
_layout = "~/Themes/" + Settings.Theme + "/Views/_Layout.cshtml";
}
}
and/or in a view-page, you can set it too:
#{
_layout = "_Your_Special_Layout.cshtml";
}
UPDATE: using a flag to avoid stack-over-flow in assigning _layout more that once:
public abstract class SitePage<T> : System.Web.Mvc.WebViewPage<T> {
public bool LayoutAssigned {
get {
return (ViewBag.LayoutAssigned == true);
}
set {
ViewBag.LayoutAssigned = value;
}
}
// set this modifier as protected, to make it accessible from view-pages
protected string _layout{
get {
return base.Layout;
}
set {
base.Layout = value;
}
}
public override void InitHelpers() {
base.InitHelpers();
if(!LayoutAssigned) {
_layout = "~/Themes/" + Settings.Theme + "/Views/_Layout.cshtml";
LayoutAssigned = true;
}
}
}
I tried to achieve the same just now by implementing a custom WebViewPage, however changing WebViewPage.Layout within my custom class didn't have any effect (as you have also discovered).
Eventually I ended up changing my _ViewStart.cshtml to have this code:
#{
this.Layout = this.Request.QueryString["print"] == "1"
? "~/Views/Layout/_Print.cshtml"
: "~/Views/Layout/_Layout.cshtml";
}
It might not be implemented how you wanted it, but it certainly does keep things dry and that is the main point.

Resources