Swashbuckle returns nullreferenceexception - asp.net-web-api

I have started to use swashbuckle for my rest api.
When I go to localhost:port/swagger
I get the following runtime error:
System.Nullreferenceexception in
Swashbuckle.Application.SwaggerDocsConfig.Getswaggerprovider(Httprequestmessage
swaggerRequest)
I initialized swashbuckle like this:
var config = new Httpconfiguration();
config.EnableSwagger();
Please, help me.

guys
The issue was resolved. I simply recreated my asp.net project, added model, referenced swagger and it is all OK.

Related

Autofac with Owin

I have an issue with Autofac. The documentation clearly states that when using Web API 2 and OWIN you must not use GlobalConfiguration.Configuration anywhere:
A common error in OWIN integration is use of the GlobalConfiguration.Configuration. In OWIN you create the configuration from scratch. You should not reference GlobalConfiguration.Configuration anywhere when using the OWIN integration.
which can be found here (at the bottom of the page): http://autofac.readthedocs.io/en/latest/integration/webapi.html
But no matter what I do, I can not get Autofac to work using:
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
instead of:
GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
When I use the latter, it works.
Does anyone know why?
Ok, I figured this out. I will post the answer here incase anyone else has this issue.
As the documentation stated, you can't use GlobalConfiguration anywhere....
so I did a search and I found it somewhere else.
I had this:
GlobalConfiguration.Configure(WebApiConfig.Register);
which should have been:
WebApiConfig.Register(config);
When I fixed that, I was able to use the proper
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);

Type controller does not have a default constructor

Hi friends in webapi using mvc format i created one webapi project here i added autofac resolver to resolve DI(dependency injection) its working fine then in next step i took this dlls and created new project for this i added the dlls now in this project i added one controller but it is showing the error 'x(controller name)' Type controller does not have a default constructor but for this many people given solution for this add new bootstrap class(which contain autofac for di resolve) but i am inherting my old global.asax file from my current global.asax file so i think dont want to add again all the stuff the autofac resolver will get from old dlls so i find many solutions but now luck can any one help me
Note : please ask me incase of any clarity (my english is not good excluse me for this)
The Mistake I did is Spelling mistake this is if Controller name is SampleController i am putting SampleContoller (missing r in Controller splleing) thats it now i corrected splleing now its working fine

Read-only model property not serialized in Web API

