Swagger w/ ASP.NET v5 Azure Api App - asp.net

I'm attempting to set up a Api App (Azure) with Swagger + Swashbuckle as demonstrated by Scott Hanselman at the //Build conference here: http://channel9.msdn.com/Events/Build/2015/2-628.
I have installed (using NuGet) the packages Swagger.Api and Swashbuckle.Core. It hasn't added any controller or settings that I would expect in order to have a swagger page. When I navigate to {baseUrl}\swagger, I get a 404 error.
I would think that since it has a UI it would require a Web App in addition to the Api App, but I've rewatched the demo and Scott clearly says you can add Swagger + Swashbuckle to any Api App. In a 2nd app though I'd think there may be issues with Api discovery. Has anyone set this up yet successfully?

Time rolls on and now Swashbuckle works for vNext (beta8 for me, probably other versions too) - thank you to the team and contributors!
In project.json add the package:
"Swashbuckle": "6.0.0-*",
In startup.cs in ConfigureServices():
services.AddSwaggerGen();
services.ConfigureSwaggerDocument(options =>
{
options.SingleApiVersion(new Info
{
Version = "v1",
Title = "My Super API",
Description = "A Super API using Swagger and Swashbuckle",
TermsOfService = ""
});
});
services.ConfigureSwaggerSchema(options =>{
options.DescribeAllEnumsAsStrings = true;
});
In startup.cs in Configure():
app.UseSwaggerGen();
app.UseSwaggerUi();
Now you can access your API doco - http://domain:port/swagger/ui/index.html
Access your Swagger definition - http://domain:port/swagger/v1/swagger.json
Then assuming you have at least one internet facing dev/uat/staging/prod environment, grab the definition URL then do File-> Import URI at http://editor.swagger.io/ - now you have code-gen for about 20 clients too :)
You can also setup your own code-gen server if you are so inclined too (https://github.com/swagger-api/swagger-codegen), however I leveraged the existing online generator. The online editor also has full model and relationship definitions too at least in my case where I fully defined my model using EF7 (I know, ick... but it's much better than <= EF6 and ServiceStack doesn't support CoreCLR, yet). Depending on the size of your project this could save you a few hours to a few weeks of work documenting, plus it is dynamically updating itself as you code more. And you can use it to test your endpoints too, but I still prefer PostMan.
Full sample project at https://github.com/mrsheepuk/ASPNETSelfCreatedTokenAuthExample/tree/beta8
Big ups to MrSheepUK
HTH

This answer is now outdated. See the other answer.
There is a nice blogpost describing the problem you have: https://alexanderzeitler.com/articles/Deploying-a-ASP-NET-MVC-6-API-as-Azure-API-App-in-Azure-App-Services/
This describes how you can add the Ahoy! package to an ASP.NET v6 Web Api project and adding this as an API app to Azure.
Here is another source: http://devmeetsbi.ghost.io/help-and-test-page-for-asp-net-web-api-asp-net-5-and-mvc-6/

You did all the right steps, but unfortunately for ASP.NET 5, Swashbuckle doesn't work yet.
You can get Ahoy! which is the next version of Swashbuckle that has ASP.NET v6 support here. That should make everything work.

Related

Change in behaviour for IActionInvokerProviders when using UseStatusCodePagesWithReExecute between core 3.1 and dotnet 6

Context:
Upgrading an existing aspnet application from core 3.1 to dotnet 6.0.
Issue:
We have registered a IActionInvokerProvider in our web app. This simply adds some information to the context route data.
We also use UseStatusCodePagesWithReExecute
app.UseStatusCodePagesWithReExecute("/somecontroller", "?statusCode={0}");
According to the documentation https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.abstractions.iactioninvoker?view=aspnetcore-3.1
An IActionInvoker is created for each request the MVC handles by querying the set of IActionInvokerProvider instances. See IActionInvokerProvider for more information.
When running this in netcoreapp3.1 when we return a NotFound() I can observe that 2 calls are made to our action provider OnProvidersExecuting. One for the request to the resource and one for a call expected UseStatusCodePagesWithReExecute to /somecontroller.
When targeting net6.0 and changing no other code this second call to /somecontroller does not get called only the first . If I call the endpoint /somecontroller?statusCode=404 I it does trigger the invoker. I cannot find a reference to a breaking change anywhere. perhaps I missed it.
Does anyone know what the casue might be?
I have tried altering the ordering of the pipeline.
Tried to repro it in https://github.com/csi-lund/core31tonet6issue
In the version the Action provider never gets called at all
The answer was a missed breaking change and documentation.
https://github.com/dotnet/aspnetcore/issues/45589
We skip the IActionInvoker by default as an optimization for
controllers and pages. This is a really heavyweight way to add route
data to the pipeline. You can set EnableActionInvokers to true to
enable this behavior.
builder.Services.AddControllers(o => {
o.EnableActionInvokers = true; }); In your sample it would be AddMvc (since you're using that).
No change in behaviour without documentation to indicate. (There might
be might have missed it)
Yes, it seems like we missed this one. I'll make sure it gets
documented.
PR: #27773 https://github.com/dotnet/aspnetcore/pull/27773

Check for PNR existence from JS in a web RedApp

We are creating a web RedApp from SabreRedWorkspace and we need to check for the existence of a valid PNR before adding some passive segments.
We need to do it from an Angular app, and we can't find ant doc or example about it. Everything talks about RestAPI or the new NativeAPI and we can't find anything about web RedApps
Any help? Someone could point us to any doc or example?
You can use Javascript API for that, using SrwApi3.retrievePnr("1.0.0") which is injected on the DOM for Web RedApps.
Please make sure to download the latest SDK from https://beta.developer.sabre.com/guides/travel-agency/sdks/sabre-red-360-resources, version 19.5, there where changes on this release exactly about "Get PNR" APIs.
For more documentation, you can check on the SDK documentation folder : [extracted dir]/red-app-sdk-3.0-19.5/documentation/htmls/get_pnr_(web_api).html, ob by navigating from index->New Sabre Red Workspace Developer Toolkit->Sabre Services 3.0, Get PNR (Web API)
Also theres sample code provided on SDK, look for the compressed project under samples folder : com.sabre.redapp.example.webkit-1.0.8-SNAPSHOT-v20190516-1622.zip

Setting project url in VS2015 ASP.NET 5 Web API application

I'm trying to create a Web API project and a client-side web project, where the web project can access the API via ajax. Currently my project looks like this:
I saw this answer on here: Setting app a separate Web API project and ASP.NET app, which explains how the project url can be set to localhost:[port]/api.
But for ASP.NET 5 projects, the properties only have 3 tabs (as opposed to the several found in ASP.NET 4 projects):
What I'm wondering is:
Do I have to set this option somewhere else? (i.e project.json)
How would this work when I publish? Ideally I'd want [websiteURL]/api to serve up my API, whereas that link explicitly put localhost:8080.
Is having these as two projects a good idea? I could easily put API and web in the same project, but I like the separation of client-side and server-side logic.
Any help would be appreciated!
First Point:
Generally speaking in ASP.NET 5, the routing defaults are very good and should work out of the box without much in the way of configuration. You can use configuration and/or attribute based routing in your application (with a detailed overview of both here), although my personal preference is for the attributed approach. Provided you have the following line in your Startup.cs file (which you should have in a new project):
app.UseMvc();
you should be able to route requests to your api controllers in the fashion required (i.e. "/api/...") simply by using [Route] attributes as below (example taken from a standard generated ASP.NET 5 Web API application)
[Route("api/[controller]")]
public class ValuesController : Controller
{
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
}
The above example will route any GET request made to "/api/values".
While this approach can be used to handle requests made to your api, in order to deliver the files needed for your front end javascript application/single page app, you will need to enable static file serving. If you add the following to the Configure method in your Startup.cs class:
app.UseStaticFiles();
this will allow your application to serve those static files - by default, these are served from the ‘wwwroot’ folder, although this can be changed in the project.json file if required. The files needed for your front end app should then be added to this folder. A tutorial on serving static files can be found here.
Second Point:
In my experience this will not be an issue when you publish your website - provided your server is set up correctly, you will not need to include the port when making a request - navigating to [yourwebsitename]/api/... will suffice.
Third point:
In my opinion this entirely depends on how large the project is likely to grow, although preference and opinion will vary from developer to developer. Generally speaking, if the project will remain small in scope then keeping both in a single project is perfectly ok, as unnecessary complexity is reduced. However it is also very useful as you have pointed out, to maintain a separation of concerns between projects. So aside from the organisational advantage of your approach, the respective dependencies of the two projects are/will be kept separate also.

Reference docs for Azure Resource Templates

I am looking for reference docs for the Azure Resource Manager JSON templates. Does anyone know if there is reference material for these templates?
There is general reference for required parameters etc like at Create a template deployment.
I am basically looking for the full availability so I can correspond setup on the portal to the JSON template. Also availability of features with apiVersion releases. I remember there being a MSDN documentation for the changelog with api version releases but cannot find it now.
If you create a VM with the desired settings, extensions etc then you can view their json template via https://resources.azure.com/
This will give some visibility into the Classic* templates.
All of the ARM templates can be found on GitHub here: https://github.com/Azure/azure-resource-manager-schemas.
It includes preview templates and should provide all the information you're after to determine which features are present in which apiVersion release.
Microsoft has finally created what I was looking for 🎉: full documentation is now available at https://learn.microsoft.com/en-au/azure/templates/
After some digging I managed to get the following list of schemas:
http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json
http://schema.management.azure.com/schemas/2014-06-01/Microsoft.Web.json
http://schema.management.azure.com/schemas/2014-04-01-preview/Microsoft.Sql.json
http://schema.management.azure.com/schemas/2014-04-01/Microsoft.Insights.json
http://schema.management.azure.com/schemas/2014-02-26/microsoft.visualstudio.json
http://schema.management.azure.com/schemas/2014-04-01-preview/Microsoft.Cache.json
http://schema.management.azure.com/schemas/2014-04-01/Microsoft.BizTalkServices.json
http://schema.management.azure.com/schemas/2014-08-01/Microsoft.Scheduler.json
http://schema.management.azure.com/schemas/2014-04-01/SuccessBricks.ClearDB.json
http://schema.management.azure.com/schemas/2015-01-01/Microsoft.Resources.json
http://schema.management.azure.com/schemas/2015-01-01/Microsoft.Authorization.json
http://schema.management.azure.com/schemas/2014-10-01-preview/Microsoft.Authorization.json
This list notably excludes:
Microsoft.ClassicCompute
Microsoft.ClassicStorage
Microsoft.ClassicNetwork
So I guess we're left to figure stuff out from the templates on those
To my mind we can dig that way:
open the azure-resource-manager schemas
Look at the main form below:
If you open properties, you will find the format that we need to fill:
open parameters and look at the structure:
$ref: #/definitions/parameter invite us to look at the same documents in definitions.parameters where you will find some documentation (like value you can use etc):
finally, if you look to properties.resources, you will find a list of url like:
{ "$ref": "http://schema.management.azure.com/schemas/2015-07-01-preview/Microsoft.ServerManagement.json#/resourceDefinitions/node" }
{ "$ref": "http://schema.management.azure.com/schemas/2015-07-01-preview/Microsoft.ServerManagement.json#/resourceDefinitions/gateway" }
if you open one of these url, you will find the JSON format you are looking for (here is a part of the first one):
There is not much available...
Azure Resource Manager Template Language
https://azure.microsoft.com/en-us/documentation/articles/resource-group-authoring-templates/
And then you can look at the different json.schemas that I have managed to find
deploymentTemplate
http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json
visualstudio
http://schema.management.azure.com/schemas/2014-02-26/microsoft.visualstudio.json
Sql
http://schema.management.azure.com/schemas/2014-06-01/2014-04-01-preview/Microsoft.Sql.json
Web
http://schema.management.azure.com/schemas/2014-06-01/Microsoft.Web.json
deploymentParameters
http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentParameters.json
If you use Visual Studio to edit the json-template file you get intellisense (sometimes) which help a bit. But the lack of documentation is really annoying...for example I have no clue if the schemas listed above are the most recent or not, and I have no idea where to find which one is the most resent.
Edit:
I came across the list of additions and changes to the Service Management APIs. Seems to be a bit outdated, 2015-01-01 is the current version and it's not there.
Edit2:
With the Iaas updates at Build 2015, there seems to also be a lot of Azure Quickstart Templates. At minimum, they have the particular cases I was looking for with storage accounts.

Google Drive API - Example code is not working

I want to use Google API to transfer SharePoint Documents to Google Drive using dot net. For that to happen I want to use this link Google Quick Start.
I have followed every little piece of information. It states in beginning "Complete the steps described in the rest of this page, and in about five minutes you'll have a simple Drive app that uploads a file to Google Drive" but it is not true.
I am trying to run this sample example since yesterday but failed.
// Register the authenticator and create the service
var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description, CLIENT_ID, CLIENT_SECRET);
var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization);
So it seems that this code example is outdated and Google APIs have been upgraded.
Here comes the warning
[Obsolete("GoogleAuthenticationServer is not supported any more and it's going to be removed in 1.7.0-beta. Consider using the new Google.Apis.Auth NuGet package which supports .NET 4, .NET for Windows Store apps, Windows Phone 7.5 and 8 and Portable Class Libraries as well")]
Another code
var service = new DriveService(new BaseClientService.Initializer()
{
Authenticator = auth
});
And the error
Cannot implicitly convert type 'Google.Apis.Authentication.OAuth2.DotNetOpenAuth.NativeApplicationClient' to 'Google.Apis.Http.IConfigurableHttpClientInitializer'. An explicit conversion exists (are you missing a cast?)
Then I was looking for [latest release samples] there I found the source files.
I was not able able to run the sample also.
Here I was successfully authenticated but then had error for redirect Uri
My Client_Secrets.JSON looks like, as you can see I have set default redirect Uri to http://localhost/.
{"web":{"auth_uri":"https://accounts.google.com/o/oauth2/auth","client_secret":"secret","token_uri":"https://accounts.google.com/o/oauth2/token","client_email":email","redirect_uris":["http://localhost/"],"client_x509_cert_url":"aa#developer.gserviceaccount.com","client_id":"id","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","javascript_origins":["http://localhost:53404/"]}}
Now I can run this application but as mentioned getting error
Error: redirect_uri_mismatch
The redirect URI in the request: [[http://localhost:2430/authorize/]] did not match a registered redirect URI
I really don’t know from where this port number comes and from where this authorize comes. As you can see in my JSON, I have not set anything similar.
I am using VS 2010 SP1 Ultimate and Windows 7 home Basic.
I have tried to give complete information and my end goal is upload document in Google drive.
The tutorial you are following uses an older version of google.apis thats why you are seeing the not supported. Here are a couple of tutorials on how it works with the new version of the api.
http://daimto.com/google-oauth2-csharp/
http://daimto.com/google-drive-api-c/
Take a look in https://code.google.com/p/google-api-dotnet-client/source/browse/Tasks.ASP.NET.SimpleOAuth2/Default.aspx.cs?repo=samples. It's a Task API ASP.NET sample. As you can see you need to add several lines of code, but it works.
I downloaded the samples Eyal mentions and got that compiling before integrating with my own app. .Net needs to be 4 although it states it supports higher.
You have to set the redirect URI in the google developer console, not in your JSON. In your case, you would want to set it to http://localhost:2430/authorize/.

Resources