How to configure OWIN Web API 1 via IAppBuilder - owin

TL;DR: How do I configure OWIN Web API 1 via a StartUp class or alternatively use the HttpSelfHostServer with the static files extension?
I have a working self-hosted OWIN Web API 2 project, which I'd like to downgrade to .Net 4.0 - I'd prefer not to install the .Net 4.5+ framework replacement, because of unknown side-effects on a production machine.
I can't use Microsoft.AspNet.WebApi.OwinSelfHost because it's not available for .Net 4.0 and therefore I can't call the IAppBuilder.useWebApi() extension method for registering my configuration.
I'm bootstraping the server now like this: WebApp.Start(Of StartUp)(url:="...")
and the StartUp class looks like this, but I can't set the config:
Public Class StartUp
Public Sub Configuration(app As IAppBuilder)
' the host should be obsolete, as it's already configured in the app context
Dim config As New HttpSelfHostConfiguration("http://localhost:9000")
' config.MapHttpAttributeRoutes()
config.Routes.MapHttpRoute(
name:="DefaultApi",
routeTemplate:="api/{controller}/{id}",
defaults:=New With {.id = RouteParameter.Optional}
)
' configure formatters, converters, filters ...
' app.UseWebApi(config)
Dim physicalFileSystem As New PhysicalFileSystem("...\static")
app.UseDefaultFiles(New DefaultFilesOptions With {.FileSystem = physicalFileSystem})
app.UseStaticFiles(New StaticFileOptions With {.FileSystem = physicalFileSystem, .ServeUnknownFileTypes = True})
End Sub
End Class
If I'd use the documented Web API 1 approach of bootstrapping the server (see below), I don't know how to configure the static files extension:
var config = new HttpSelfHostConfiguration("http://localhost:8080");
config.Routes.MapHttpRoute(
"API Default", "api/{controller}/{id}",
new { id = RouteParameter.Optional });
using (HttpSelfHostServer server = new HttpSelfHostServer(config)) {
server.OpenAsync().Wait();
Console.WriteLine("Press Enter to quit.");
Console.ReadLine();
}
Although I've checked the AspNetWebStack sources, I think the message handling has changed between V2.1.0 and V5.0.0+ and therefore I can't apply that approach.
I'm using the following nuget packages - which I might reduce after its working:
<packages>
<package id="Microsoft.AspNet.WebApi.Client" version="4.0.20710.0" targetFramework="net40" />
<package id="Microsoft.AspNet.WebApi.Core" version="4.0.20710.0" targetFramework="net40" />
<package id="Microsoft.AspNet.WebApi.SelfHost" version="4.0.30506.0" targetFramework="net40" />
<package id="Microsoft.Net.Http" version="2.0.20710.0" targetFramework="net40" />
<package id="Microsoft.Owin" version="2.1.0" targetFramework="net40" />
<package id="Microsoft.Owin.Diagnostics" version="2.1.0" targetFramework="net40" />
<package id="Microsoft.Owin.FileSystems" version="2.1.0" targetFramework="net40" />
<package id="Microsoft.Owin.Host.HttpListener" version="2.1.0" targetFramework="net40" />
<package id="Microsoft.Owin.Host.SystemWeb" version="2.1.0" targetFramework="net40" />
<package id="Microsoft.Owin.Hosting" version="2.1.0" targetFramework="net40" />
<package id="Microsoft.Owin.SelfHost" version="2.1.0" targetFramework="net40" />
<package id="Microsoft.Owin.StaticFiles" version="2.1.0" targetFramework="net40" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net40" />
<package id="Newtonsoft.Json" version="4.5.6" targetFramework="net40" />
<package id="Owin" version="1.0" targetFramework="net40" />
</packages>
Btw. although I've used VB.Net in the example, feel free to use your preferred language in your answer.

Related

"Not available: couldn't connect to your application" live metrics Azure Application Insights

