<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mario Diaz</title>
    <description>The latest articles on DEV Community by Mario Diaz (@devmariodiaz).</description>
    <link>https://dev.to/devmariodiaz</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F435387%2Fa9e7513e-c4bc-4d41-85bc-7d530153bb29.jpg</url>
      <title>DEV Community: Mario Diaz</title>
      <link>https://dev.to/devmariodiaz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/devmariodiaz"/>
    <language>en</language>
    <item>
      <title>.Net Core and Kafka</title>
      <dc:creator>Mario Diaz</dc:creator>
      <pubDate>Tue, 03 Dec 2024 15:29:43 +0000</pubDate>
      <link>https://dev.to/devmariodiaz/net-core-and-kafka-mcp</link>
      <guid>https://dev.to/devmariodiaz/net-core-and-kafka-mcp</guid>
      <description></description>
      <category>dotnetcore</category>
      <category>kafka</category>
    </item>
    <item>
      <title>How I solved .NET error: A circular dependency was detected for the service of type</title>
      <dc:creator>Mario Diaz</dc:creator>
      <pubDate>Thu, 26 Sep 2024 14:02:21 +0000</pubDate>
      <link>https://dev.to/devmariodiaz/how-i-solved-net-error-a-circular-dependency-was-detected-for-the-service-of-type-17mf</link>
      <guid>https://dev.to/devmariodiaz/how-i-solved-net-error-a-circular-dependency-was-detected-for-the-service-of-type-17mf</guid>
      <description>&lt;p&gt;I was facing an error in an app that I took with some design problems and trying to do some things when we needed to add new features and solve bugs, this error was:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: IMyService Lifetime: Scoped ImplementationType: MyService': A circular dependency was detected for the service of type 'Interfaces.IMyService'.\r\nInterfaces.IMyService(Services.MyService) -&amp;gt; Interfaces.IMyService(Services.MyService) -&amp;gt; Interfaces.IMyServiceService(Services.MyService) -&amp;gt; Interfaces.IMyService(Services.MyService) -&amp;gt; Interfaces.IMyService(Services.MyService) -&amp;gt; Interfaces.IMyService) (Error while validating the service descriptor 'ServiceType: Interfaces.IMyService Lifetime: Scoped ImplementationType: Services.MyService': A circular dependency was detected for the servi..."    string&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The solution I found was this:&lt;br&gt;
Creating a class member:&lt;br&gt;
&lt;code&gt;private readonly IServiceProvider _serviceProvider;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Inject this in the class constructor: &lt;br&gt;
&lt;code&gt;public MyService(&lt;br&gt;
    IServiceProvider serviceProvider,&lt;br&gt;
    // Other injections ...&lt;br&gt;
)&lt;br&gt;
{&lt;br&gt;
    _serviceProvider = serviceProvider;&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In the place we need to use the service:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;var scope = _serviceProvider.CreateScope();&lt;br&gt;
IMyService _myService = scope.ServiceProvider.GetRequiredService&amp;lt;IMyService&amp;gt;();&lt;br&gt;
await _myService .TheMethod()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you have any other solution, please let me know!&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>dependencyinversion</category>
      <category>microsoft</category>
    </item>
  </channel>
</rss>
