Adding CsWinRT nuget breaks using Windows.System namespace - c#

I've created a .NET 5.0 project, one of the dependencies I have is on this API:
AnalyticsInfo.VersionInfo.DeviceFamily
After installing Microsoft.Windows.SDK.Contracts, I'm able to use this API. Then, I needed to install the Microsoft.Windows.CsWinRT package to resolve this error:
Error NETSDK1130 Referencing a Windows Metadata component directly when targeting
.NETCoreApp,Version=v5.0 is not supported.
Use the C#/WinRT projection tool (https://aka.ms/cswinrt) or a provided projection for this target.
After installing this, I no longer have access to the Windows.System.Profile namespace to call the AnalyticsInfo API:
Error CS0234 The type or namespace name 'System' does not exist in the namespace 'Windows' (are you missing an assembly reference?)

With .NET 5, built-in support for WinRT APIs in .NET is removed (because it's Windows specific) , so we can't use Microsoft.Windows.SDK.Contracts any more.
The solution as explained here Built-in support for WinRT is removed from .NET is to
Remove references to the Microsoft.Windows.SDK.Contracts package.
Instead, specify the version of the Windows APIs that you want to
access via the TargetFramework property of the project. For example:
<TargetFramework>net5.0-windows10.0.19041</TargetFramework>
Note with that in place, there's no need to manually add a reference to C#/WinRT (Microsoft.Windows.CsWinRT) it should be done automatically and shown as "Microsoft.Windows.SDK.NET.Ref" in the list of Frameworks Dependencies.

Related

Can't add System.Drawing namespace in C# console application

I'm trying to add the System.Drawing namespace in my C# console application but when I go to use it's 'Image' type, I get the error:
"The type name 'Image' could not be found it the namespace 'System.Drawing'. This type has been forwarded to assembly System.Drawing.Common, Version=4.0.20, Culture=neutral, Consider adding reference to that assembly".
I have already went to Project>Add Reference>COM>System.Drawing.dll>Select>OK to add it but the error is still there.
Don't use the "COM" section in that dialog. Use "Assemblies" instead:
Note how this gives newer versions (4.0) instead of old versions (2.0 / 2.4).
If there is no "Assemblies" section, check that you really created a C# .NET Framework project. Likely you chose C# .NET Core.
If you want to stick with .NET Core, use the Nuget package System.Drawing.Common

.NET Code System.Web install source

Using Visual Studio Code on my mac book on sort of a Hello World type app. However, when I try to include a reference to System.Web, I get an alert stating:
The type or namespace name 'Web' does not exist in the namespace
'System' (are you missing an assembly reference?) [netcoreapp1.0]
Unnecessary using directive. [netcoreapp1.0]
I obviously supposed to add a reference System.Web, but giving the relative newness of this, I'm not sure of which nuget package I should apply to the project.
Any advice will be helpful.
Removed System.Web dependence is one of the benefit of ASP.NET Core, that by the way allows to build and run cross-platform ASP.NET apps. From ASP.NET Core documentation:
ASP.NET Core has a number of architectural changes that result in a much leaner and modular framework. ASP.NET Core is no longer based on System.Web.dll. It is based on a set of granular and well factored NuGet packages.
Don't know about source with nuget package descriptions, but you can start from searching in https://github.com/aspnet for appropriate project

HttpUtility not recognised in .Net 4.5

I Developed a WinForm application in with the target framework set to .net 4.0, now I wish to add to a project that has it's target framework set to .net 4.5. After I added the 4.0 WinForm application to my 4.5 project I keep getting the an error on my HttpUtility object.
data += "&batch_data=" + HttpUtility.UrlEncode(batch, System.Text.Encoding.GetEncoding("ISO-8859-1"));
"The name 'HttpUtility' does not exist in the current context"
I did include the System.Web namespace where the HttpUtility is located.
Visual Studio Error:
CS0234 The type or namespace name 'HttpUtility' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)
The problem is somewhere else.
As you can see in MSDN the HttpUtility class is present in System.Web in .NET Framework 4.5.
You're probably targeting the Client Profile: target the full framework in Project Properties. Otherwise:
either you did not add the right using statement using System.Web;
or you did not add the reference to System.Web.dll in the project.
WebUtility
You also have another possibility: Use the WebUtility class.
The WebUtility class is recommended by Microsoft itself and should be used outside of web applications.
Like the HttpUtility class it also provides you with the possibility to encode and decode URLs.
This way you don't have the problems with importing the library into your project or setting some specific profiles.
From the Documentation (Source)
The HttpUtility class is used internally by the HttpServerUtility class, whose methods and properties are exposed through the intrinsic ASP.NET Server object. Additionally, the HttpUtility class contains encoding and decoding utility methods that are not accessible from the Server.
To encode or decode values outside of a web application, use the WebUtility class.
The HttpUtility class exists from .NET 1.1, so I think it is not possible for regular projects to 'not see it', as long as you have included a reference to System.Web.
You might be using a PCL (Portable Class Library), which uses a stripped down version of the framework that is supported on the platforms you selected, like Windows Store apps, Windows Phone, Silverlight, etc.
I hope this link will help you.
http://msdn.microsoft.com/en-us/library/system.web.httputility(v=vs.110).aspx
Dot net framework 4.5 support HttpUtility as it is under System.Web namespace.
Also adding a System.Web reference, without System.Web.Extensions reference into your project. If it doesn't work remove the existing and add new reference of System.Web into project. Also check which framework it is targeting it should be .NET Framework 4 or 4.5 without Client.
I encountered this issue in .net 4.5.2 (using VS2019). I did check that I was using full framework and I also tried explicitly declaring System.Web in a using statement, though VS complains that the using clause is not needed.
System.Web.Utility appears to have been replaced by System.Net.Webutility

