C
C#2y 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
JakenVeina2y 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
anita
anita2y ago
builder.Services.AddTransient etc
JakenVeina
JakenVeina2y 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_codeOP2y ago
This didn't work
anita
anita2y ago
can you show how your program.cs looks? @dlaw_code
dlaw_code
dlaw_codeOP2y ago
Ok. In a jiffy
dlaw_code
dlaw_codeOP2y ago
anita
anita2y ago
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_codeOP2y ago
I put it but it's bringing error
anita
anita2y ago
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_codeOP2y ago
Tim Corey What nuget package reference do you think am missing
anita
anita2y ago
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_codeOP2y ago
Yeah I thought maybe it's cos he's using an older version of .NET Cos am working with .Net 6
anita
anita2y ago
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_codeOP2y ago
I thought so too Now i have no idea of what using statement i can use
anita
anita2y ago
in what namespace is the interface defined?
Pobiega
Pobiega2y ago
Those seem to be self-created types, not library provided
anita
anita2y ago
yes, thats why i am trying to find out in what namespace the types are
Pobiega
Pobiega2y ago
Ctrl+T, type "IContactData" select first hit. check namespace.
dlaw_code
dlaw_codeOP2y ago
Pobiega
Pobiega2y ago
Pobiega
Pobiega2y 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_codeOP2y ago
This is what it brought
Pobiega
Pobiega2y 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_codeOP2y ago
Lol. Am sorry. Didn't get that ???
Pobiega
Pobiega2y ago
???
dlaw_code
dlaw_codeOP2y ago
What should i do next?
Pobiega
Pobiega2y ago
Did you add the using statement to program.cs?
dlaw_code
dlaw_codeOP2y ago
U mean the
using DataAccessLibrary?
using DataAccessLibrary?
Pobiega
Pobiega2y ago
yes.
dlaw_code
dlaw_codeOP2y ago
Yeah i did but it didn't work
Pobiega
Pobiega2y 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_codeOP2y ago
I mean it brought error
Pobiega
Pobiega2y ago
So show the errors. I cant see your screen
dlaw_code
dlaw_codeOP2y ago
Pobiega
Pobiega2y 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_codeOP2y ago
Pobiega
Pobiega2y ago
so add the missing ; then we add the project reference
dlaw_code
dlaw_codeOP2y ago
I added it. Stil the same thing How do i add the project reference
Pobiega
Pobiega2y ago
find your project in the solution explorer expand "dependencies" and right click it then do "add project reference"
dlaw_code
dlaw_codeOP2y ago
Ok. In a jiffy
dlaw_code
dlaw_codeOP2y ago
This is what brought. Is it the one?
Pobiega
Pobiega2y 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_codeOP2y ago
Ok I have done that. What's next?
Pobiega
Pobiega2y ago
your problem should now be solved.
dlaw_code
dlaw_codeOP2y ago
still showing
Pobiega
Pobiega2y ago
open your .csproj for the blazor project show content
dlaw_code
dlaw_codeOP2y ago
How do i locate that
Pobiega
Pobiega2y ago
its your project file
dlaw_code
dlaw_codeOP2y ago
Ok
dlaw_code
dlaw_codeOP2y ago
u mean this?
Pobiega
Pobiega2y ago
yes, doubleclick that should open it as text
dlaw_code
dlaw_codeOP2y ago
U mean this?
Pobiega
Pobiega2y ago
your project reference wasnt saved did you press cancel :p
dlaw_code
dlaw_codeOP2y ago
Nah
Pobiega
Pobiega2y ago
If you say so but the fact is that it wasnt saved. ¯\_(ツ)_/¯
dlaw_code
dlaw_codeOP2y ago
How do i save it?
Pobiega
Pobiega2y ago
by clicking the "OK" button after checking the box
dlaw_code
dlaw_codeOP2y ago
I have done that already
Pobiega
Pobiega2y ago
If you say so. but from what I can see, the project reference was not saved
dlaw_code
dlaw_codeOP2y ago
Am confused bro with the project
Pobiega
Pobiega2y 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_codeOP2y 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
Pobiega2y ago
best of luck.
dlaw_code
dlaw_codeOP2y ago
Bro. I figured it out Already
Pobiega
Pobiega2y ago
Great. What was it?
dlaw_code
dlaw_codeOP2y ago
I moved the Sqldata and the Contactdata class into the Data folder But i encountered another error someplace again🙁
Accord
Accord2y 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