Solving issues with referencing packages in .NET Core

Damir Dobric Posts

Next talks:

 

    

Follow me on Twitter: #ddobric



 

 

Archives

 

When migrating projects from .NET to  .NET Core (CoreFX), it is most likely tht you will run into problem like this one:

“error CS0246: The type or namespace name 'WebException' could not be found (are you missing a using directive or an assembly reference?)”

image

Issue shown above is clear. The application is using a type WebException, which is not referenced by your project. This is one of probably most common situation in migration projects. Because .NET Framework (desktop version) is a big package with many assemblies referenced by default, such errors will happen not that often.

When working with .NET core, your application have to explicitly reference required assemblies. By default is almost nothing referenced. The solution for this issue is very simple. Commonly you will add a reference to assembly which implements the type WebException. But with .NET core all looks a bit different.
First, you will not reference assembly explicitly. You can do this, but it does not fit in the strategy of .NET framework anymore.
You should reference the NUGET package, which contains assembly, which implement named type. So far so clear. But how do you know in which NUGET package is your assembly with the type. It looks like it is even more complicated as it was before. Now, we are not only looking for assembly. We are looking for NUGET package, assembly and type.

Fortunately, Microsoft silently tuned http://nuget.org site to help solving such issues.
Let’s solve this issue now step by step.

Open the browser of you choice and navigate to http://nuget.org. Then search for missing type “WebException”.

image

Sometimes, you will get multiple results. You have to chose the right one. I have unfortunately no a general algorithm, for this. Then click on your package and you will see following:

image

As you see, the package page shows exactly which types are implemented in assembly, which is in that package.
Now, copy the name of package and go to VS to add a reference to it.

image

At the end your project referenced packages will look like:

image

As you see, with .NET Core we will have to take more care about details. This might feel like a step backward, but it is not.
By using this approach, we will learn exactly where is which feature implemented. This will give us a chance to build applications with smaller footprint and it will help us to maintain part of applications easier. In other words, we can replace or upgrade on level of the package, which will save lot of time in overall application live cycle.


Posted Nov 05 2015, 10:33 PM by Damir Dobric
Filed under:
developers.de is a .Net Community Blog powered by daenet GmbH.