The type or namespace name 'Http' does not exist?

I have this portable class library, its settings it Windows Phone 8, Windows Store and .NET 4.5.
I'm trying to add the HttpClient, but after I add it by NuGet its runtime version is v4.0.30319, and when i try to using System.Net.Http it states:
The type or namespace name 'Http' does not exist in the namespace 'System.Net' (are you missing an assembly reference?)
The following is added as a reference, and was added by NuGet upon install:
System.Net.Http
System.Net.Http.Extensions
System.Net.Http.Primitives
The class I'm trying to use is HttpClient in my portable class library :-/
This MSDN blog post makes it clear that you need to use this special release of HttpClient.
To use the HttpClient package, right click on your solution, go to the
Manage Nuget Packages dialog, search for Id Microsoft.Net.Http, and
make sure “Include Prerelease” is turned on.
This was released last February, but I haven't seen any updates to indicate that it is now obsolete.
EDIT
Here's the page for the latest version of this package

C# using System.Linq error

Why might "using System.Linq" cause the following error?
The type or namespace name 'Linq' does
not exist in the namespace 'System'
Reference System.Core
And then there are others that merge this namespace too - but that's the primary one on .Net 3.5 and above.
If you're project is currently .Net 2.0, say, and you're using the right version of VS (2005 and above) - you can simply right-click on the proejct properties; and change the 'Target Framework Version' to 3.5. System.Core will then become available.
If you don't see that in the options - then I guess you're using an older VS
The most probable reason is that you are using wrong version of .NET Framework.
Try to add System.Core assembly to your project
You'll get this error if you don't have "System.Core.dll" referenced (the assembly which contains the core LINQ APIs).
System.Linq is available in .Net 3.5 and above version.
Maybe you're targeting an older framework, Linq came in with 3.5 IIRC.
You are using lower version of .NET Framework than 3.5 to compile the source code or you don't have added the System.Core assembly to your project.
Manually type using System.Linq in the starting of the project, you will not be able to find this namespace in add reference dialogue box.
If you are still getting error then try to Add Reference System.Core.
If you are getting an error that it has been already referred then you can unload your project and then edit your csproject file, manually copy reference to System tag and paste and change the name to System.Core and reload the project.
In my case the only thing that worked was:
Adding a new Razor item (e.g. MVC 5 View Page)
That automatically pulls in some NuGet packages
The package that makes System.Linq available to Razor Views IntelliSense seems to be Microsoft.AspNet.WebPages.

Categories

Resources