On Startup.cs in my Azure Function v2 project:
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using MyCompany.MyLib.Contracts; //namespace from external library
[assembly: FunctionsStartup(typeof(Startup))]
namespace Test
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddTransient(typeof(Logging.ILogger<>), typeof(Logging.Logger<>));
builder.Services.AddTransient<IUserLogic, UserLogic>();
builder.Services.AddTransient<IBillingLogic, BillingLogic>(); //---> loads up from above referenced "MyCompany.MyLib.Contracts" namespace and this namespace is from externally referenced class library but with in same solution
}
}
}
The above code with my own custom classes within function app project like "EmailLogic", "Logger" works fine.
But the moment I added up custom classes to services container like "BillingLogic" from external C# library project which is added as reference project from the existing visual studio solution it throws up below issue:
"A host error has occurred during startup operation '945918c0-af3a-4d50-ab1d-ac405d4f1c7b'. [2/3/2020 2:11:02 PM] MyFunction.FunctionApp: Could not load file or assembly 'MyCompany.MyLib.Contracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Could not find or load a specific file. (Exception from HRESULT: 0x80131621). System.Private.CoreLib: Could not load file or assembly ''MyCompany.MyLib.Contracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
If these lines from "referenced external projects" are removed,
using MyCompany.MyLib.Contracts;
builder.Services.AddTransient<IBillingLogic, BillingLogic>();
startup.cs works as expected but referring this external class from referenced project is must for my solution.
My Azure function csproj file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<AzureFunctionsVersion>v2</AzureFunctionsVersion>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<AutoGenerateBindingRedirects>false</AutoGenerateBindingRedirects>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.0.0" />
<PackageReference Include="Microsoft.Azure.Storage.Queue" Version="11.1.2" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="3.0.8" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.29" />
<PackageReference Include="NLog" Version="4.6.8" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="NLog.Extensions.AzureStorage" Version="1.1.4" />
<PackageReference Include="Microsoft.Azure.DocumentDB.Core" Version="2.9.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MyCSharpLib.DataStore\MyCSharpLib.DataStore.csproj">
<Private>true</Private>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToPublishDirectory>Always</CopyToPublishDirectory>
</None>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
<None Update="nlog.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToPublishDirectory>Always</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
MyCSharpLib.DataStore.csproj file:
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<RuntimeIdentifier>win7-x64</RuntimeIdentifier>
<Platforms>x64</Platforms>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" />
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Cosmos.Table" Version="1.0.6" />
<PackageReference Include="Microsoft.Azure.DocumentDB.Core" Version="2.9.2" />
<PackageReference Include="Microsoft.Azure.Storage.Blob" Version="11.1.1" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Newtonsoft.Json.Bson" Version="1.0.1" />
<PackageReference Include="Polly" Version="5.3.1" />
<PackageReference Include="StackExchange.Redis" Version="1.2.6" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MyContractLib.Contracts\MyContractLib.Contracts.csproj" />
</ItemGroup>
</Project>
MyCSharpLib.DataStore
.\MyContractLib.Contracts\MyContractLib.Contracts.csproj
My Azure function csproj file:
<ProjectReference Include="..\MyCSharpLib.DataStore\MyCSharpLib.DataStore.csproj">
so
using MyCompany.MyLib.Contracts;
is coming through the ref to DataStore which then has ref to MyContractLib.Contracts
But it is not coping the dll as its silly, so either get Azure function csproj to ref MyLib.Contracts
or do this
How to set dependencies when I use .NET Standard 2.0 DLL libraries with a .NET Framework console application?
which is on all your std libs add
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
so on both your standard libs
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
</PropertyGroup>
if this does not work i will delete
Related
I have an Azure function(V3) that uses BlobTrigger binding and written in C#.
In order to add custom properties in Application Insights RequestTelemetry for it using
Activity.Current?.AddTag("TraceId", traceId);
I need to access the Activity.Current based on the suggestion from this Stackoverflow answer. However, it didn't work due to Activity.Current is NULL.
My package configuration looks like as follow:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AzureFunctionsVersion>v3</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Storage.Blobs" Version="12.9.0" />
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.14.0" />
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="5.0.0-beta.2" />
<PackageReference Include="Microsoft.Azure.WebJobs.Logging.ApplicationInsights" Version="3.0.14" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.8" />
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.3" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="5.0.1" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
And my function looks like as follow:
[FunctionName("Create-Thumbnail")]
public async Task CreateThumbnail([BlobTrigger("input/{name}", Source = BlobTriggerSource.EventGrid, Connection = "AzureWebJobsStorage")] Stream image,
IDictionary<string,string> metadata,
string name,
ILogger log,
ExecutionContext context)
{
Activity.Current?.AddTag("TraceId", traceId);
}
I have been doing research for whole day but failed to find any solution. Does anyone know what could be the issue?
Currently it can be working in HTTP Trigger Functions that were ending up in the Custom Properties of Requests in application insights are no longer. Refer here
The same Activity.Current value is null issue available in github Azure Function host & Application Insights
The Application Insights .NET SDK uses DiagnosticSource (DiagnosticSourceUsersGuide) and Activity (ActivityUserGuide) steps to collect and correlate telemetry.
Please open an issue in the Functions repo link here.
Thanks to this GitHub issue, I manage to get it work after downgrading System.Diagnostics.DiagnosticSource to to version 4.6
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="4.6.0" />
I am trying to use
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.Id("logonIdentifier")));
However I am getting an error Could not load file or assembly 'SeleniumExtras.WaitHelpers. I am using
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DotNetSeleniumExtras.WaitHelpers" Version="3.11.0" />
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="TestProject.SDK" Version="0.63.1" />
</ItemGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<IsPackable>false</IsPackable>
<PublishChromeDriver>true</PublishChromeDriver>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DotNetSeleniumExtras.WaitHelpers" Version="3.11.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.0" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.0" />
<PackageReference Include="coverlet.collector" Version="1.2.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\testForLogin\testForLogin.csproj" />
</ItemGroup>
</Project>
It works locally but when I tried pushing it to the testproject.io I get the above error message.
I am not able to change it to the recommend selenium web driver version.
The package you are using is not .NET Core compliant. With some restructure of the code what you need is available with
using OpenQA.Selenium.Support.UI;
Then take the code that used the SeleniumExtras wait helpers and change to use methods on your driver class such as
WebDriverWait browserWait = new WebDriverWait(driver, TimeSpan.FromSeconds(90));
browserWait.Until(driver => driver.FindElement(By.Id("logonIdentifier")).Displayed);
You could easily check the properties on the driver as well, say for the Url, such as
const string expectedUrl = "https://www.google.com";
browserWait.Until(driver => driver.Url == expectedUrl);
Then go remove the package you are no longer using from your project
dotnet remove package DotNetSeleniumExtras.WaitHelpers
Which funny enough is pretty much the homepage of https://www.selenium.dev/documentation/en/ but when you search you tend to get distracted by the SeleniumExtras project.
When reflecting into a unittests assembly (System.Reflection.Assembly.GetTypes()) from a console app I get a slew of file not found errors.
I tried adding a bunch of packages with minimal success, I'm unsure that is the way to go.
This is my project file.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="MSTest.TestAdapter" Version="1.4.0" />
<PackageReference Include="MSTest.TestFramework" Version="1.4.0" />
<PackageReference Include="PossumLabs.Specflow.Core" Version="1.0.0-CI00024" />
<PackageReference Include="PossumLabs.Specflow.Selenium" Version="1.0.0-CI00030" />
<PackageReference Include="Selenium.Chrome.WebDriver" Version="74.0.0" />
<PackageReference Include="Selenium.WebDriver" Version="3.141.0" />
<PackageReference Include="SpecFlow" Version="3.0.225" />
<PackageReference Include="SpecFlow.MsTest" Version="3.0.225" />
<PackageReference Include="SpecFlow.Tools.MsBuild.Generation" Version="3.0.225" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="4.5.1" />
</ItemGroup>
</Project>
Could not load file or assembly
'Microsoft.VisualStudio.TestPlatform.ObjectModel, Version=11.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot
find the file specified.
Same goes for
Microsoft.VisualStudio.TestPlatform.TestFramework
Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface
Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices
Microsoft.VisualStudio.TestPlatform.ObjectModel
Microsoft.VisualStudio.TestPlatform.TestFramework
I want to get these to copy to my output folder so that I can reflect into the unit test DLL.
No Answer, just workaround;
This happened when reflecting into a Microsoft DLL; it seems that in .net core reflection changed a little bit and just blindly reflecting over every dll in a folder is no longer safe. Filtering the Dll by their dependency was possible, and that if how I narrow the scope; But even though I load every DLL the GetTypes will throw an error as mentioned above.
How you filter is less important than that you filter down what DLL's you look into.
Both start the same
foreach (string dll in Directory.GetFiles(path, "*.dll"))
assemblies.Add(Assembly.LoadFile(dll));
Bad code:
foreach (var assembly in assemblies)
{}
Good Code:
foreach (var assembly in assemblies.Where(a=>a.GetReferencedAssemblies().Any(x=>x.Name.Contains("YourCompanyNamespace"))))
{}
I am experimenting with Visual Studio on the Mac. I have a _layout.cshtml file that works fine in the Windows version of VS but I am getting multiple errors when executing it on the Mac.
The error is:
One or more compilation references are missing. Ensure that your project is referencing 'Microsoft.NET.Sdk.Web' and the 'PreserveCompilationContext' property is not set to false.
And it is flagging many common razor function such as
The type or namespace name 'HtmlString' could not be found (are you missing a using directive or an assembly reference?)
private HtmlString WriteOption(string Menu)
The name 'Styles' does not exist in the current context
#Styles.Render( "~/Content/css" )
The name 'Scripts' does not exist in the current context
#Scripts.Render( "~/bundles/modernizr" )
The name 'Session' does not exist in the current context
int CurrentCustomerId = (Session["CurrentCustomerId"] == null ? -1 : (int)Session["CurrentCustomerId"]);
I've tried numerous things such as
adding and removing Microsoft.AspNetCore
ensuring PreserveCompilationContext is set to true
adding Microsoft.AspNetCoreRazor packages
adding: #using HttpContext.Current;
adding: #using System.Web.Optimization;
rebooting, cleaning and rebuilding....
This is what my .csproj file looks like:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<PreserveCompilationContext>true</PreserveCompilationContext>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="jQuery" Version="3.3.1" />
<PackageReference Include="bootstrap" Version="4.0.0" />
<PackageReference Include="log4net" Version="2.0.8" />
<PackageReference Include="FontAwesome" Version="4.7.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="2.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Design" Version="1.1.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.0.1" />
<PackageReference Include="EntityFramework" Version="6.2.0" />
<PackageReference Include="Microsoft.AspNetCore" Version="2.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.2" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor" Version="2.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.0.2" />
<PackageReference Include="Microsoft.AspNet.Razor" Version="3.2.3" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
<Folder Include="Data\" />
<Folder Include="Views\Customer\" />
<Folder Include="Views\Contact\" />
<Folder Include="Views\User\" />
<Folder Include="wwwroot\img\" />
<Folder Include="Helpers\" />
</ItemGroup>
<ItemGroup>
<Compile Remove="Views\Customer\Index.cshtml.cs" />
</ItemGroup>
<ItemGroup>
<Content Remove="Views\Home\About.cshtml" />
<Content Remove="Views\Home\Contact.cshtml" />
<Content Remove="wwwroot\images\banner1.svg" />
<Content Remove="wwwroot\images\banner2.svg" />
<Content Remove="wwwroot\images\banner3.svg" />
<Content Remove="wwwroot\images\banner4.svg" />
</ItemGroup>
</Project>
Here's the IDE vitals:
Visual Studio Community 2017 for Mac
Version 7.3.3 (build 12)
Installation UUID: 0237be3a-3db7-4f5c-af82-a9699df5a1f9
Runtime:
Mono 5.8.0.108 (2017-10/9aa78573ee2) (64-bit)
GTK+ 2.24.23 (Raleigh theme)
Package version: 508000108
NuGet
Version: 4.3.1.4445
.NET Core
Runtime: /usr/local/share/dotnet/dotnet
Runtime Version: 2.0.5
SDK: /usr/local/share/dotnet/sdk/2.1.4/Sdks
SDK Version: 2.1.4
MSBuild SDKs: /Library/Frameworks/Mono.framework/Versions/5.8.0/lib/mono/msbuild/15.0/bin/Sdks
Any suggestions?
There is a difference between "ASP.NET Web Application" and "ASP.NET Core Web Application". Your project is of the first type which uses .Net Framework and does not work on Mac. The "Core" type uses .Net Core and is supported on Mac.
The project types have some differences, like the absence of #Styles and #Scripts. More differences can be found here: http://www.mithunvp.com/difference-between-asp-net-mvc6-asp-net-mvc5/
If you would like to migrate your project, you can use this guide: https://learn.microsoft.com/en-us/aspnet/core/migration/mvc
I have a unit Test that checks that all my service methods make a call to some authorization method.
I'm checking this in a unit Test using Roslyn.
The test runs fine on my local machine but fails on the build server, because project.Documents is empty.
I'm using VS 2017 and mstest V2.
The build server is a private on prem build agent in VSTS.
[TestMethod]
public async Task All_public_Service_Methods_should_check_authorization()
{
var workspace = MSBuildWorkspace.Create();
var project = await workspace.OpenProjectAsync(pathToCsprojFile).ConfigureAwait(false);
Console.WriteLine($"AssemblyName is {project.AssemblyName}"); // same output
Console.WriteLine($"FilePath is {project.FilePath}"); // same output
Console.WriteLine($"Documents are {string.Join(Environment.NewLine,project.Documents.Select(d => d.FilePath))}"); // empty on build server
//...
}
This is the csproj, that I'm trying to load
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard1.5</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<DocumentationFile>bin\Debug\netstandard1.5\adremes.Exchange.Services.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<DocumentationFile>bin\Release\netstandard1.5\adremes.Exchange.Services.xml</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="adremes.Common" Version="1.4.0" />
<PackageReference Include="AutoMapper" Version="5.2.0" />
<PackageReference Include="AutoMapper.Collection" Version="2.1.2" />
<PackageReference Include="DocumentFormat.OpenXml" Version="2.7.1" />
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="1.1.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\adremes.Exchange.Common\adremes.Exchange.Common.csproj" />
<ProjectReference Include="..\adremes.Exchange.Data\adremes.Exchange.Data.csproj" />
</ItemGroup>
</Project>