Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue in GetOriginatingAssembly function inside HttpRequestHeadersExtensions #264

Closed
johnhydemtm365 opened this issue Feb 5, 2021 · 11 comments
Assignees
Labels

Comments

@johnhydemtm365
Copy link
Contributor

johnhydemtm365 commented Feb 5, 2021

Brief bug description

Using Kentico Kontent Delivery version 14.2.1

When running Dancing goat boilerplate sample and our own Kontent asp.net core implementation and turning on Redis caching we seem to get an error, when trying to set the source tacking header:

InvalidOperationException: Sequence contains no elements
System.Linq.ThrowHelper.ThrowNoElementsException()
System.Linq.Enumerable.Last(IEnumerable source)
Kentico.Kontent.Delivery.Extensions.HttpRequestHeadersExtensions.GetOriginatingAssembly()
Kentico.Kontent.Delivery.Extensions.HttpRequestHeadersExtensions.GetSource()
System.Lazy.ViaFactory(LazyThreadSafetyMode mode)
System.Lazy.ExecutionAndPublication(LazyHelper executionAndPublication, bool useDefaultConstructor)
System.Lazy.CreateValue()
System.Lazy.get_Value()
Kentico.Kontent.Delivery.Extensions.HttpRequestHeadersExtensions.AddSourceTrackingHeader(HttpRequestHeaders headers)
Kentico.Kontent.Delivery.DeliveryClient.SendHttpMessageAsync(string endpointUrl, string continuationToken)
Kentico.Kontent.Delivery.DeliveryClient+<>c__DisplayClass18_0.b__0()
Kentico.Kontent.Delivery.RetryPolicy.DefaultRetryPolicy.ExecuteAsync(Func<Task> sendRequest)
Kentico.Kontent.Delivery.DeliveryClient.GetDeliveryResponseAsync(string endpointUrl, string continuationToken)
Kentico.Kontent.Delivery.DeliveryClient.GetItemsAsync(IEnumerable parameters)
Kentico.Kontent.Delivery.Caching.DistributedCacheManager.GetOrAddAsync(string key, Func<Task> valueFactory, Func<T, bool> shouldCache, Func<T, IEnumerable> dependenciesFactory)
Kentico.Kontent.Delivery.Caching.DeliveryClientCache.GetItemsAsync(IEnumerable parameters)

What went wrong?
Looking in the code in the project it seems to be failing on line 133 in the HttpRequestHeadersExtensions class. For some reason, it's not finding the assembly, and then because of that throwing an error.

Weirdly if I just use memory caching it all works fine...

Repro steps

Download Dancing goat boilerplate, update NuGet packages to the latest version.
Configure Redis caching for distributed caching
Run the website

Suggested change...
Replace
https://github.com/Kentico/kontent-delivery-sdk-net/blob/2e1fe1380814072ec02b3dfea30df7c16af2ef91/Kentico.Kontent.Delivery/Extensions/HttpRequestHeadersExtensions.cs#L133

With:
var originatingAssembly = callerAssemblies.Any() ? callerAssemblies.Last() : null;

or

System.Reflection.Assembly.GetEntryAssembly().GetName()

Obviously, this might be being caused by a simple resolution as if I lift the code and dump it into a project that my website references it all works fine, but when the code is in the same assembly as executing assembly then it does not work as that would mean the executing assembly is referencing its self.

@petrsvihlik petrsvihlik self-assigned this Feb 5, 2021
@petrsvihlik
Copy link
Contributor

Hi @johnhydemtm365 ,
do you use the DeliverySourceTrackingHeaderAttribute in the code of your project? Or the AssemblyAttribute in your csproj? ...as described at https://github.com/Kentico/kontent-delivery-sdk-net/wiki/Developing-plugins
Otherwise, the source tracking logic should be skipped.

BTW: This functionality is meant for reusable plugins/extensions built on top of this SDK, I'd like to better understand your scenario. Are you building something like that?

@johnhydemtm365
Copy link
Contributor Author

johnhydemtm365 commented Feb 5, 2021 via email

@petrsvihlik
Copy link
Contributor

Thanks, I get you. We'll investigate what's going on.

@petrsvihlik
Copy link
Contributor

I was able to reproduce it...investigating

@johnhydemtm365
Copy link
Contributor Author

johnhydemtm365 commented Feb 5, 2021 via email

@petrsvihlik
Copy link
Contributor

the origin of the error is rather mysterious to me and it requires further investigation. HOWEVER, this functionality is not critical in any way and it should never end up being blocking so a set of null checks is in order.

@petrsvihlik
Copy link
Contributor

Let's wait for the build: https://github.com/Kentico/kontent-delivery-sdk-net/releases/tag/15.0.0-alpha3

There are a few breaking changes you should know about. The named client registration has been extracted to a separate package Kentico.Kontent.Delivery.Extensions.DependencyInjection.

If you use it, make sure you reference Autofac <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="7.1.0" />

and add .UseServiceProviderFactory(new AutofacServiceProviderFactory()) to the Program.cs like this:

      public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseServiceProviderFactory(new AutofacServiceProviderFactory())
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });

@petrsvihlik
Copy link
Contributor

here's the motivation for the breaking changes: #240

we decided that handling of named services is out of the scope of our responsibilities.

@petrsvihlik
Copy link
Contributor

Please drop a comment here stating if it works for you now.

@johnhydemtm365
Copy link
Contributor Author

johnhydemtm365 commented Feb 5, 2021 via email

@petrsvihlik petrsvihlik added this to the v15 milestone Feb 7, 2021
@petrsvihlik petrsvihlik added the bug label Feb 7, 2021
@petrsvihlik
Copy link
Contributor

hi, thanks for submitting the other issue! let's continue the discussion about it in #265

Regarding v15, I expect approximately the following timeline: https://github.com/Kentico/kontent-delivery-sdk-net/milestones?direction=asc&sort=due_date&state=open

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants