c# - Setting Accept Header without using MediaTypeWithQualityHeaderValue -


in asp.net web api 2 difference between setting httpclient accept header using following traditional method :

        httpclient client = httpclientfactory.create(handler);          client.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("application/json")); 

and following method :

var headers = new dictionary<string, string>             {                 {"accept", "application/json"}};  headers.foreach(h => client.defaultrequestheaders.add(h.key, h.value)); 

update 1:

based on answer @darrenmiller in following post what overhead of creating new httpclient per call in webapi client? appears preferred method using defaultrequestheaders property because contains properties intended multiple calls. mean if set default header using simple dictionary httpclient client not efficient 1 uses defaultrequestheaders? in addition cant understand how values inside defaultrequestheaders reused? lets create 20 httpclient client using httpclientfactory.create , inside every single 1 of them set defaultrequestheaders property [do need because defaultrequestheaders meant reused?!]. reuse kick-in , setting defaultrequestheaders every time create httpclient client result in kind of performance hit?

part 1 of question: there difference adding headers?

httpclient client = httpclientfactory.create(handler); 

method 1:

client.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("application/json")); 

method 2:

var headers = new dictionary<string, string>{{"accept", "application/json"}}; headers.foreach(h => client.defaultrequestheaders.add(h.key, h.value)); 

method 1 gives nice typed values ability add multiple accept types. method 2 has 1 more "magic string" place typos , there no way add multiple accept types.

part 2 of question: performance , reuse value?

the performance hit of using new httpclient every request depends on use case. bench mark , measure see if matters. performance on developer gains be. consider every httpclient use have remember bunch of headers add. if forget add proper header, errors happen. so, can use defaultrequestheaders set these in factory.

public class apiservice {     public static httpclient getclient()     {         var client = new httpclient(new uri("https://someservice/"));         client.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("application/json"));          //add other setup items here.         return client;     } } 

now use it:

public async task dostuff() {     using(var client = apiservice.getclient())     {         //client have proper base uri , headers set.         var data = await client.getasync<dynamic>("sales");          //client still have proper base uri , headers set.         var data2 = await client.getasync<dynamic>("products");     } } 

httpclients should short lived , wrapped in using statement. reuse occurs when multiple requests made using same client.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -