I have developed self-hosted(owin based) web api using console application.
In development phase I was running the console application and everything was okay.
static void Main(string[] args)
{
string baseUri = "http://+:8080";
Console.WriteLine("Starting web Server...");
var server = WebApp.Start<Startup>(baseUri);
Console.WriteLine("Server running at {0} - press Enter to quit. ", baseUri);
Console.ReadLine();
server.Dispose();
}
Now I need to deploy my self-hosted web api to run on IIS.So, could you please tell me the steps to get my web api up and running on IIS?
In order to understand theory - take a look at this question. If you want your OWIN self-hosted WEB API application to be running on IIS, you need to use Owin.Host.SystemWeb package. You should:
Add a Startup.cs class (entry point for your IIS-hosted app)
Tell OWIN pipeline about your entrypoint: Mark Startup class with
owin attribute OR do it via web.config. (See this article for reference)
P.S. You can always take a look at a standard scaffolded empty Web API project in Visual Studio. It includes IIS web host out-of-the-box
Related
I want to create a gRPC service but I need to host in a winform .net application.
there is an extraordinary example of how Hosting ASP.NET Core API in a Windows Forms Application
I would like someone to explain to me, what I need to install and what I should change in that example, to host a grpc service in the form of Windows ...
You could follow the same steps but with several additional:
Install the package Microsoft.AspNetCore.Grpc.HttpApi This is going to map your gRPC endpoints to the classical HTTP. It is not automatic you need to specify the services in the Startup.cs as follow:
app.UseEndpoints(endpoints =>
{
endpoints.MapGrpcService<MygRPCService>();
});
Into your protos you need to indicate the HTTP path, something like this:
rpc Get(GetRequest) returns (GetReply) {
option (google.api.http) = {
get: '/my-endpoint'
body: '*'
};
}
Add to your Startup.cs ConfigureService method:
services.AddGrpc();
services.AddGrpcHttpApi();
I can in ASP.NET and .NET Core create a selfhosted web service (WCF,REST, based on Kastrel or Katana and so on). But is there a way to create a full working web site, or SPA, may be based MVC ?
For example I can inside .NET Core Startup file add code:
app.Run(async (context) =>
{
await context.Response.WriteAsync($"Hello World! {
//insert your web site here :-)
}");
});
- but I should to create all html, js and so on code by myself. May be we have a better way ?
Of course I know that we have a big parts like ASP and so on - but we can't create selfhosted app, we need a web server - usually IIS !
I guess you mean that your self-hosted web server of visual studio is not accessible from the outside?
Assuming that your port forwarding and firewall are correctly configured. You need to change the bind address of your application:
new WebHostBuilder()
.UseKestrel()
...
.UseUrls("http://*:80")
...;
Note:
This should never be used without reverse proxy.
More info about why this should not be done without reverse proxy: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?view=aspnetcore-2.0&tabs=aspnetcore2x
I am new to WCF development and I am trying to create a WCF service hosted in a console app.
I have already created the WCF service and tested it by running it on IIS Express. Doing so, the WCF service will be accessible from http://localhost:5576/MyFirstService.svc. Within the service, I have defined a GET endpoint /test/<param> just to test if it works. Upon visiting the url with Postman http://localhost:5576/MyFirstService.svc/test/123, it will echo back 123.
My console app that hosts the WCF on the other hand is super simple. I followed the tutorial (http://www.topwcftutorials.net/2014/05/wcf-self-hosting-console-application.html). The relevant code is below:
Uri httpBaseAddress = new Uri("http://localhost:4321/StudentService");
//Instantiate ServiceHost
studentServiceHost = new ServiceHost(typeof(StudentService.StudentService), httpBaseAddress);
//Add Endpoint to Host
studentServiceHost.AddServiceEndpoint(typeof(StudentService.IStudentService), new WSHttpBinding(), "");
//Metadata Exchange
ServiceMetadataBehavior serviceBehavior = new ServiceMetadataBehavior();
serviceBehavior.HttpGetEnabled = true;
studentServiceHost.Description.Behaviors.Add(serviceBehavior);
//Open
studentServiceHost.Open();
Console.WriteLine("Service is live now at : {0}", httpBaseAddress);
Console.ReadKey();
As I launched the console app and visited http://localhost:4321/StudentService, I am greeted with the standard page talking about wsdl. However, if I tried to visit http://localhost:4321/StudentService/test/123, I get a 400 bad request error.
Am I doing things right? What is the path that I should be using to get to my endpoints? I tried many variations of the URL and it just does not seem to work.
Your first service, http://localhost:5576/MyFirstService.svc, was running within the context of IIS (yes even though it's express) so that is what allows for the URL (REST style) routing like you were seeing in your "test/123" example.
But the code example, and posted reference link, is actually a self-hosted service from a console application which doesn't utilize IIS or WAS (Windows Activation Service) so routing won't be available.
Don't get me wrong, your StudentService will still work just fine if called via SOAP, just not from a REST perspective which is what Postman is used for.
There are free tools out there like SoapUI that work just like Postman to test your WCF services.
well, I am now learning aspnet core , I can't understand when does the application start its server(like IIS or KestrelServer),and how the server listen the httprequest and forwards the request to the application. can anybody help me? thanks
Well, let's start from beginning (As I couldn't figure out your knowledge about C#)
Every C# application must contain a single Main method specifying where program execution is to begin, so there it is, by default the templates have a Class Program where you can set the type of WebServer that you'll use, and tell the server to start listening for HTTP requests, something like:
public static void Main(string[] args)
{
var config = new ConfigurationBuilder()
.AddCommandLine(args)
.Build();
var host = new WebHostBuilder()
.UseKestrel()
.UseConfiguration(config)
.UseStartup<Startup>()
.Build();
host.Run();
}
In AspNetCore and even in AspNet (MVC or WebApi) you can (and should) use OWIN (aka Katana or vNext, that are Microsoft's OWIN implementations for AspNet and AspNetCore respectively).
OWIN represents a Interface (just a specification), that tell how WebServers should comunicate with WebApplications. Normally it handle the Http Requests to a pipeline that you can plug Middlewares, like Authentication/Authorization, Log, Error Handlings and so on, and in the end of the pipeline you should plug your Web Application.
In AspNetCore you set your Middleware Pipeline by using UseStartup<MyStartupClass> in your Host configuration see Main method above and simple as that your Pipeline will handle every HttpRequest.
When building a MVC applications in AspNetCore (.UseMvc()) you are setting a middleware that tells your application to look for classes that inherit from Microsoft.AspNetCore.Mvc.Controller to look for RESTful entry points (HTTP GET, POST...)
This is just a simple overview, and you can learn a lot more looking in the documentation of this technologies. Just search for tags like Katana, vNext, OWIN, OWIN Middleware, OWIN Pipeline...
ASP.NET Core application anatomy is discussed here at this asp.net core introduction.
Some important text that answers your question is as follows from the tutorial:
An ASP.NET Core app is simply a console app that creates a web server in its Main method. Main uses WebHostBuilder, which follows the builder pattern, to create a web application host. The builder has methods that define the web server (for example UseKestrel) and the startup class (UseStartup). In the example above, the Kestrel web server is used, but other web servers can be specified. The Startup class is where you define the request handling pipeline and where any services needed by the app are configured. The Startup class must be public and contain the following methods:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
}
public void Configure(IApplicationBuilder app)
{
}
}
I guess this will help you to understand how asp.net core handles Http requests.
Thanks
I am trying to convert an existing ASP.NET Web API project (currently hosted in IIS) into one that can use the SelfHost framework. I'm a bit fuzzy on the actual details but understand I can run a self-host server in a console window and then run the service on top of it. The problem I'm having is that my project is an MVC project and not a console one. My familiarity with console/Windows apps is somewhat limited as I generally work with projects to be hosted in IIS.
What I'm a bit confused on is whether I need to convert my existing Web API project in Visual Studio into a new console application, or if there's a way to create another console application Project in the solution which can act as the web server for the Web API services, or rather if there's a way to add a console element with a Main() entry point to the existing MVC project (overriding the Global.asax entry point.)
Search didn't yield much information that helps me fill this knowledge gap. Hoping someone can point me in the right direction. Even at a high level.
I recently had to convert a Web API project into a self-hosted service using OWIN (on Visual Studio 2013). I did that as follows:
Manually added Program.cs and Startup.cs files at the root of the project. Both files containing code as described here: http://www.asp.net/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api.
Went to the properties of the Web API project. On the "Applications" section, I stated "Output Type" as "Console Application", and set the "Program" class as the "Startup object".
Although not required, I slightly modified the using block within Program.Main() to look as follows:
// Start OWIN host
using (WebApp.Start<Startup>(url: baseAddress))
{
// Create HttpCient and make a request to api/values
HttpClient client = new HttpClient();
var response = client.GetAsync(baseAddress + "api/values").Result;
if (response != null)
{
Console.WriteLine("Information from service: {0}", response.Content.ReadAsStringAsync().Result);
}
else
{
Console.WriteLine("ERROR: Impossible to connect to service");
}
Console.WriteLine();
Console.WriteLine("Press ENTER to stop the server and close app...");
Console.ReadLine();
}
Finally, instead of calling config.Routes.MapHttpRoute() multiple times within Startup.Configuration(), you can refer to the routes you already wrote for the Web API:
// Configure Web API for self-host.
var config = new HttpConfiguration();
WebApiConfig.Register(config);
app.UseWebApi(config);