I have web application in .Net Framework 4.7.2. It deployed as App service in Azure. I have enable Application Insights in it. And I can read logs. But when I click on "Live metrics", It shows "Not available: couldn't connect to your application"
I have added latest packages of "Microsoft.ApplicationInsights" in application. Also in azure portal, in that app service, app settings "APPINSIGHTS_INSTRUMENTATIONKEY", "APPLICATIONINSIGHTS_CONNECTION_STRING" added in configuration.
Also I check Microsoft troubleshoot article "Troubleshoot couldn't connect to your application" but steps which mention in that article are already followed.
I am not able to find reason why it could not connect to application. Can someone help me regarding this?
When you create the App Service in the Azure Portal, it will also create the App Insights Resource as well:
You have to connect the Web Application to the associated App Insights Resource before publishing the Web Site:
When you run the Web Application (Open the URL), then the Request Rate, Time will be shown in the Live Metrics. As you can see in below Gifs it is showing the Request Rate, Time when I refresh the home page:
Note:
Check you have the packages related to Application Insights in Web.Config File > Under Configuration Section:
<system.web>
<compilation debug="true" targetFramework="4.7.2" />
<httpRuntime targetFramework="4.7.2" />
<httpModules>
<add name="TelemetryCorrelationHttpModule" type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation" />
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
</httpModules>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules>
<remove name="TelemetryCorrelationHttpModule" />
<add name="TelemetryCorrelationHttpModule" type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation" preCondition="managedHandler" />
<remove name="ApplicationInsightsWebTracking" />
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
</modules>
</system.webServer>
Packages.config file code:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.ApplicationInsights" version="2.22.0-beta1" targetFramework="net472" />
<package id="Microsoft.ApplicationInsights.Agent.Intercept" version="2.4.0" targetFramework="net472" />
<package id="Microsoft.ApplicationInsights.DependencyCollector" version="2.22.0-beta1" targetFramework="net472" />
<package id="Microsoft.ApplicationInsights.PerfCounterCollector" version="2.22.0-beta1" targetFramework="net472" />
<package id="Microsoft.ApplicationInsights.Web" version="2.22.0-beta1" targetFramework="net472" />
<package id="Microsoft.ApplicationInsights.WindowsServer" version="2.22.0-beta1" targetFramework="net472" />
<package id="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel" version="2.22.0-beta1" targetFramework="net472" />
<package id="Microsoft.AspNet.TelemetryCorrelation" version="1.0.8" targetFramework="net472" />
<package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="4.1.0-preview1" targetFramework="net472" />
<package id="System.Buffers" version="4.5.1" targetFramework="net472" />
<package id="System.Diagnostics.DiagnosticSource" version="7.0.0" targetFramework="net472" />
<package id="System.Memory" version="4.5.5" targetFramework="net472" />
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net472" />
<package id="System.Runtime.CompilerServices.Unsafe" version="7.0.0-preview.2.22152.2" targetFramework="net472" />
</packages>
Refer to this MS Doc for more information.

Application Insights Status Monitor doesn't track Sql dependencies for .Net Frameworks 4.5

