access Web API from windows web service - windows-services

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

Related

SAPUI5 OData 401 (Unauthorized) + No Access-Control-Allow-Origin Header

I work in SAP Web IDE to develop an SAPUI5 application which uses OData Service CUAN_IMPORT_SRV.
I start by defining the OData Model this way:
var oModel = new sap.ui.model.odata.v2.ODataModel("https://host:port/sap/opu/odata/sap/CUAN_IMPORT_SRV/", true, "UNAME", "PASSWORD");
Username and Password can access the data and do read + create operations on Hybris Marketing. This is tested with Postman.
However, when I try to test my application with Test Fiori Launchpad, I get this error message when executing the OData call:
Concering this topic, I read some other posts and tried to edit the URL to
proxy/https/host:port/sap/opu/odata/sap/CUAN_IMPORT_SRV/
...but this results in 404 Not Found.
Since I use Chrome as Browser, I disabled web security as I read in other posts. This did not work. That's why I tried this Chrome AddOn. As a result, I got this error log:
What more can I try to solve this issue?
Thank you for any advice!
Tried configuring destination in Cloud Platform:
Web IDE does not allow Cross Origin access.
In order access your ODATA service from Web IDE, you have to use Cloud Connector.
Cloud connector will create a ternel between AP Cloud platform and your ODATA Service Gateway System.
Follow these steps:
https://blogs.sap.com/2014/06/22/how-to-configure-an-external-gw-system-with-sap-river-rde/

How to comunicate with Service fabric stateles . NET CORE API

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

SignalR v2 with load balancer

I am pretty new to SignalR and have been going through some tutorials as I've been tasked with upgrading our current implementation.
We've got an ASP.NET MVC application which uses SignalR (version 1.x). The application is in our F5 load-balanced cloud environment. So we have multiple sites (for different customers) using the same load balancer. To make the SignalR calls servers-side, we use the HubConnection from the Microsoft.ASPNET.SignalR.Client namespace and create a proxy like this (full example here):
var hubConnection = new HubConnection("http://www.contoso.com/");
IHubProxy stockTickerHubProxy = hubConnection.CreateHubProxy("StockTickerHub");
stockTickerHubProxy.On<Stock>("UpdateStockPrice", stock => Console.WriteLine("Stock update for {0} new price {1}", stock.Symbol, stock.Price));
await hubConnection.Start();
Where http://contoso.com/ is the current customer's site URL.
We are looking to upgrade to the latest SignalR (version 2.x) and I am wondering if it is necessary to even use the HubConnection. Even though the article above specifies version 2, it does mention:
This document provides an introduction to using the Hubs API for SignalR version 2 in .NET clients, such as Windows Store (WinRT), WPF, Silverlight, and console applications.
This is a web application with a regular class library back-end for data access. Taking a look at this tutorial, I don't see anything about HubConnection (it also doesn't mention load balancing). Consider the following, from the Chat tutorial:
public class ChatHub : Hub
{
public void Send(string name, string message)
{
// Call the addNewMessageToPage method to update clients.
Clients.All.addNewMessageToPage(name, message);
}
}
Then, in Statup.cs:
public class Startup
{
public void Configuration(IAppBuilder app)
{
// Any connection or hub wire up and configuration should go here
app.MapSignalR();
}
}
So my question is, are we using SignalR properly? If not, what considerations/modifications need to be made when running a load-balanced application which uses SignalR (v2.x)? I haven't been able to find much regarding load balancing, etc.
Or is this a job for Groups?
You don't need to change anything structural. Have a look at the signalr redis scaleout, or any other scaleout option. Basically you need to install an additional package and ms opentech redis to do load balancing. So the scaleout will make sure that each request is sent over a message bus, making multiple servers possible.

Consume Secured web service in Asp.Net MVC

I'm working on asp.net mvc
I want to consume secured web service in my project
In previous I can consume unsecured web service (asmx) by calling wsdl to create proxy class ,now
I tried to create proxy class for the service by using wsdl.exe by using the following formula
Wsdl /language:language /protocol:protocol /namespace:myNameSpace /out:filename /username:username /password:password /domain:domain
But I had the following error
Error: There was an error processing 'https:// .asmx?wsdl'.
- There was an error downloading 'https:// .asmx?wsdl'
- Unable to connect to the remote server
- A socket operation was attempted to an unreachable network ??.???.???.??:???
Can you tell me how can I consume secured web service in my project
Yara
I dont know what sort of service you are talking about but I suspect you are Adding it as a Service/Web reference. If thats the case then assuming its called WebService1 and that the security you are talking about it basic authentication then:
WebService1 svc = new WebService1();
svc.AuthenticationHeaderValue = new WebService1.AuthenticationHeader();
svc.AuthenticationHeaderValue.UserName = "username";
svc.AuthenticationHeaderValue.Password = "password";
Try that. As I said I dont have much info on what you are trying to do so its a guess.
First of all it doesn't matter you are in MVC or any other technology.
If you can call https webservice using simple code, it will do wonders for you.
So first try calling simple HTTPS web service, google and play around it.
For learning purpose and to gain more knowledge, check these links
How to call HTTPS web service method from a .net console application?
Calling a https web service (C#)
http://support.microsoft.com/kb/901183

.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