SendingRequest event and DataServicePackageRepository

Jul 3, 2012 at 3:43 PM

Hi,

Using MyGet.org I have a private feed. I use this code to read packages from it:

var uri = new Uri("http://www.myget.org/F/09f70f7dc86c4e2685e752988c67e5ad/");

var rep = new DataServicePackageRepository(uri);

rep.SendingRequest += (sender, eventArgs) =>

{

eventArgs.Request.Credentials = new NetworkCredential("stovellp", "<my password>");

};

// Works

var packages = rep.GetPackages().ToList();

However, if I try to call FindPackagesById or Search instead of GetPackages, the SendingRequest event is never raised, and so I get a 401 error.

var rep = new DataServicePackageRepository(uri);

rep.SendingRequest += (sender, eventArgs) =>

{

// This handler is never called :(

eventArgs.Request.Credentials = new NetworkCredential("stovellp", "<my password>");

};

var packages = rep.FindPackagesById("jQ").ToList();

Is there a reason why DataServicePackageRepository doesn’t raise a SendingRequest event? Is there an alternative way I should be passing credentials?

I’m happy to send a pull request with a fix, but I just wanted to discuss it first.

Paul

http://octopusdeploy.com

Developer
Jul 3, 2012 at 4:16 PM

You should be able to specify a credential for your feed via the exe - http://docs.nuget.org/docs/reference/command-line-reference#Sources_Command. Does that not work for you?

Jul 3, 2012 at 4:30 PM

I’m trying to avoid using the exe or relying on the config file.

However I did try this:

static void Main(string[] args)

{

var source = new PackageSource("http://www.myget.org/F/09f70f7dc86c4e2685e752988c67e5ad/", "MyGet");

source.UserName = "stovellp";

source.Password = "<mypassword>";

source.IsEnabled = true;

var provider = new PackageSourceProvider(new InMemorySettings());

HttpClient.DefaultCredentialProvider = new SettingsCredentialProvider(NullCredentialProvider.Instance, provider);

provider.SavePackageSources(new[] { source });

var rep = provider.GetAggregate(new PackageRepositoryFactory());

// Works

var packages1 = rep.GetPackages().ToList();

// 401's

var packages2 = rep.FindPackagesById("jQ").ToList();

}

But again, FindPackagesById doesn’t seem to consult the credential provider, where GetPackages does.

(InMemorySettings is just an implementation of Settings that uses an in-memory XDocument instead of persisting to disk)

Paul

From: pranavkm [email removed]
Sent: 03 July 2012 17:16
To: Paul Stovell
Subject: Re: SendingRequest event and DataServicePackageRepository [nuget:361837]

From: pranavkm

You should be able to specify a credential for your feed via the exe - http://docs.nuget.org/docs/reference/command-line-reference#Sources_Command. Does that not work for you?

Jul 3, 2012 at 4:41 PM

Just to rule it out, I used NuGet.exe to set the credentials in my NuGet.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <activePackageSource>
    <add key="NuGet official package source" value="https://nuget.org/api/v2/" />
  </activePackageSource>
  <packageSources>
    <add key="MyGet" value="http://www.myget.org/F/09f70f7dc86c4e2685e752988c67e5ad/" />
  </packageSources>
  <disabledPackageSources />
  <packageSourceCredentials>
    <MyGet>
      <add key="Username" value="stovellp" />
      <add key="Password" value="AQAAA...my password, etc. etc....s=" />
    </MyGet>
  </packageSourceCredentials>
</configuration>

Then I used:

        static void Main(string[] args)
        {
            var provider = new PackageSourceProvider(new Settings(new PhysicalFileSystem(@"C:\Users\Paul\AppData\Roaming\NuGet")));
            HttpClient.DefaultCredentialProvider = new SettingsCredentialProvider(NullCredentialProvider.Instance, provider);

            var rep = provider.GetAggregate(PackageRepositoryFactory.Default);

            // Works
            var packages1 = rep.GetPackages().ToList();

            // 401's
            var packages2 = rep.FindPackagesById("jQ").ToList();
        }

And same 401 error. 

Developer
Jul 6, 2012 at 6:21 PM

SendingRequest does seem to be firing just fine for me:

static void Main(string[] args)
{
    var repo = new DataServicePackageRepository(new Uri("http://nuget.org/api/v2/"));
    repo.SendingRequest += (sender, eventArgs) => Console.WriteLine("Sending");

    Console.WriteLine(repo.FindPackagesById("NuGet.Core").First());
    Console.WriteLine(repo.Search("NuGet.Core", allowPrereleaseVersions: false).First());
}

 

// Sending
// Nuget.Core 1.0.1120.104
// Sending
// Nuget.Core 1.0.1120.104

Jul 8, 2012 at 1:29 PM

That works for me too.

What doesn’t work, is when I use the exact same code, but pass a URI that requires authentication. I get a 401 before the SendingRequest event is raised.

My guess is that there’s a HTTP request that happens at the start which doesn’t go through SendingRequest, even if the following requests do.

Paul

From: pranavkm [email removed]
Sent: 06 July 2012 19:21
To: Paul Stovell
Subject: Re: SendingRequest event and DataServicePackageRepository [nuget:361837]

From: pranavkm

SendingRequest does seem to be firing just fine for me:

static void Main(string[] args)
{
    var repo = new DataServicePackageRepository(new Uri("http://nuget.org/api/v2/"));
    repo.SendingRequest += (sender, eventArgs) => Console.WriteLine("Sending");
 
    Console.WriteLine(repo.FindPackagesById("NuGet.Core").First());
    Console.WriteLine(repo.Search("NuGet.Core", allowPrereleaseVersions: false).First());
}

// Sending
// Nuget.Core 1.0.1120.104
// Sending
// Nuget.Core 1.0.1120.104
 
Developer
Jul 9, 2012 at 3:06 AM

It might be our RedirectedHttpClient type trying to resolve the url. Could you try using the alternate constructor for DataServicePackageRepository - the one that accepts an IHttpClient and pass in an HttpClient instance?