I have a cloud service hosted application contains Web and Worker roles. To attach AppInsights telemetry I followed this article (https://learn.microsoft.com/en-us/azure/azure-monitor/app/cloudservices) including Sql Statements telemetry step. I've also relied on this sample (https://github.com/microsoft/ApplicationInsights-Home/tree/master/Samples/AzureEmailService)
After solution deployed I have all (http and azure storage) dependencies tracked, but sql.
Enviroment: .Net Framework v4.5, EntityFramework 6.1.3, Azure SDK 2.9.6
Nuget dependecies:
<package id="Microsoft.ApplicationInsights" version="2.10.0" targetFramework="net45" />
<package id="Microsoft.ApplicationInsights.Agent.Intercept" version="2.4.0" targetFramework="net45" />
<package id="Microsoft.ApplicationInsights.DependencyCollector" version="2.10.0" targetFramework="net45" />
<package id="Microsoft.ApplicationInsights.JavaScript" version="1.0.0-build00159" targetFramework="net45" />
<package id="Microsoft.ApplicationInsights.PerfCounterCollector" version="2.10.0" targetFramework="net45" />
<package id="Microsoft.ApplicationInsights.TraceListener" version="2.10.0" targetFramework="net45" />
<package id="Microsoft.ApplicationInsights.Web" version="2.10.0" targetFramework="net45" />
<package id="Microsoft.ApplicationInsights.WindowsServer" version="2.10.0" targetFramework="net45" />
<package id="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel" version="2.10.0" targetFramework="net45" />
I've checked logs and can see that AppInsights Status Monitor has been installed successully.
Any ideas?
UPD
Found SDK version: rddf:2.10.0-32157
Any ideas how can I fix it to rddp?
I'm Alex from Microsoft. My team works on cloud services extension. It appears we have a problem with installing Status Monitor for you to enable SQL tracking. My apologies.
Would that be possible for you to install Status Monitor by yourself as work-around on your Web and Worker roles as described here:
https://learn.microsoft.com/en-us/azure/azure-monitor/app/status-monitor-v2-api-enable-instrumentation-engine
?

No redirect to SingleSignOnService location when challenge occurs in Web Forms project

I've taken over a project and some package upgrading was necessary for other things so I went from these where things worked...
<package id="Kentor.AuthServices" version="0.18.0" targetFramework="net452" />
<package id="Kentor.AuthServices.Owin" version="0.18.0" targetFramework="net452" />
<package id="Microsoft.Owin" version="3.0.1" targetFramework="net452" />
<package id="Microsoft.Owin.Host.SystemWeb" version="3.0.1" targetFramework="net452" />
<package id="Microsoft.Owin.Security" version="3.0.1" targetFramework="net452" />
<package id="Microsoft.Owin.Security.Cookies" version="3.0.1" targetFramework="net452" />
To these...
<package id="Sustainsys.Saml2" version="2.2.0" targetFramework="net472" />
<package id="Sustainsys.Saml2.Owin" version="2.2.0" targetFramework="net472" />
<package id="Microsoft.Owin" version="4.0.0" targetFramework="net472" />
<package id="Microsoft.Owin.Host.SystemWeb" version="4.0.0" targetFramework="net472" />
<package id="Microsoft.Owin.Security" version="4.0.0" targetFramework="net472" />
<package id="Microsoft.Owin.Security.Cookies" version="4.0.0" targetFramework="net472" />
I followed the migration guide but fail to get a redirect to the SingleSignOnService location when a challenge occurs in my Web Forms project.
My Web.config has the following structure...
<sustainsys.saml2 entityId="https://demo.local/AuthServices"
returnUrl="https://demo.local"
publicOrigin="https://demo.local"
modulePath="/AuthServices">
<serviceCertificates>
<add fileName="~/somename.pfx"
use="Signing" />
</serviceCertificates>
<identityProviders>
<add entityId="My-IDP"
allowUnsolicitedAuthnResponse="true"
loadMetadata="true"
metadataLocation="https://some-saml2-idp.com/metadata" />
</identityProviders>
</sustainsys.saml2>
And my Owin startup...
var defaultSignInAsAuthType = "Cookies";
app.SetDefaultSignInAsAuthenticationType(defaultSignInAsAuthType);
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = defaultSignInAsAuthType,
ReturnUrlParameter = "returnUrl",
LoginPath = new PathString("/login"),
LogoutPath = new PathString("/logout")
});
var saml2Options = new Saml2AuthenticationOptions(true);
app.UseSaml2Authentication(saml2Options);
app.UseStageMarker(PipelineStage.Authenticate);
AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.Name;
I've tried triggering a challenge in a path mapping...
ctx.Authentication.Challenge(new AuthenticationProperties()
{
RedirectUri = "https://demo.local"
});
My questions are if I upgraded the Owin packages too far and if someone has some troubleshooting hints?
The default setting for active/passive has changed. Previously the middleware was active by default, which meant it listened to any Challenge call. Now it is passive, so you have to use the Challenge overload that specifies an authentication scheme and set it to "Saml2".
The reason for the change is to better follow best practice for how middleware for external authentication should behave.

TypeLoadException with DocumentClient on Visual Studio Mac

