I installed the NuGet package Ninject Integration for WebApi2 via the Package Manager Console.
According to the wiki on the project's GitHub pages, this should have created a class called NinjectWebCommon in the App_Start folder. But it didn't.
That same GitHub wiki page explains what you should see so that you can add it yourself. So I tried creating the NinjectWebCommon class myself. The problem here is that I can't find the namespace for OnePerRequestModule in the following snippet.
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
bootstrapper.Initialize(CreateKernel);
}
The alternative, and according to that GitHub wiki page there's no effective difference between the two, is to modify the global.asax. So I tried this method and added some bindings like so
private void RegisterServices(IKernel kernel)
{
kernel.Bind<IEntityAccess>().To<EntityAccess>();
kernel.Bind<IDictionaryRepository>().To<DictionaryRepository>();
}
and found that my project builds, but when a request is sent to the WebAPI project it can't be found (i.e., I receive a 404 response).
So there's obviously some other piece of necessary wiring up which isn't in my project.
It appears that despite changing my global.asax to derive from NinjectHttpApplication, the global.asax is no longer being called.
Can anyone tell me what I might be missing? I've uninstalled and reinstalled the NuGet package a few times but the NinjectWebCommon class never appears, nor does my global.asax file ever get modified.
I've also read Ninject's own documentation but frustratingly this is a fairly large tutorial covering the basics of IoC and how Ninject operates rather than telling you how to get started with using Ninject.
There's also this SO post asking how to get started with Ninject for WebAPI, so it looks like something's amiss with their NuGet package.
And like that, I've just found the answer: there is an additional NuGet package which must be referenced:
Ninject.Web.WebApi.WebHost
Installing "Ninject integration for WebApi2" package is not sufficient.
This really should be more clearly explained.
Related
I'm trying to use the Fody MethodDecorator to log method entry/exit in multiple projects. I've created a project in visual studio and implemented the IMethodDecorator interface as described in the ReadMe.md in the Fody MethodDecorator project.
The Fody MethodDecorator works fine in the one project where I've implemented the IMethodDecorator interface and I can log method entries/exits. But when I try to log method entry/exits in other projects by adding a reference to my first project, there is no logging in any other projects.
I've created a github repository with a minimal working example just to show the issue, click here for link.
There are others that have had the same issue, like this post from 3 yeas ago. But the solution in that post is to add the Fody and MethodDecorator nuget packages to all projects. I have already added both nuget packages to all projects. I have also added the FodyWeavers.xml to all projects.
If you want to know what my code looks like, just check out the git repo. Is there anything else I have to do to get logging of method entry/exit working in other projects?
I would like to simply add a reference to my first project and reuse the method decorator from the first project. I'm using .Net 4.8, Fody 6.5.3 and MethodDecorator.Fody 1.1.1.
Edit
If you download the repository and get the error: A numeric comparison was attempted on "$(MsBuildMajorVersion)" that evaluates ... This is apparantly due to a bug in Visual Studio, that can be overcome by a restart.
Not really an answer to the question, but I ended up switching to MethodBoundaryAspect.Fody.
It's a nuget package that does the same thing as MethodDecorator.Fody, and has an interface that you implement in an almost identical manner.
I got MethodBoundaryAspect.Fody to work in multiple projects, by referencing an attribute that I only created in one of the projects. You still have to add the nuget packages MethodBoundaryAspect.Fody and Fody, and also add the FodyWeavers.xml file to each project though.
For Fody.MethodDecorator to work in other projects, you need to add the module
[module: Interceptor] in each project.
My suggestion is to create an Interface as below in each project, and make sure your classes inherit from the interface. ( This way you only added the module at one place in the project)
[module: Interceptor]
public interface IInterceptor
{
// no methods here.
// the interface is used only to register the Interceptor attribute at the module level.
}
public class SomeClass : IInterceptor
{
[Interceptor]
public void SomeMethod()
{
}
}
I'm following this guide and in step 4, I'm asked to add three lines to the project.json file (which I did and then ran dotnet restore getting a lot of updated packages).
When I enter the three lines in the Configure method, I get red lines on all of them. The methods aren't recognized, no intellisense provided etc.
I also noticed that in the example in the guide, the method signature only takes one parameter of IApplicationBuilder, whereas the one I got generated (using the yo aspnet command) looks like this.
Configure(IApplicationBuilder, IHostingEnvironment, ILoggerFactory);
I'm not sure how to resolve it. My guess is that there's a new version of something in the process (Yo, Generators, Core etc.) but I'm not entirely sure.
I've also found this blog where the method signature resembles the one I'm getting. However, the author of it suggest the same syntax that doesn't work for me. I'm guessing it's a matter of referencing the wrong libraries. How do I approach the issue?
For Asp.Net core MVC you need to install Nuget package
install-package "Microsoft.AspNetCore.StaticFiles"
That guide is outdated. The updated .Net core does not use project.json anymore which is unfortunate. Instead it is now part of csproj file. And to add the Static file library you have to add it to the project using nuget packet manager. And when you rebuild you will see an entry in csproj file for that library. I think the project.json was a great idea which was inline with core opt-in methodology, since it would allow intellisense to kick in to help you select from available libraries. And since csproj file cant be directly edited in solution you lose that feature.
Judging from the screenshots in the linked tutorial, its about ASP.NET Core RC1 (back then called ASP.NET 5 r1-final). You can easily recognize this on the package and namespace names. Microsoft.AspNet.* is used until rc1.
Starting with RC2 the packages were renamed to Microsoft.AspNetCore.* to make it clearer its a new framework and not that much compatible with legacy ASP.NET.
The UseIISPlatformHandler() isn't there anymore, it's now UseIISIntegration() within the Main(...) method:
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
}
And the packages the package is Microsoft.AspNetCore.Server.IISIntegration": "1.0.0" and "Microsoft.AspNetCore.Server.Kestrel": "1.0.1". For static files it's: "Microsoft.AspNetCore.StaticFiles": "1.0.0".
For the Configure overload: Configure(IApplicationBuilder); is default one, but you can add any other type which is registered with the dependency injection system (in ConfigureServices method), as it's a convention system (the startup.cs).
I am using ASP.NET core and I'm trying to make use of structuremap for my IoC, but I seem to be having some issues. When I write a unit test that inits structuremap directly, everything works fine. When I print the configuration out to file, I can see that my setup is indeed registering everything correctly.
However, the populate seems to be giving me trouble.
I am trying to use StructureMap.Microsoft.DependencyInjection but I get an error when I build:
The dependency structuremap >= 4.4.0 could not be resolved.
I have StructureMap 4.4.1 installed in my project, including the project I installed the StructureMap.Microsoft.DependencyInjection library into (my Web API project).
So, I then took the files out of the project on github and loaded them into my solution, and removed the nuget package, but for some reason it is not working.
Here is a plunker with the relevant files
Ideally, I'd rather use the nuget package to do this, but I've never encountered a dependency issue when I have the actual dependency already installed.
EDIT: A few more details
When I write the results of container.WhatDoIHave() to a file, my classes are all shown correctly t0 be part of structuremap, however when I run container.AssertConfigurationIsValid(); is when I am getting errors about things correctly defined as reported by WhatDoIHave()
Here is what my configure method looks like
public IServiceProvider ConfigureIoC(IServiceCollection services)
{
var container = new Container();
container.Configure(config =>
{
// scan the webapi to register all the controllers
config.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
});
//this is an IoC configuration in another library that
// ties together a bunch of class libraries so they
// don't all have to be in my web project
IoC.BootStrapper.Configure(config, "MembershipReboot");
});
System.IO.File.WriteAllText(#"C:\users\{me}\documents\structuremapTest.txt", container.WhatDoIHave());
container.AssertConfigurationIsValid();
//Populate the container using the service collection
container.Populate(services);
return container.GetInstance<IServiceProvider>();
}
Rename the "StructureMap" package to "structuremap", seems like some weird issue with NuGet casing.
Cheers :)
See this issue on the StructureMap.Microsoft.DependencyInjection issue tracker:
The dependency structuremap >= 4.3.0 could not be resolved
https://github.com/structuremap/StructureMap.Microsoft.DependencyInjection/issues/17
I am using Visual Studio 2013 and haved opened a Project in MVC 5.
I wanted to have dynamic breadcrumbs in my website.
I searched Google and found the question related to mine on stackoverflow.
http://stackoverflow.com/questions/1066777/how-can-dynamic-breadcrumbs-be-achieved-with-asp-net-mvc
So i went for SiteMapProvider, i installed the package using nuget, but after installing it i am getting this following error.
The SiteMapLoader has not been initialized.
Check the 'MvcSiteMapProvider_UseExternalDIContainer' setting in the AppSettings section of web.config.
If the setting is set to 'false', you will need to call the MvcSiteMapProvider.DI.Composer.Compose() method at the end of Application_Start in the Global.asax file. Alternatively, if you are using .NET 4.0 or higher you can install the MvcSiteMapProvider.MVCx NuGet package corresponding to your MVC version.
If the setting is set to 'true', you must set the SiteMaps.Loader property during Application_Start in Global.asax to an instance of the built-in SiteMapLoader type or a custom ISiteMapLoader instance. This can be achieved most easily by using your external DI container.
Ninject is Also installed. i am not sure why i am getting the error. i have not done any settings only installed.
As in this below link there was no settings needed.
http://maartenba.github.io/MvcSiteMapProvider/getting-started.html
How to fix this error and make breadcrumbs work.
I faced similar issue, solved it by installing the MvcSiteMapProvider.MVC5.DI.Ninject package
https://www.nuget.org/packages/MvcSiteMapProvider.MVC5.DI.Ninject/
To use Ninject with MvcSiteMapProvider some configurations and classes are needed which are installed by this package.
I also didn't do any configuration, worked for me at first run.
First, I'll ask my question, then explain our problems found during testing. We can't seem to access the Swagger API on the resources route using ServiceStack 4.0. Is this still supported?
We're starting a greenfield project and are investigating ServiceStack. As recommended, we're using version 4.0 from http://ServiceStack.net. We've established a "Security" service and verified that /Security/User/username correctly returns our information about the user. End to end tests of ServiceStack are working great.
As we go forward, we also want to document our API using Swagger. It appears in our tests that the resources route is no longer supported, or at least is not working, in version 4.0. We've downloaded all the sample projects for guidance and they're all using ServiceStack 3.9.33, so not much luck using the samples. We've tried these local URLs for the Swagger resources snapshot:
localhost:85/resources
localhost:85/api/resources (with routing changes in the web.config
localhost:85/security/resources
localhost:85/api/security/resources (with routing changes in the web.config)
All with no luck. What are we missing?
Here is our AppHost class:
public class AppHost : AppHostBase
{
public AppHost()
: base("API Services", typeof(SecurityService).Assembly)
{
}
public override void Configure(Container container)
{
Plugins.Add(new SwaggerFeature());
Plugins.Add(new ValidationFeature());
container.RegisterValidators(typeof(UserValidator).Assembly);
}
}
And the relevant code from our Global.asax file:
protected void Application_Start(object sender, EventArgs e)
{
new AppHost().Init();
}
We've got the lastest of all packages using NuGet and Visual Studio 2013. Any guidance for ServiceStack newbies is appreciated.
I wish the solution were smarter than this, if only so that I felt that I understood the problem, but removing and then reinstalling all ServiceStack packages via the package management console solved the issue. Thanks to all who helped me to debug.