How to comunicate with Service fabric stateles . NET CORE API - asp.net-mvc

I'm new to service fabric and I need please small help...
I have front-end stateless .NET CORE 2.0 service. This web service will communicate with multiples stateless .NET CORE 2.0 APIs. - (I would like to have for each functionality separated API service...)* In each sample code is communication only with stateless service (not .NET CORE 2.0)... I have in API service code in controller, but I don't any exactly, how can I call this code from front-end web service. How to implement circuit breaker in this scenario. etc. Can someone help?

If you want to call REST API(or WEB-API) from .net core , you can do something like this:
var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
var stringTask = client.GetStringAsync("http://localhost:9098/api/User");
var msg = await stringTask;
Console.Write(msg);
Source: https://learn.microsoft.com/en-us/dotnet/csharp/tutorials/console-webapiclient

Related

Consuming a WCF service in .NET Core 2.0

I have a WSDL service which worked fine (and is still working in .net 4.x) and fairly newcomer to .NET Core 2.0.
I created the WSDL web service reference (The same steps followed as https://learn.microsoft.com/en-us/dotnet/core/additional-tools/wcf-web-service-reference-guide)
My question is how to consume this service? Do someone know of a good tutorial? Any help would be greatly appreciated.
Thank you in advance
My question is how to consume this service?
The WCF Web Service Reference Provider Tool creates everything you need.
The client is automatically created with all the end-points in the service and any associated classes such as service parameters.
During the WCF Web Service Reference Provider Tool wizard you specify a namespace for the client, so if you entered CompanyName.Service you'd be able to create the client by typing var client = new CompanyName.Service.ClientName();
Please note that the name ClientName will be generated by the tool, and intelli-sense will you give you the actual name.
Once you have the client you can call any method on the service in the normal way. Such as:
var response = client.CancelPolicyAsync(cancelRequest);
Please check the link here:
Calling a SOAP service in .net Core
Calling a SOAP service in .net Core
How to Call WCF Services and Create SOAP Services with ASP.NET Core
Click here!
Your WCF service still is on .NET Classic - so nothing changed - you should consume it as always you do as regular WCF service.
What you have done creating WSDL web service reference - you have created client for Standard framework. Put it into separate Standard project. Then it can be referenced in core and classic frameworks aps.

Consume Windows service from WebAPI

Is it possible to consume WebAPi into windows service. Because WebAPI is http protocol, so iam not sure weather i will consume WebApi.
I tried search for consuming WebAPI with Windows service. I can't even find single examples.
Can any face similar kind of scenario
Yes we can able able to consume WebAPi inside the Windows service. Since it is windows based service we need consume it with HttpClient object.

Autofac manual injection in ASP .NET Web API application with per request lifetime scope

I am developing ASP .NET Web API application and I need to use autofac manual injection in custom class (not web api controller) with per request lifetime scope. I have been searching for solutions for several days, and have found only the suggestion to use HttpRequestMessage in web api controller here
In web api controller:
var dependencyScope = actionContext.Request.GetDependencyScope();
var lifetimeScope = dependencyScope.GetRequestLifetimeScope();
var serviceObject = new AutofacWebApiDependencyResolver(lifetimeScope ).GetService(typeof(IMyService)) as IMyService;
But I, as far as I know, I cannot retrieve HttpRequestMessage object (actionContext.Request) in custom class. I know this is some odd approach and I should consider using constructor injection, but I am trying to add autofac to already existing big project and not to break everything for this step (this will definitely be refactored later).
Could you please help me with this issue.
Thanks for all responses!

access Web API from windows web service

I am developing a Windows service to run maintenance tasks on our customer's servers which returns results to a web service running in our headquarters.
I ran into the Web API as a new and simple alternative to build intuitive web services over HTTP.
So the question is whether or not it's possible to host a Web API web service in IIS 7 at our HQ which is accessed from several remote windows services to return result sets?
Yes it is possible, and there is specific guidance on how to do this provided by asp.net.
In principle, create your web api project and then from the client do:
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://mywebapi.mycompany.com/");
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
var message = new MaintenanceResult { result = resultstatus, server = "servername" };
response = client.PostAsJsonAsync("api/maintenanceresults", message).Result;
// now check for success, and response appropriately

.NET WebApi Authentication

Currently, I have an MVC web application that sells widgets. A user logs into our system using forms authentication, and can then do various functions based on the group they belong to(ie Place an order, View an Order, Cancel an Order, etc).
We've been tasked with writing an Api that will give third parties the ability to create and view orders in our system. Each third party will have it's own username and will be limited to certain api methods based upon the group they belong to.
We are looking at using Web Api as a mechanism to provide the api. We would also like to be able to consume this api from our MVC web application. Unfortunately, we are running into issues with Authentication for the Web Api. Using a DelegatingHandler, we have implemented Basic Authentication over SSL for our WebApi. This works great for our third parties. However, when trying to consume the Api from our MVC application we are getting 401 access denied errors because the user was authenticated in the MVC app using Forms authentication, but we have no way of passing those credentials on to the Web Api. Is there a way to pass the Forms Auth credentials from our MVC app to our Web api app?
IIS Setup
WebSite named WidgetStore with two web applications
WidgetStore\UI -uses forms authentication
WidgetStore\Api - uses basic authentication
Is there a way to pass the Forms Auth credentials from our MVC app to our Web api app?
Sure, let's take for example the following MVC controller action calling the Web API:
[Authorize]
public ActionResult CallWebApi()
{
var baseAddress = new Uri("https://example.com");
var cookieContainer = new CookieContainer();
using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer })
using (var client = new HttpClient(handler) { BaseAddress = baseAddress })
{
var authCookie = Request.Cookies[FormsAuthentication.FormsCookieName].Value;
cookieContainer.Add(baseAddress, new Cookie(FormsAuthentication.FormsCookieName, authCookie));
var result = client.GetAsync("/api/values").Result;
result.EnsureSuccessStatusCode();
// now you can read the result.Content ...
}
}
This assumes that you have also enabled forms authentication in the web.config of your Web API project and that the cookie name is the same as the one used in your MVC project.

Resources