I downloaded the getting started sample app from Azure Cosmos DB and am getting the following error with Visual Studio Mac
System.TypeLoadException: Could not resolve type with token 010000f6 (from typeref, class/assembly System.Diagnostics.Eventing.EventProviderTraceListener, System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
at Microsoft.Azure.Documents.Client.DocumentClient.Initialize (System.Uri serviceEndpoint, Microsoft.Azure.Documents.Client.ConnectionPolicy connectionPolicy, System.Nullable`1[T] desiredConsistencyLevel) [0x00014] in <f7f11c3ada88490092c73d6bef54be97>:0
at Microsoft.Azure.Documents.Client.DocumentClient..ctor (System.Uri serviceEndpoint, System.String authKeyOrResourceToken, Microsoft.Azure.Documents.Client.ConnectionPolicy connectionPolicy, System.Nullable`1[T] desiredConsistencyLevel) [0x00069] in <f7f11c3ada88490092c73d6bef54be97>:0
at GraphGetStarted.Program.Main (System.String[] args) [0x00021] in /DocumentDB-Quickstart-DotNet-Graph/GraphGetStarted/Program.cs:29
And the code where it fails
DocumentClient client = new DocumentClient(url, authKey, policy);
Here's the packages.config: do I need to set a different targetFramework for Visual Studio Mac?
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Azure.DocumentDB" version="1.14.0" targetFramework="net452" />
<package id="Microsoft.Azure.Graphs" version="0.2.0-preview" targetFramework="net452" />
<package id="Microsoft.CodeAnalysis.Analyzers" version="1.1.0" targetFramework="net452" />
<package id="Microsoft.CodeAnalysis.Common" version="1.3.0" targetFramework="net452" />
<package id="Microsoft.CodeAnalysis.CSharp" version="1.3.0" targetFramework="net452" />
<package id="Newtonsoft.Json" version="10.0.2" targetFramework="net452" />
<package id="System.Collections" version="4.3.0" targetFramework="net452" />
<package id="System.Collections.Immutable" version="1.1.37" targetFramework="net452" />
<package id="System.Diagnostics.Debug" version="4.3.0" targetFramework="net452" />
<package id="System.Globalization" version="4.3.0" targetFramework="net452" />
<package id="System.Linq" version="4.3.0" targetFramework="net452" />
<package id="System.Reflection.Metadata" version="1.2.0" targetFramework="net452" />
<package id="System.Resources.ResourceManager" version="4.3.0" targetFramework="net452" />
<package id="System.Runtime" version="4.3.0" targetFramework="net452" />
<package id="System.Runtime.Extensions" version="4.3.0" targetFramework="net452" />
<package id="System.Threading" version="4.3.0" targetFramework="net452" />
</packages>
I think Protocol.Tcp is not is .NET Core. Can you try using
new ConnectionPolicy { ConnectionMode = ConnectionMode.Gateway, ConnectionProtocol = Protocol.Https }))
or
new ConnectionPolicy { ConnectionMode = ConnectionMode.Direct, ConnectionProtocol = Protocol.Https }))
Thanks!
According to your comment, you use .net project. Please don't try to run .net project on the MacOS. We can create .NET Core apps that run on Windows, Linux and macOS. We also could get info about azure DocumentDB and .NET Core from the official tutorials.

Missing Microsoft.Threading.Tasks with azure SDK

I'm attempting to use the Microsoft Azure SDK for .net version 0.9.5-preview. I am basically attempting to just get the sample for listing locations and services from the Readme working. However I'm running into an exception
An unhandled exception of type 'System.IO.FileNotFoundException' occurred in Microsoft.WindowsAzure.Management.dll
Additional information: Could not load file or assembly 'Microsoft.Threading.Tasks, Version=1.0.12.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
Information about this assembly is pretty sparse. I've found some references to it in relation to PCLs and windows phone but this is just a console application using the full 4.5.1(although I have also tried 4.5).
I've found a copy of the dll in a google repository and throwing that in the bin directory does make the application work, however that seems like a spectacular hack. I'd rather see a better solution. Any ideas?
The packages.config contents are
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.WindowsAzure.Common" version="0.9.8-preview" targetFramework="net45" />
<package id="Microsoft.WindowsAzure.Management" version="0.9.5-preview" targetFramework="net45" />
<package id="Microsoft.WindowsAzure.Management.Compute" version="0.9.9-preview" targetFramework="net45" />
<package id="Microsoft.WindowsAzure.Management.Libraries" version="0.9.6-preview" targetFramework="net45" />
<package id="Microsoft.WindowsAzure.Management.Monitoring" version="0.9.2-preview" targetFramework="net45" />
<package id="Microsoft.WindowsAzure.Management.Scheduler" version="0.9.8-preview" targetFramework="net45" />
<package id="Microsoft.WindowsAzure.Management.ServiceBus" version="0.9.3-preview" targetFramework="net45" />
<package id="Microsoft.WindowsAzure.Management.Sql" version="0.9.4-preview" targetFramework="net45" />
<package id="Microsoft.WindowsAzure.Management.Storage" version="0.9.3-preview" targetFramework="net45" />
<package id="Microsoft.WindowsAzure.Management.VirtualNetworks" version="0.9.3-preview" targetFramework="net45" />
<package id="Microsoft.WindowsAzure.Management.WebSites" version="0.9.3-preview" targetFramework="net45" />
</packages>
Microsoft.Threading.Tasks assembly you are missing is in Microsoft.Bcl.Async NuGet package. Not sure why but your packages.config file is missing a bunch of dependent assemblies. You may want to un-install and install again your NuGet packages to bring back the dependencies.
Can you verify that you have the latest versions of the dependent assemblies from the dependencies NuGet package, installed with -Pre?
http://www.nuget.org/packages/Microsoft.WindowsAzure.Common.Dependencies/

Resources