Do I have to do the configuration for nservicebus on mvc4 that would be handled by the EndpointConfig.cs? - asp.net-mvc-4

In the examples, most of the config is done by the dev by changing AsA_Server to AsA_Client.
public class EndpointConfig : IConfigureThisEndpoint, AsA_Client { }
However, I can't seem to do that with an ASP.NET MVC4 app.
Do I have to manually configure everything in a web environment?

Yes, here are links to the documentation:
http://support.nservicebus.com/customer/portal/articles/894008-using-nservicebus-with-asp-net-mvc
http://support.nservicebus.com/customer/portal/articles/894123-injecting-the-bus-into-asp-net-mvc-controller
You can also have a look at our sample projects for examples on how to do it, see https://github.com/NServiceBus/NServiceBus/tree/master/Samples/AsyncPagesMVC3
There is also a sample that uses MVC4 but that is against NServiceBus v4 which has not been released yet, see https://github.com/Particular/NServiceBus/tree/develop/Samples/VideoStore.Msmq/VideoStore.ECommerce

Related

Add SignalR client functionality to a Fable.Elmish application

I found 2 resources from the community
1.Fable.SignalR - A functional type-safe wrapper for SignalR and Fable. I am using Elmish so the package is Fable.SignalR.Elmish
2.fable-signalr - Fable bindings for SignalR
2 Is declared to work only before Fable 3, which is not my case.
With 1 I have a problem: I do not control the server side. The server application is a standard C# ASP.NET core Web Api application that uses SignalR.
All the examples I found for 1 require a shared F# library, which cannot be done in this case.
Let's suppose to have a simple Hub like in the official docs:
using Microsoft.AspNetCore.SignalR;
namespace SignalRChat.Hubs
{
public class ChatHub : Hub {}
}
and an Api controller that receives the hub context through DI and simply broadcasts a message:
class YatpApiController(IHubContext<ChatHub> hubContext) : ControllerBase() {
[HttpPost("signalr-broadcast")]
public SignalrBroadcast()
{
hubContext.Clients.All.SendAsync("Method1", "Message1");
}
}
Can someone please
Show how to use Fable.SignalR.Elmish client side with this very simple case?
Give advise on an alternative way to connect to this simple hub from a Fable.Elmish application, without writing bindings to the javascript signalr package?

How do I get a connection string in a .net core standard class library from the configuration file in a .net core 2.0 web app?

