C
C#16mo ago
dlaw_code

❔ Startup File to Program file

services.AddTransient<ISqlDataAccess, SqlDataAccess>();
services.AddTransient<IContactData>, ContactData>();
services.AddTransient<ISqlDataAccess, SqlDataAccess>();
services.AddTransient<IContactData>, ContactData>();
I watched a tutorial video where the code above was called in the Startup.vsmean while I only have a Program.cs . Is there a way I can call this in Program.cs
68 Replies
JakenVeina
JakenVeina16mo ago
short answer: yes it's code you can call whatever code you want in code that you write it's called writing code long answer: that's presumably a snippet of setup code for the Microsoft.Extensions.DependencyInjection library where services is an IServiceCollection if you're building something like ASP.NET or something based on Microsoft.Extensions.Hosting or maybe a MAUI app the app framework already has an IoC/DI container, you just need to configure it as an example
chef drone builder
builder.Services.AddTransient etc
JakenVeina
JakenVeina16mo ago
using var host = Host.CreateDefaultBuilder()
.ConfigureServices(services => services.Whatever())
.Build();

host.Run();
using var host = Host.CreateDefaultBuilder()
.ConfigureServices(services => services.Whatever())
.Build();

host.Run();
this would essentially be your Main() method if you're not using something like this, you can still use the MS.E.DI library, you just have to set it up yourself
using var rootServiceProvider = new ServiceCollection()
.AddWhatever()
.BuildServiceProvider();

rootServiceProvider.GetRequiredService<Whatever>()
.DoSomething();
using var rootServiceProvider = new ServiceCollection()
.AddWhatever()
.BuildServiceProvider();

rootServiceProvider.GetRequiredService<Whatever>()
.DoSomething();
dlaw_code
dlaw_code16mo ago
This didn't work
chef drone builder
can you show how your program.cs looks? @dlaw_code
dlaw_code
dlaw_code16mo ago
Ok. In a jiffy
dlaw_code
dlaw_code16mo ago
chef drone builder
oh, what doesnt work? you are already doing the same thing with the AddSingleton<>. Just put in this code after the addsingleton call:
builder.Services.AddTransient<ISqlDataAccess, SqlDataAccess>();
builder.Services.AddTransient<IContactData, ContactData>();
builder.Services.AddTransient<ISqlDataAccess, SqlDataAccess>();
builder.Services.AddTransient<IContactData, ContactData>();
dlaw_code
dlaw_code16mo ago
I put it but it's bringing error
chef drone builder
Ok, but this has nothing do to with the difference between startup.cs and program.cs. You are just missing some import statements and/or nuget package references. what tutorial are you following?
dlaw_code
dlaw_code16mo ago
Tim Corey What nuget package reference do you think am missing
chef drone builder
that's a youtube channel, what tutorial are you following? not shure tbh, i cant find anything that has and IContactData Interface. is this an interface that you have created?
dlaw_code
dlaw_code16mo ago
Yeah I thought maybe it's cos he's using an older version of .NET Cos am working with .Net 6
chef drone builder
if you have defined the interface and class, then this shouldn't make a difference you just need do add a using statement
dlaw_code
dlaw_code16mo ago
I thought so too Now i have no idea of what using statement i can use
chef drone builder
in what namespace is the interface defined?
Pobiega
Pobiega16mo ago
Those seem to be self-created types, not library provided
chef drone builder
yes, thats why i am trying to find out in what namespace the types are
Pobiega
Pobiega16mo ago
Ctrl+T, type "IContactData" select first hit. check namespace.
dlaw_code
dlaw_code16mo ago
Pobiega
Pobiega16mo ago
Pobiega
Pobiega16mo ago
so in your program.cs using DataAccessLibrary; you need to have a reference to that project, if they are different projects (which I assume they are)
dlaw_code
dlaw_code16mo ago
This is what it brought
Pobiega
Pobiega16mo ago
The entire purpose of that was to find IContactData and check the namespace something you already did 8 minutes ago :p
dlaw_code
dlaw_code16mo ago
Lol. Am sorry. Didn't get that ???
Pobiega
Pobiega16mo ago
???
dlaw_code
dlaw_code16mo ago
What should i do next?
Pobiega
Pobiega16mo ago
Did you add the using statement to program.cs?
dlaw_code
dlaw_code16mo ago
U mean the
using DataAccessLibrary?
using DataAccessLibrary?
Pobiega
Pobiega16mo ago
yes.
dlaw_code
dlaw_code16mo ago
Yeah i did but it didn't work
Pobiega
Pobiega16mo ago
"didn't work" doesnt help me at all what exactly do you mean by that Im sure VS is giving you some errors
dlaw_code
dlaw_code16mo ago
I mean it brought error
Pobiega
Pobiega16mo ago
So show the errors. I cant see your screen
dlaw_code
dlaw_code16mo ago
Pobiega
Pobiega16mo ago
hm, this seems odd your Program class isnt in a namespace thats.... very unusual also, I think you are missing a project reference
dlaw_code
dlaw_code16mo ago
Pobiega
Pobiega16mo ago
so add the missing ; then we add the project reference
dlaw_code
dlaw_code16mo ago
I added it. Stil the same thing How do i add the project reference
Pobiega
Pobiega16mo ago
find your project in the solution explorer expand "dependencies" and right click it then do "add project reference"
dlaw_code
dlaw_code16mo ago
Ok. In a jiffy
dlaw_code
dlaw_code16mo ago
This is what brought. Is it the one?
Pobiega
Pobiega16mo ago
yep check that box. then click ok Your program is split in multiple projects, and they are not by default aware of eachother
dlaw_code
dlaw_code16mo ago
Ok I have done that. What's next?
Pobiega
Pobiega16mo ago
your problem should now be solved.
dlaw_code
dlaw_code16mo ago
still showing
Pobiega
Pobiega16mo ago
open your .csproj for the blazor project show content
dlaw_code
dlaw_code16mo ago
How do i locate that
Pobiega
Pobiega16mo ago
its your project file
dlaw_code
dlaw_code16mo ago
Ok
dlaw_code
dlaw_code16mo ago
u mean this?
Pobiega
Pobiega16mo ago
yes, doubleclick that should open it as text
dlaw_code
dlaw_code16mo ago
U mean this?
Pobiega
Pobiega16mo ago
your project reference wasnt saved did you press cancel :p
dlaw_code
dlaw_code16mo ago
Nah
Pobiega
Pobiega16mo ago
If you say so but the fact is that it wasnt saved. ¯\_(ツ)_/¯
dlaw_code
dlaw_code16mo ago
How do i save it?
Pobiega
Pobiega16mo ago
by clicking the "OK" button after checking the box
dlaw_code
dlaw_code16mo ago
I have done that already
Pobiega
Pobiega16mo ago
If you say so. but from what I can see, the project reference was not saved
dlaw_code
dlaw_code16mo ago
Am confused bro with the project
Pobiega
Pobiega16mo ago
I can see that. I think you've been moving a bit too fast in your ambition and should repeat the basics a bit more
dlaw_code
dlaw_code16mo ago
Okies Bro. Thanks. I would just take my time to go through the project again and see where the error is coming from. Thanks for the help anyways
Pobiega
Pobiega16mo ago
best of luck.
dlaw_code
dlaw_code16mo ago
Bro. I figured it out Already
Pobiega
Pobiega16mo ago
Great. What was it?
dlaw_code
dlaw_code16mo ago
I moved the Sqldata and the Contactdata class into the Data folder But i encountered another error someplace again🙁
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts