1

In my solution I have a project named Something.IdentityServer. In it, I need to use a nuget package named IdentityServer.External.TokenExchange.

Now, when I need to use anything from that package, I need to make references to it, however, the using directive thinks I want to use my Something.IdentityServer instead of the IdentityServer from the nuget package.

What can I do to solve that problem?

mgPePe
  • 5,677
  • 12
  • 52
  • 85
  • 3
    Maybe use the [`global::` prefix](https://stackoverflow.com/q/15022441/107625)? – Uwe Keim Mar 24 '20 at 15:23
  • 4
    You can alias a namespace using the following format `using SomeAlias = SomeNamespace.SomeSubnamespace` then use the alias instead to reference the external package. It depends which one is referenced more often - I usually alias the one that's external since you will be more often referring to your internal classes than someone else's libs, but your mileage may vary. (Or you can fully qualify your type names) – Charleh Mar 24 '20 at 15:23
  • @UweKeim `using global::IdentityServer.External.TokenExchange;` did remove the confusion from my project name, but somehow it doesn't connect with the method I am using, I will investigate further – mgPePe Mar 24 '20 at 15:28
  • @Charleh that doesn't seem to work as the alias also things it has got to use the project name... – mgPePe Mar 24 '20 at 15:29
  • 1
    @UweKeim this seems to work and solved my issue. There was another unrelated issue with this specific nuget. – mgPePe Mar 24 '20 at 16:16
  • @Uwe Keim; Peter Duniho; It's not a good idea to flag the question as a duplicate when has an active conversation cuz you don't like it. Alias Directive is completely different and recommended approach then global directive. So please open the post. – XAMT Mar 24 '20 at 17:04
  • @XAMT: look again. I'm not the one who closed it as a duplicate. Take your beef up with someone else. (That said, if the duplicate solves the problem, it doesn't matter what _you_ think is the "best" way to approach the problem, the question is still a duplicate.) – Peter Duniho Mar 24 '20 at 17:23

1 Answers1

3

Create an alias for a namespace by using alias directive :

using MyIdentityServer = Something.IdentityServer;

Then use the Fully qualified name (Explicit Directive) :

//Calling Some Methods:
MyIdentityServer.MyMethod()

More info: Using Directive

Here is an example :

using MyJson = Newtonsoft.Json;
using TheirJson = System.Runtime.Serialization.Json;

namespace ConsoleApp9
{
    class Program
    {
        static void Main(string[] args)
        {
            // using MyJson namespace to create Variable_Using_MyJson
            var Variable_Using_MyJson = MyJson.JsonConvert.SerializeObject("Some String");

            // using TheirJson namespace to create Variable_Using_TheirJson
            var Variable_Using_TheirJson = new TheirJson.DataContractJsonSerializer(typeof(string));
        }
    }
}
XAMT
  • 1,515
  • 2
  • 11
  • 31
  • This helps you use your own class. But you can't access the nuget library that I am trying to access. – mgPePe Mar 24 '20 at 16:17
  • When u want to import nuget's namespace to your code on the top of the page use it. (e.g. using MyJson = System.Text.Json) – XAMT Mar 24 '20 at 16:25
  • 1
    I didn't understand what you are saying. Can you please explain? – mgPePe Mar 24 '20 at 16:27