FileNotFoundException: Could not load file or assembly 'System.Data.Entity' - c#

I am trying to parse a dynamic Expression string using Linq
var x = Expression.Parameter(typeof(T), "x");
var e = Dynamic.DynamicExpression.ParseLambda(new[] { x }, null, "x.Id > 1");
var compiledDelegate = exp.Compile();
var values = new List<T>
{
new T
{
Id = 1
},
new T
{
Id = 2
}
};
var result = values.Where((Func<T, bool>)compiledDelegate);
When trying to execute the ParseLambda line, the code throws the below exception
I am using net standard 2.0 and Visual Studio 15.3.5.
I am pulling the below two packages with all the necessary ASP.NET core stuff.
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<NeutralLanguage>en</NeutralLanguage>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Linq.Dynamic" Version="1.0.7" />
<PackageReference Include="System.Linq.Expressions" Version="4.3.0" />
</ItemGroup>
<PropertyGroup>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>

With more googling I found that System.Data is not yet ported (if it will ever be) to .net standard 2.0 APIs so System.Linq.Dynamic was actually not fully compatible with .net standard 2.0.
Instead I found another port of the same assembly System.Linq.Dynamic.Core which did the job.

Related

How to handle System.MissingMethodException in Lucent.net dll?

I new to Lucene & C#. I am trying to replicate the example given on Lucene.net tutorial but when i running the code it is showing
Unhandled exception. System.MissingMethodException: Method not found: 'System.IO.MemoryMappedFiles.MemoryMappedFile System.IO.MemoryMappedFiles.MemoryMappedFile.CreateFromFile(System.IO.FileStream, System.String, Int64, System.IO.MemoryMappedFiles.MemoryMappedFileAccess, System.IO.MemoryMappedFiles.MemoryMappedFileSecurity, System.IO.HandleInheritability, Boolean)'.
at Lucene.Net.Store.MMapDirectory.Map(MMapIndexInput input, FileStream fc, Int64 offset, Int64 length)
at Lucene.Net.Store.MMapDirectory.MMapIndexInput..ctor(MMapDirectory outerInstance, String resourceDescription, FileStream fc)
at Lucene.Net.Store.MMapDirectory.OpenInput(String name, IOContext context)
at Lucene.Net.Store.Directory.Copy(Directory to, String src, String dest, IOContext context)
at Lucene.Net.Store.TrackingDirectoryWrapper.Copy(Directory to, String src, String dest, IOContext context)
at Lucene.Net.Index.IndexWriter.CreateCompoundFile(InfoStream infoStream, Directory directory, CheckAbort checkAbort, SegmentInfo info, IOContext context)
at Lucene.Net.Index.DocumentsWriterPerThread.SealFlushedSegment(FlushedSegment flushedSegment)
at Lucene.Net.Index.DocumentsWriterPerThread.Flush()
at Lucene.Net.Index.DocumentsWriter.DoFlush(DocumentsWriterPerThread flushingDWPT)
at Lucene.Net.Index.DocumentsWriter.FlushAllThreads(IndexWriter indexWriter)
at Lucene.Net.Index.IndexWriter.DoFlush(Boolean applyAllDeletes)
at Lucene.Net.Index.IndexWriter.Flush(Boolean triggerMerge, Boolean applyAllDeletes)
I don't know to handle this error or what is error. this error occurring writing the folder , sometimes reading the folder it's just so random .
my code
const LuceneVersion luceneVersion = LuceneVersion.LUCENE_48;
//Open the Directory using a Lucene Directory class
string indexName = "example_index";
string indexPath = Path.Combine(Environment.CurrentDirectory, indexName);
FSDirectory indexDir = FSDirectory.Open(new DirectoryInfo(indexPath));
//Create an analyzer to process the text
Analyzer standardAnalyzer = new StandardAnalyzer(luceneVersion);
//Create an index writer
IndexWriterConfig indexConfig = new IndexWriterConfig(luceneVersion, standardAnalyzer);
//indexConfig.OpenMode = OpenMode.CREATE; // create/overwrite index
IndexWriter writer = new IndexWriter(indexDir, indexConfig);
//Add three documents to the index
Document doc = new Document();
doc.Add(new TextField("titleTag", "The Apache Software Foundation - The world's largest open source foundation.", Field.Store.YES));
doc.Add(new StringField("domain", "www.apache.org/", Field.Store.YES));
writer.AddDocument(doc);
doc = new Document();
doc.Add(new TextField("title", "Powerful open source search library for .NET", Field.Store.YES));
doc.Add(new StringField("domain", "lucenenet.apache.org", Field.Store.YES));
writer.AddDocument(doc);
doc = new Document();
doc.Add(new TextField("title", "Unique gifts made by small businesses in North Carolina.", Field.Store.YES));
doc.Add(new StringField("domain", "www.giftoasis.com", Field.Store.YES));
writer.AddDocument(doc);
// //Flush and commit the index data to the directory
writer.Flush(true,true) ;
writer.Commit();
writer.Dispose() ;
using DirectoryReader reader = writer.GetReader(applyAllDeletes: true);
IndexSearcher searcher = new IndexSearcher(reader);
Query query = new TermQuery(new Term("domain", "lucenenet.apache.org"));
TopDocs topDocs = searcher.Search(query,1); //indicate we want the first 2 results
int numMatchingDocs = topDocs.TotalHits;
Document resultDoc = searcher.Doc(topDocs.ScoreDocs[0].Doc); //read back first doc from results (ie 0 offset)
string title = resultDoc.Get("title");
Console.WriteLine($"Matching results: {topDocs.TotalHits}");
Console.WriteLine($"Title of first result: {title}");
as comments suggest i am adding .csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp6.0.0</TargetFramework>
<TargetFrameworkVersion>v6.0.0</TargetFrameworkVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Lucene.Net" Version="4.8.0.770-beta" />
<PackageReference Include="Lucene.Net.Analysis.Common" Version="4.8.0.770-beta" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="6.0.0" />
</ItemGroup>
</Project>
Problem
The problem is that you are using a really old pre-release version of Lucene.NET prior to when .NET Core support was added which uses a confusing version numbering scheme, 4.8.0.770-beta and was released only on MyGet.org. This version will not support .NET 6.0, it only supports .NET Framework.
<ItemGroup>
<PackageReference Include="Lucene.Net" Version="4.8.0.770-beta" />
<PackageReference Include="Lucene.Net.Analysis.Common" Version="4.8.0.770-beta" />
</ItemGroup>
Solution
To fix this, use one of the beta versions on NuGet.org instead.
<ItemGroup>
<PackageReference Include="Lucene.Net" Version="4.8.0-beta00016" />
<PackageReference Include="Lucene.Net.Analysis.Common" Version="4.8.0-beta00016" />
</ItemGroup>
Also, be sure to change the target framework moniker to net6.0 instead of net6.0.0. TargetFrameworkVersion is unnecessary for .NET 6.0.
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp6.0</TargetFramework>
</PropertyGroup>
This error seems like dependency version problem of your dll's.
To check dependencies of your project, first check package versions in csproj file of project.
Then check package's(dll's) version by checking properties of dll files wherever you are deploying your project.
If both files versions are different you got your problem.
You need to copy all dll's which are present in csproj file.
You'll get all your dll's inside bin/release folder wherever you are creating build.