I have .net core standard class library which is essentially a DAL with several class methods that return collections and objects from a database. The connection string is in the appsettings.json file of the ASP.net 2 core web app. I also want to access this class library from a console app project where the configuration file with the connection string will be present in that console app project.
This was simple in .net prior to .net core. The DAL class library would just access the web.config from a web project and an app.config from a console application as it the library is referenced in both the web app and console apps. But it doesn't seem like this is at all possible.
I'm looking for the simple solution in .net core to get a connection string from web app or console app as the case may be.
Where you're probably going wrong is that you want to access configuration from your class library, but then you want to leak details specifically about the caller (That it will have a web.config).
But what if you decide in your Web Application you want to use Azure Key Vault or another secrets mechanism? Does your class library need to then change it's entire implementation to use Key Vault? And then does that mean your console application also has no option but to use Key Vault too?
So the solution is to use dependency inversion. Put simply, let's say I have code like the following :
interface IMyRepositoryConfiguration
{
string ConnectionString {get;}
}
class MyRepositoryConfiguration : IMyRepositoryConfiguration
{
public string ConnectionString {get;set;}
}
class MyRepository
{
private readonly IMyRepositoryConfiguration _myRepositoryConfiguration;
public MyRepository(IMyRepositoryConfiguration myRepositoryConfiguration)
{
_myRepositoryConfiguration = myRepositoryConfiguration;
}
}
Now in my startup.cs I can do something like :
services.AddSingleton<IMyRepositoryConfiguration>(new MyRepositoryConfiguration {//Set connection string from app settings etc});
And now my class library doesn't need to know exactly how those configuration strings are stored or how they are fetched. Just that if I request an instance of IMyRepositoryConfiguration, that it will have the value in there.
Alternatively of course, you can use the Options class too, but personally I prefer POCOs. More info here : https://dotnetcoretutorials.com/2016/12/26/custom-configuration-sections-asp-net-core/
It is very much possible to access "connection strings" or other configuration data easily in .Net core without much additional effort.
Just that the configuration system has evolved (into something much better) & we have to make allowances for this as well (& follow recommended practices).
In your case as you are accessing the connection string value in a standard library (intended to be reused), you should not make assumptions as how the configuration values will be "fed" to your class. What this means is you should not write code to read a connection string directly from a config file - instead rely on the dependency injection mechanism to provide you with the required configuration - regardless of how it has been made available to your app.
One way to do this is to "require" an IConfiguration object to be injected into your class constructor & then use the GetValue method to retrieve the value for the appropriate key, like so:
public class IndexModel : PageModel
{
public IndexModel(IConfiguration config)
{
_config = config;
}
public int NumberConfig { get; private set; }
public void OnGet()
{
NumberConfig = _config.GetValue<int>("NumberKey", 99);
}
}
In .net core, before the app is configured and started, a "host" is configured and launched. The host is responsible for app startup and lifetime management. Both the app and the host are configured using various "configuration providers". Host configuration key-value pairs become part of the app's global configuration.
Configuration sources are read in the order that their configuration providers are specified at startup.
.Net core supports various "providers". Read this article for complete information on this topic.

Versioning with WebAPI .Net Core does not work as expected

I am trying to introduce URL versioning into my .Net Core WebAPI application. I am also using Swagger web tools for ease of use for users.
Now, while trying to introduce versioning into my application, I referenced the docs here: https://github.com/Microsoft/aspnet-api-versioning/wiki/New-Services-Quick-Start#aspnet-core
Now, I made following code changes into my code:
Startup.cs/ConfigureServices I added code below:
services.AddApiVersioning(options => {
options.AssumeDefaultVersionWhenUnspecified = true;
});
Now, my controller annotations before any kind of versioning was added, looked like below:
[Produces("application/json")]
[Route("api/controllerName")]
and produces a URL which looks like something below:
http://localhost:12003/swagger/#!/Workspace/GetAll
Now, I added annotations below to enable api versioning:
. [ApiVersion("1.0")]
[Produces("application/json")]
[Route("api/v{version:apiVersion}/workspace")]
and now when I click on the same method listed in my swagger UI
the url looks like below:
http://localhost:12003/swagger/#!/controllername/ApiV_versionGetAll
While what I was expecting was something like:
http://localhost:12003/swagger/#!/controllername/V1.0/GetAll
Also on my swagger it is now asking me explicitly about entering version number. So I think my question boils down to two major points:
How I can I fix my URL? and what am I doing wrong?
Why is swagger now asking me to enter version number in API UI when I have explicitly stated that the version is going to be 1.0 in the annotation of the controller?
What you are missing is the complementary package for API versioning that supports an API version-aware API Explorer:
https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer
Install-Package Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer
The API Explorer is how Swagger generators like Swashbuckle do all their work. The source and links are also available in the API versioning repo.
To achieve the result you want, you need to configure API version substitution in the URL:
services.AddMvcCore().AddVersionedApiExplorer( options => options.SubstituteApiVersionInUrl = true );
Note: that the call the AddMvcCore() is no longer required in API Versioning 3.0+
Documentation and samples are available in the official API versioning repo. I recommend reviewing the API Documentation wiki topic:
https://github.com/Microsoft/aspnet-api-versioning/wiki/API-Documentation
The accepted answer extends this package, which is fine as long as it stay up-to-date with the flavor of API versioning you are using. API Versioning always ships compatible API Explorer extensions on every release.
Setting up api versioning with swagger is indeed a tricky thing as it is lot's of pieces that need to be setup correctly.
Luckily for us, there's a great nuget packages called SwashbuckleAspNetVersioningShim which solves this in an excellent way.
Add it
Install-Package SwashbuckleAspNetVersioningShim -Version 2.2.1
Then follow the readme here

How to add asp.net identity to existing web api project?

I have 2 projects in my solution. First one is simple mvc project and the other one is web api. There was no pre-written code in web api. I put all logics myself. Now I want to add asp.net identity in the web api project. How can I do that?
Thanks.
In your web api project, you can do this:
1. Create a DbContext class that looks like this:
public class DataContext : IdentityDbContext<IdentityUser>
{
public DataContext() : base("ConnectionStringLocal") { }
}
Add a connection string in your Web.config file
In Package manager console, do Enable-Migrations, Add-Migration IdentityUpdate, Update-Database. This will create a database that has asp.net identity built in.
Please let me know if you have additional questions.

Autobinding in MVC4 WebApi with Ninject.Extensions.Conventions

In my MVC4 app I'm using Ninject.Extensions.Conventions to autobind all itnerfaces with their implementation using default mechanism:
kernel.Bind(x => x
.FromAssembliesMatching("*")
.SelectAllClasses()
.BindDefaultInterface());
This works great for regular controllers, but doesn't for WebApi controllers. What do I need to change/add?
Ok, I resolved the issue by following this article:
http://www.peterprovost.org/blog/2012/06/19/adding-ninject-to-web-api/
In short: I created my own dependency resolver (as per article) and assigned it to GlobalConfiguration.Configuration.DependencyResolver as suggested by nemesv
There are already a many examples on how to integrate NInject with Web API through the web, using:
DependencyResolver
On ASP.NET website is shown standart way of integration of any DI/IoC with Web API:
http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver
IDependencyResolver.
WebApiContrib project shows how to do it with IDependencyResolver
https://github.com/WebApiContrib/WebApiContrib.IoC.Ninject
Please post more information about your implementation of these resolvers, to be more specific in answer.