How to add mediatr in .NET 6? - mediatr

In .NET Core, we can add it in ConfigureServices method
services.AddMediatR(typeof(Startup));
But in .NET 6, there is only Program.cs. How to add mediatr in .NET 6?
Tried with this code but got build error

builder.Services.AddMediatR(Assembly.GetExecutingAssembly());
Edit (Explanation): The AddMediatR extension method needs an addembly to scan so it can register all the handlers and mediator types. In previous versions of dotnet we used the typeof(Startup) to point to the assembly of our asp project. We can always do the same thing instead of getting the executing assembly by creating an interface in our asp project, which can also be helpful at testing. Just create an empty interface with a meaningful name, for example something like IProjectNameMarker and then you can use typeof(IProjectNameMarker).

The above answer is correct, but you could also do
builder.Services.AddMediatR(typeof(Program));

Related

ASP.NET Core MVC controllers in separate assembly

I'm using ASP.NET MVC Core RC-2. I have a web project targeting the full .NET framework. I also have a separate class library in the solution, also targeting the full framework.
In the class library, I have a controller, marked with a route attribute. I have referenced the class library from the web project. This assembly references the nuget package Microsoft.AspNetCore.Mvc v. 1.0.0-rc2-final.
It was my understanding that this external controller would be discovered automatically, e.g.
http://www.strathweb.com/2015/04/asp-net-mvc-6-discovers-controllers/
However this doesn't work for me- I browse to the URL of the route and I get a blank page and it doesn't hit my controller breakpoint.
Any ideas how to get this working?
Interestingly, it does seem to work for web projects targeting .NET Core Framework, referencing a class library also targeting .NET Core. But not for a web project targeting the full framework, referencing a standard .NET class library.
Note: this is MVC Core which is supposed to support this kind of scenario without any MVC<=4 routing overrides.
Still an issue in ASP.Net Core 1.0, not sure if it's by design now. Easiest solution is to do this in Startup.cs/ConfigureServices
services.AddMvc()
.AddApplicationPart(typeof(<class in external assembly>).Assembly)
.AddControllersAsServices();
AddApplicationPart explicitly includes the assembly in searches for controllers.
The call to AddControllersAsServices() will add all the discovered controllers into the services collection, and if you put a breakpoint after this line and inspect 'services', you will see in the collection all the controller types which have been found.
You might also want to check here: https://docs.asp.net/en/latest/migration/rc1-to-rtm.html#asp-net-5-mvc-compile-views as the discovery rules are now changed for controllers from RC1.
Also remember to use IActionResult instead of ActionResult!
I believe you are hitting the following known issue in RC2.
https://github.com/aspnet/Mvc/issues/4674 (workaround is mentioned in the bug)
This has been fixed since then but will only be available in next release (unless you are ok with using nightly builds)

MvvmCross with two core libraries

Is it possible to link two Core libraries into your app?
I would like to create one Common.Core library that has login and account view models.
I would like another one Domain.Core library that has some domain view models in it.
These could be used across a couple different projects.
In my app, I do a new Setup().Initialize().
My Setup class overrides CreateApp() ...
public class Setup : MvxPhoneSetup
{
protected override IMvxApplication CreateApp()
{
CreatableTypes().EndingWith("Service").AsInterfaces().RegisterAsLazySingleton();
return new Common.Core.App();
// TODO: can I setup a Domain.Core library here too?
}
}
I have tried doing this ...
CreatableTypes(Assembly.Load("Domain.Core")).EndingWith("Service").AsInterfaces().RegisterAsLazySingleton();
but I'm getting a ReflectionTypeLoadException when I try to resolve a domain model from there.
Anyone tried something like this?
Thanks!
Yes, using multiple 'core' projects should work.
The ReflectionTypeLoadException occurring on Resolve suggests that maybe your second Assembly requires another Assembly that isn't available? Do you get the same problem with a very simple second core project? Can you get any more information about the exception? Which platform is this occurring on?
If you want to load ViewModel types from multiple assemblies, then there is a Setup method you can override - The default ViewModelLocator in MvvmCross gets its list of ViewModels from the assemblies listed in Setup - see MvxSetup.cs
(Sorry this list is in the ui project - should really be in the main core project)
For cross-platform compatibility, I don't recommend using Assembly.Load - better to use a more static method like typeof(Domain.Core.Something).Assembly
Working on 'packaged application' platforms like xamarin.android and (especially) xamarin.ios I don't recommend using Assembly.Load - this will only work on the iOS platforms if the assembly is referenced statically and has already been loaded - that's the reason plugins have a special bootstrap file on iOS. Also be aware that the name used in Assembly.Load is different on different platforms - eg in Android you must use the filename ending in .dll - see MvxAndroidSetup.cs. For other platforms like WP and winRT, then Assembly.Load may work more conventionally though - although I've personally spent hours/days/weeks swearing at this sort of code in the last year.

Could not load file or assembly Microsoft.Expression.Interactions and GalaSoft.MvvmLight.Extras.WPF4