Py.GIL() is giving error pythonnet embedded python in C#

I have the following C# code:
static void Main()
{
string pythonpath1 = #"C:\Users\user\Documents\pynet_test\Python\Python37";
string pythonpath2 = #"C:\Users\user\Documents\pynet_test\Python\Python37\lib";
string envpythonhome = #"C:\Users\user\Documents\pynet_test\Python\Python37\python37.dll";
Environment.SetEnvironmentVariable("PYTHONNET_PYDLL", envpythonhome, EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("PATH", pythonpath1, EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("PYTHONHOME", pythonpath1, EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("PYTHONPATH", pythonpath2, EnvironmentVariableTarget.Process);
using (Py.GIL())
{
dynamic np = Py.Import("numpy");
Console.WriteLine(np.cos(np.pi * 2));
dynamic sin = np.sin;
Console.WriteLine(sin(5));
double c = np.cos(5) + sin(5);
Console.WriteLine(c);
dynamic a = np.array(new List<float> { 1, 2, 3 });
Console.WriteLine(a.dtype);
dynamic b = np.array(new List<float> { 6, 5, 4 }, dtype: np.int32);
Console.WriteLine(b.dtype);
Console.WriteLine(a * b);
}
Console.WriteLine(DateTime.Now);
}
The error I am getting is:
System.MissingMethodException
HResult=0x80131513
Message=Method not found: 'System.Reflection.Emit.AssemblyBuilder System.AppDomain.DefineDynamicAssembly(System.Reflection.AssemblyName, System.Reflection.Emit.AssemblyBuilderAccess)'.
Source=Python.Runtime
StackTrace:
at Python.Runtime.CodeGenerator..ctor()
at Python.Runtime.DelegateManager..ctor()
at Python.Runtime.PythonEngine.Initialize(IEnumerable`1 args, Boolean setSysArgv, Boolean initSigs)
at Python.Runtime.PythonEngine.Initialize(Boolean setSysArgv, Boolean initSigs)
at Python.Runtime.PythonEngine.Initialize()
at Python.Runtime.Py.GIL()
at WrapperPython.Program.Main() in C:\Users\user\Documents\pynet_test\pynet_test\Program.cs:line 50
The Python environment is in the Project folder and this is the following specifications:
python version used : 3.7 (x64)
pythonnet version: 2.5.2-cp37-cp37m-win_amd64
OS : Windows Server 2019
Reference has been made to the python.Runtime.dll under site-packaged. CSproj looks like following:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<Reference Include="Python.Runtime">
<HintPath>..\Python\Python37\Lib\site-packages\Python.Runtime.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
I have tried everything provided online but can't seem to find the issue. I have a hunch it's based on the environment variables but not sure how to proceed.
okay so python.net installation is really not well documented and the folks maintaining the python.net repository don't really help a lot since it's not a "support forum".
I solved this issue by installing the python.runtime.AllPlatflorms nuget package and pointing the environment variables to the right python folders/ files.
This works with python3.8 as well.

Azure function binding to container error when unzipping file to blob storage

I have been trying to set up a Blobtrigger function that when a zip file is uploaded to a container it unzips and uploads it to the same container. I got the solution from Azure Function: Unzip file works in debug but not in production.
public static class Function1
{
[FunctionName("Function1")]
public static async Task Run([BlobTrigger("raw-nppes/{inputBlobName}", Connection = "key")] Stream inputBlob, string inputBlobName,
Binder binder,
ILogger log)
{
log.LogInformation($"Blob trigger function received blob\n Name:{inputBlobName} \n Size: {inputBlob.Length} Bytes");
if (Path.GetExtension(inputBlobName)?.ToLower() == ".zip")
{
// We use the first char of the input file name as a dynamic part in the container. (Note: You should check if this is a valid char for the container name)
var container = $"my-dynamic-container-{inputBlobName.Substring(0, 1).ToLower()}";
var attributes = new Attribute[]
{
new BlobAttribute($"{container}", FileAccess.ReadWrite),
new StorageAccountAttribute("AzureWebJobsStorage")
};
var outputContainer = await binder.BindAsync<CloudBlobContainer>(attributes);
await outputContainer.CreateIfNotExistsAsync();
var archive = new ZipArchive(inputBlob);
foreach (var entry in archive.Entries)
{
// we write the output files to a directory with the same name as the input blob. Change as required
var blockBlob = outputContainer.GetBlockBlobReference($"{inputBlobName}/{entry.FullName}");
using (var fileStream = entry.Open())
{
if (entry.Length > 0)
{
log.LogInformation($"Extracting - {entry.FullName} to - {blockBlob.Name}");
await blockBlob.UploadFromStreamAsync(fileStream);
}
}
}
}
else
{
log.LogInformation("Not a zip file. Ignoring");
}
}
}
}
I got this error: Microsoft.Azure.WebJobs.Host: Can't bind Blob to type 'Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer'
with this possible cause: Tried binding to 'Microsoft.Azure.Storage.Blob.CloudBlobDirectory, Microsoft.Azure.Storage.Blob, Version=11.1.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' but user type assembly was 'Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer, Microsoft.WindowsAzure.Storage, Version=9.3.1.0
I don't know what that possible cause actually means. I have looked EVERYWHERE on here, github, and social.msdn but no answer. I have no idea what the issue is.
It seems that your assembly is too old, please install the latest Microsoft.Azure.Storage.Blob assembly:
https://www.nuget.org/packages/Microsoft.Azure.Storage.Blob/11.2.2
Then use using Microsoft.Azure.Storage.Blob; instead of using Microsoft.WindowsAzure.Storage.Blob;.
The csproj file like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AzureFunctionsVersion>v3</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Storage.Blob" Version="11.2.2" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="4.0.3" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.11" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
Regarding the difference between them, you can refer to this post:
What is the difference between the Microsoft.Azure.Storage and WindowsAzure.Storage Nuget packages?
Pay attention on the "Important" section of the Changelog!
I was doing the same error.
https://github.com/Azure/azure-webjobs-sdk/releases/tag/storage-v4.0.0
Important Storage namespaces have changed, so you may need to update
your code if you are using any of the Storage types directly. Your
code may continue to compile as you may have an implicit reference to
the old Storage SDK, but it will fail at runtime if these have not
been updated in your code:
For blobs and queues, Microsoft.WindowsAzure.Storage.* namespaces are
now Microsoft.Azure.Storage.. For tables, the namespaces are now
Microsoft.Azure.Cosmos.Table..
Check your using ;)

'AvroDeserializer<GenericRecord>' does not contain a definition for 'AsSyncOverAsync' and no accessible extension method

I have update my unity project dlls and after that i am getting this error
'AvroDeserializer' does not contain a definition for
'AsSyncOverAsync' and no accessible extension method 'AsSyncOverAsync'
accepting a first argument of type 'AvroDeserializer'
could be found (are you missing a using directive or an assembly
reference?)
at this line
.SetValueDeserializer(new AvroDeserializer<GenericRecord>(schemaRegistry).AsSyncOverAsync())
I don't know why this is not method is not available. Here are my dll files:
I've managed to reproduced a small solution only with this code in program.cs, I can build the solution and see the AsSyncOverAsync extension method which located in Confluent.Kafka.SyncOverAsync namespace.
class Program
{
static void Main(string[] args)
{
IEnumerable<KeyValuePair<string, string>> consumerConfig = new List<KeyValuePair<string, string>>();
using (var schemaRegistry = new CachedSchemaRegistryClient(new SchemaRegistryConfig { }))
using (var consumer =
new Confluent.Kafka.ConsumerBuilder<string, GenericRecord>(consumerConfig)
.SetValueDeserializer(new AvroDeserializer<GenericRecord>(schemaRegistry).AsSyncOverAsync())
.Build()) { }
}
}
This is the *.csproj file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Confluent.Kafka" Version="1.4.0" />
<PackageReference Include="Confluent.SchemaRegistry.Serdes.Avro" Version="1.4.0" />
</ItemGroup>
</Project>
It looks like you are missing the Confluent.SchemaRegistry.Serdes.Avro package so try manually install them both from Nuget.

Is there an easy way to add multiple projects to a solution?

In some solutions I use direct references to internal projects during development and switch to nuget references for release. However, adding 30+ projects to new solutions manually is a tedious task.
I was wondering whether there is a quicker way to do this. With projects it's pretty easy because you just copy/paste the xml but solution files are not that easy to edit but maybe there is some trick?
You can use the dotnet CLI.
For Linux:
dotnet sln MySolution.sln add **/*.csproj
For Windows PowerShell:
dotnet sln MySolution.sln add (Get-ChildItem -Recurse *.csproj)
I've done some reaserch on the visualstudio extensibility based on #JamesFaix link and I managed to put together this small piece of code:
using System;
using System.IO;
using System.Linq;
using EnvDTE;
using EnvDTE80;
class Program
{
static void Main(string[] args)
{
// VS2019
var dteType = Type.GetTypeFromProgID("VisualStudio.DTE.16.0", true);
var dte = (EnvDTE.DTE)System.Activator.CreateInstance(dteType);
var sln = (SolutionClass)dte.Solution;
// Solution to add projects to
sln.Open(#"C:\Projects\MyProject\MySolution.sln");
// Projects should be added to the "lib" solution-folder.
var lib = FindSolutionFolderOrCreate(sln, "lib");
// These projects should be added.
var projectPaths = new[]
{
#"C:\Projects\MyLibs\Lib1.csproj",
#"C:\Projects\MyLibs\Lib2.csproj"
};
foreach (var path in projectPaths)
{
var name = Path.GetFileNameWithoutExtension(path);
// If project not already in the solution-folder then add it.
if(!(lib.Parent.ProjectItems.Cast<ProjectItem>().Any(pi => pi.Name == name)))
{
lib.AddFromFile(path);
}
}
dte.Solution.Close(true);
}
private static SolutionFolder FindSolutionFolderOrCreate(SolutionClass sln, string folderName)
{
foreach (var x in sln.Projects)
{
if (x is Project p && p.Name.Equals(folderName, StringComparison.OrdinalIgnoreCase))
{
return (SolutionFolder)p.Object;
}
}
var proj = (sln as Solution2).AddSolutionFolder(folderName);
return (SolutionFolder)proj.Object;
}
}
*.csproj file of this utility:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<!--<TargetFramework>netcoreapp2.2</TargetFramework>-->
<TargetFramework>net47</TargetFramework>
<EnableUnmanagedDebugging>true</EnableUnmanagedDebugging>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebugType>full</DebugType>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>
<ItemGroup>
<Reference Include="EnvDTE">
<HintPath>..\..\..\..\..\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\PublicAssemblies\envdte.dll</HintPath>
</Reference>
<Reference Include="EnvDTE80">
<HintPath>..\..\..\..\..\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\PublicAssemblies\envdte80.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
I'm using net47 here because netcoreapp doesn't allow you to debug COM objects which makes it really a painful experience.
You'll also need references to the two EnvDTE files.

Categories