As per the title, I'm seeing that my read-only model properties are not serialized in my Web API project. MVC 4 Web API, VS2010.
I've seen a multitude of posts like this stackoverflow question that state that the MVC 4 Web API beta did not support JSON serializing of read-only properties. But many additional references stated that the final release used JSON.NET instead of DataContractJsonSerializer so the issue should be resolved.
Has this issue been resolved or not? If not, am I forced to put in fake setters just to get serialization?
Correction, it does seem to work with JSON (sorry!), but XML exhibits the problem. So same question as before but in the context of XML serialization.
The default JSON serializer is now Json.NET. So readonly property serialization should work without you having to do anything at all.
For XML, in 4.5 we added this flag to the DataContractSerializer:
http://msdn.microsoft.com/en-us/library/vstudio/system.runtime.serialization.datacontractserializersettings.serializereadonlytypes.aspx
You should be able to write something like this:
config.Formatters.XmlFormatter.SetSerializer(myType, new DataContractSerializer(myType, new DataContractSerializerSettings() { SerializeReadOnlyTypes = true });
Place this code in a function called by GlobalConfiguration.Configure in the Application_Start. By default this would be WebApiConfig.Register().

DotNetOpenAuth 4.0 and HTTPRequestInfo, NerdDinner 2.0

I'm using NerdDinner 2.0 as a base for learning DotNetOpenAuth. Great project BTW. I have it working using a DotNetOpenAuth DLL built on runtime version v2.0.50727 (came with the project). I would like to upgrade to a DotNetOpenAuth DLL built on runtime version v4.0.20926 but I get the following compile time error in the AuthController.cs file...
HttpRequestInfo clientResponseInfo = new HttpRequestInfo("GET", auth, auth.PathAndQuery, headers, null);
Exception:
DotNetOpenAuth.Messaging.HttpRequestInfo does not contain a constructor that takes 5 arguments.
Obviously I do not know what the code equivalent is in v4.0.20926.
Any help is greatly appreciated.
Try replacing this with:
HttpRequestBase clientResponesInfo = HttpRequestInfo.Create("GET", auth, headers, null);

Error using Autofac with ASP.NET Web API RC and ASP.NET MVC3

I added ASP.NET Web API RC to my MVC3 project using NuGet:
Install-Package AspNetWebApi
Then I configured it. In Global.asax.cs:
// configure regular controllers
var configuration = GlobalConfiguration.Configuration;
var container = Bootstrapper.ConfigureContainer(configuration);
containterProvider = new ContainerProvider(container);
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
// Set the dependency resolver implementation for Web API.
var resolver = new AutofacWebApiDependencyResolver(container);
configuration.DependencyResolver = resolver;
And in Boostrapper.ConfigureContainer(...) I added:
// I register my types as InstancePerLifetimeScope()
// but I also tried .InstancePerHttpRequest().InstancePerApiRequest()
// to the same end
builder.RegisterType<SomeService>()
.AsImplementedInterfaces().InstancePerLifetimeScope();
// Register API controllers using assembly scanning.
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
builder.RegisterWebApiFilterProvider(config);
This is described here and here.
I also updated Autofac, Autofac.Web, Autofac.Mvc3 packages using NuGet and installed Autofac.WebApi package.
With this configuration I tried running my ApiController and got the following error:
No scope with a Tag matching 'httpRequest' is visible from the scope in which the instance was requested. This generally indicates that a component registered as per-HTTP request is being reqested by a SingleInstance() component (or a similar scenario.) Under the web integration always request dependencies from the DependencyResolver.Current or ILifetimeScopeProvider.RequestLifetime, never from the container itself.
Then as I read this comment from Alex Meyer-Gleaves:
I suspect you are using the Web API package with MVC3 and this is causing the problem. In the MVC3 integration the tag for the InstancePerHttpRequest lifetime scope was "httpRequest". It was in the MVC4 Beta and Web API Beta packages that I changed both InstancePerHttpRequest and InstancePerApiRequest to use the common tag "AutofacWebRequest". You can grab the MVC4 integration package from NuGet using Autofac.Mvc4.
source article with comment
So following the advice from Alex I got the package Autofac.Mvc4 but it works only with Mvc4 and my project wouldn't build. I then grabbed the source code of Autofac to build Autofac.Mvc4 against Mvc3:
hg clone https://code.google.com/p/autofac/ --branch "MVC4 beta" C:\my\path
After using this assembly as my reference ApiController started working but regular Controllers worked ok only for a single controller action call. When the view called Html.RenderAction(...) and when I refresh or navigate to another controller action it crashes with this error:
No scope with a Tag matching 'AutofacWebRequest' is visible from the scope in which the instance was requested. This generally indicates that a component registered as per-HTTP request is being reqested by a SingleInstance() component (or a similar scenario.) Under the web integration always request dependencies from the DependencyResolver.Current or ILifetimeScopeProvider.RequestLifetime, never from the container itself.
I thought building from the newest source for Autofac.Mvc4 version 2.6.2.859 against Mvc3 could help but I can't find the source for that. Or maybe there's something else wrong in here?
I found the problem. I also used Autofac.Integration.Web to inject dependencies into custom Membership and Role providers. But in WebLiftime.cs there was this line:
public static readonly object Request = "httpRequest";
Once I changed it to:
public static readonly object Request = "AutofacWebRequest";
and used the built assembly everything works fine and I get no errors :)
I believe this constant value should the same as in all projects Autofac.Integration.Web, Autofac.Integration.Mvc and 'Autofac.Integration.WebApi for Mvc4 so this supposedly is a bug.

Resources