aspnet / Hosting

[Archived] Code for hosting and starting up an ASP.NET Core application. Project moved to https://github.com/aspnet/Extensions and https://github.com/aspnet/AspNetCore
Apache License 2.0
553 stars 312 forks source link

Not able to use Web Api 2 as Self Console Host in C# #644

Closed Cruik closed 8 years ago

Cruik commented 8 years ago

Hey guys,

i m not able to set up a Web Api 2 service as a self hosted console applicaton. My problem is more precisely in this Stack Overflow Post:

http://stackoverflow.com/questions/35943690/using-web-api-2-as-self-console-host-in-c-sharp

Hope you can help me :+1:

davidfowl commented 8 years ago

Are you using RC1? These new APIs are only in the upcoming RC2 build. If you're looking for RC1 bits I'd suggest:

https://github.com/aspnet/Hosting/blob/1.0.0-rc1/test/Microsoft.AspNet.Hosting.Tests/WebHostBuilderTests.cs#L115

or making a new empty web project and looking at the Main method

muratg commented 8 years ago

@Itempas You mean ASP.NET 4 Web API 2? Because this repo tracks ASP.NET Core 1 work. And Web API has been merged with MVC in MVC Core 1.

Cruik commented 8 years ago

@muratg i m using RC2 and also asp.net core 1. i can make it compile now but till i am very new to web api thing i need some more experience and time.

i get now the: The given assembly name or codebase was invalid. with the following code:

var host = new WebHostBuilder()
                .UseDefaultConfiguration(args)
                .UseServer("http://localhost:5004")
                .UseStartup<Program>()
                .Build();
Tratcher commented 8 years ago

What's the full exception and stack trace?

Cruik commented 8 years ago

Ah sry my bad, forget to take the complete stack trace :)

at System.Reflection.AssemblyName.nInit(RuntimeAssembly& assembly, Boolean forIntrospection, Boolean raiseResolveEvent) at System.Reflection.AssemblyName..ctor(String assemblyName) at Microsoft.AspNetCore.Hosting.Server.ServerLoader.LoadServerFactory(String assemblyName) at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureServer() at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication() at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build() at ConsoleHost.Program.Main(String[] args) in C:\Users\e1073\Documents\Visual Studio 2015\Projects\TraverseFileSystem\ConsoleHost\Program.cs:line 13

davidfowl commented 8 years ago

@Itempas can you make a repository on github with the repro and instructions on how you ran it?

Cruik commented 8 years ago

yeah sure :)

https://github.com/Itempas/WebApi2Test

I hope i did not miss anything :$

davidfowl commented 8 years ago

How did you end up with this solution? There are a few problems:

WebApi2

ConsoleHost

Which project is giving you the error? Also, is it a runtime error now? The original post had a compile time error.

Tratcher commented 8 years ago

Note your ConsoleHost .UseStartup<Program>() won't work because Program does not have a Configure method to setup the app. It should be referring to WebApi2's Startup class instead.

muratg commented 8 years ago

@Itempas Let us know if fixing the references + using Startup instead of Program did not fix your issue.

Cruik commented 8 years ago

Sry guys, i was on vacation for a few days.

I updated my code now and get the following stack strace:

at System.Reflection.AssemblyName.nInit(RuntimeAssembly& assembly, Boolean forIntrospection, Boolean raiseResolveEvent) at System.Reflection.AssemblyName..ctor(String assemblyName) at Microsoft.AspNet.Hosting.Server.ServerLoader.LoadServerFactory(String assemblyName) at Microsoft.AspNet.Hosting.Internal.HostingEngine.EnsureServer() at Microsoft.AspNet.Hosting.Internal.HostingEngine.BuildApplication() at Microsoft.AspNet.Hosting.Internal.HostingEngine.Start() at ConsoleHost.Program.Main(String[] args) in C:\Users\e1073\Source\Repos\WebApi2Test\ConsoleHost\Program.cs:line 18

Source is mscorlib

The exception is thrown at host.Start():

The code is on : https://github.com/Itempas/UpdatedWebApi2Test/tree/Test

Am i missing a thing ?

Tratcher commented 8 years ago
            var host = CreateWebHostBuilder()
                .UseServer("Microsoft.AspNet.Server.Kestrel")
                .UseUrls("http://localhost:5004")
                .UseStartup<Program>()
                .Build();

Also add "Microsoft.AspNet.Server.Kestrel" to your project dependencies.

Cruik commented 8 years ago

Hey @Tratcher , thanks for your reply!

i tried that, but i get an error with .UseUrls()

'WebHostBuilder' does not contain a definition for 'UseUrls' and no extension method 'UseUrls' accepting a first argument of type 'WebHostBuilder' could be found (are you missing a using directive or an assembly reference?) ConsoleHost.DNX Core 5.0

davidfowl commented 8 years ago

@Itempas instead of this back and forth can you make a project that reproduces the issue? We cant tell what versions you;re using so it would be better if you could provide a project on github with the project.json and project.lock.json so we can take a look.

Cruik commented 8 years ago

@davidfowl yeah , sure.

i just added the code from Tratcher to my Program.cs + the reference in project.json

https://github.com/Itempas/UpdatedWebApi2Test/tree/Test

davidfowl commented 8 years ago

So you're using rc1

Cruik commented 8 years ago

yep, since i couldn´t found a rc2-final

davidfowl commented 8 years ago

rc2 is far from final.

Cruik commented 8 years ago

Thats the reason why i decided to stick with rc1 :D sry if it´s annoying, i just began with Web API and i want to understand it :x

Cruik commented 8 years ago

Hey guys, i got it!!

The server is running but now he is refusing my connections when i try to call the controller actions via browser :/

The code is here : https://github.com/Itempas/UpdatedWebApi2Test/tree/Test

Tratcher commented 8 years ago

Locally or remotely?

Cruik commented 8 years ago

I m testing it locally on my computer

Cruik commented 8 years ago

or shall i open a new issue for that ?

davidfowl commented 8 years ago

I'm not sure there's any issue yet. I think you just have a mix of package versions and it's causing problems.

Cruik commented 8 years ago

The important packages are: "Microsoft.AspNet.Hosting": "1.0.0-rc1-final", "Microsoft.AspNet.Mvc.Core": "6.0.0-rc1-final", "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final" Am i right?

davidfowl commented 8 years ago

Sure, it looks like all Rc1 bits now.

Cruik commented 8 years ago

that would mean that the packages are right ?

Tratcher commented 8 years ago

Note you're getting a 500 response from the controller. When I added app.UseDeveloperExceptionPage(); from Microsoft.AspNet.Diagnostics I see this error:

InvalidOperationException: No service for type 'WebApi2.Repository.IContactsRepository' has been registered.

Adding the following fixed it:

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvcCore();
            services.AddSingleton<IContactsRepository, ContactsRepository>();
        }