I am facing unknown problem while using Prism 4. We implement business solution in MVVM pattern. For that I have used
1. Microsoft.Expression.Interactions
2. System.Windows.Interactivity
3. GalaSoft.MvvmLight.Extras.WPF4
for Interaction.Triggers, CallMethodAction and EventToCommand.
<SWI:Interaction.Triggers>
<SWI:EventTrigger SourceObject="{Binding ElementName=txtName}" EventName="GotFocus">
<MEI:CallMethodAction TargetObject="{Binding Path=DataContext,ElementName=txtName}" MethodName="txtName_GotFocus"></MEI:CallMethodAction>
</SWI:EventTrigger>
<SWI:EventTrigger SourceObject="{Binding ElementName=txtName}" EventName="KeyUp">
<GMEWCMD:EventToCommand Command="{Binding Path=DataContext.cmdtxtNameKeyUpEvent,ElementName=txtName}" PassEventArgsToCommand="True">
</GMEWCMD:EventToCommand>
</SWI:EventTrigger>
</SWI:Interaction.Triggers>
Everything is working fine but now we required to make whole system loosely couple we choose prism 4. We already achieved this but above code create following problem as below.
Could not load file or assembly 'Microsoft.Expression.Interactions, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
If I comment above code everything is working fine. I don't know what an issue. Does PRISM 4 not support above assemblies?
Please help us to get rid from this problem.
Thank you
This commonly happens to me when I reference both Microsoft.Expression.Interactions and System.Windows.Interactivity in a view that belongs to a module, but I fail to also add those references to the project that acts as the shell.
I had a similar problem with Prism in Visual Studio 2012. The XAML editor would fail with the same exception.
The solution was to install Microsoft Expression Blend SDK. The assembly should be part of SDK. Allegedly, it is necessary for binding between control events and commands in XAML and some code may require it.
I had the same problem during a Prism 6.1 composite app using .NET 4.5.2, where i was referencing in one of the modules (class library) the Microsoft.Expression.Interactions & System.Windows.Interactivity.
Eventually i had to reference the same dlls in the Desktop project (where bootstrapper and Shell belong)

ASP.NET MVC 3.0 - Why strong-typing Model in a View, if static compilation is not happening?

I'm actively developing desktop applications, local and network services, some classic ASP.NET, etc., so I'm used to static compilation and static code analysis. Now that I'm (finally) learning ASP.NET MVC 3.0 I'm seeing that many of the ASP.NET MVC experts and experienced developers are recommending using strongly-typed views in ASP.NET MVC 3.0 (where applicable).
I'm guessing that "strongly-typed" means writing #model=... at the top of a view's code. But in doing that I only get IntelliSense to work, no static code checking is taking place. I can write anything I want in the #model statement in cshtml and it would compile and run. Consequentially, Model.Anything also compiles. In fact, if I don't type #model I can dynamically use whatever model I want that has "compatible" properties and methods.
I'm used to "strongly-typed" meaning "won't compile", like LINQ to whatever just will not compile if you don't get the properties right. Is there any other purpose for #model other than IntelliSense and a run-time error, and why is it called strong-typed if it's in fact, not?
Strong typing, Meanings in computer literature
Views are compiled at runtime by default. You can modify your project file (csproj) to compile the views when the application builds by setting the following property:
<MvcBuildViews>true</MvcBuildViews>
Downside of this approach is that your buildtime will increase significantly. You should consider only setting this option to true for release builds.
You can edit your project file by unloading the project, right-click the project and choose Edit ProjectFile
It is possible to setup your project so that it includes views in your compilation. This would be where static typing is useful. Another place would be in runtime, if you try to pass in a model that does not match the expected model you will immediately get an exception. If you were to type views dynamically then you would not know your model was invalid until your view tried to access a property of the model and finds out it isn't there.
The second scenario is also a nightmare if you pass in the wrong model object but it happens to have the same named property as the expected model. Then you just get invalid data and debugging becomes hell.
model is new dynamic type in .net 4.0, so these types get resolved at runtime not at compilation time.

How to change VS2010 Add Reference box filter?

I'm getting used to the new IDE (it's vc# express), but the first contact is somewhat confusing. When I open the Add Reference dialog and switch to the .NET tab, a label above the assembly list states: "Filtered to:.NET Framework 4". And it's true - I can reference .NET 4.0 assemblies only plus things like XNA 3.1. However I can't see older assemblies i.e. Managed DirectX libs, which are obviously installed on my computer as there was no trouble with adding a ref to them in vc#08.
What is this? How to change the filter? The label is read-only. These sound like dull guy's questions, but I'm out of luck in finding an answer and there is no intuitive solution.
Thanks in advance.
Change your new application to target the .NET Framework 4 and not .NET Framework 4 Client
"When you create a new application, some projects target the .NET Framework 4 Client Profile by default."
http://msdn.microsoft.com/en-us/library/cc656912.aspx
"If you are targeting the .NET Framework 4 Client Profile, you cannot reference an assembly that is not in the .NET Framework 4 Client Profile. Instead you must target the .NET Framework 4. "
We've created a tool that will help you to achieve your goal. Muse VSReferences will allow you to add a Global Assembly Cache reference to the project from Add GAC Reference menu item.
Regards
s
How to change the filter?
Very simple - you must change the Target .NET Framework for your project, as whole. It's not pain :) just RMB on project name in Solution Explorer->Properties->Application tab->Target Framework(combobox). Select what you want. Change combobox == change filter in Add Reference dialog. ;)
I was having a similar problem until I noticed that the older .NET assemblies were actually in the list, there's just some really strange sorting going on. If you sort by assembly name, you should see them in the proper order.
Another possibility is to go into your project’s Properties page and change the Target Framework from 4.0 to your desired Framework. You will then need to reload you application. Now your Reference should be there. Once you have added the Reference you want, change back to 4.0 and again reload.
Hope this helps.
If, for instance, your project is Framework 4 and you want to reference say Microsoft.Deployment.WindowsInstaller (which is not in the filtered list of the .NET tab), then go to the Browse tab and enter the path to the reference item e.g. C:\Program Files\Windows Installer XML v3.5\SDK\Microsoft.Deployment.WindowsInstaller.dll

Resources