Determine port in asp.net core - c#

I want to make some microservices with asp.net core (Web Api)
For service discovery I want use Consul where I must register the services via the HTTP API including IP and port.
My question is:
How can I know which port is used within in a service? Is there a way to determine the used port via code?

Yeah, Use HttpContext.Connection.LocalPort to get the local port that the server is listening on.

Related

Communicating Azure Web app with WCF Web App

I need to host ASP.NET based app as Azure Web App which should communicate with WCF based Azure Web App. ( WCF app is also an azure Web App not an on-premise app at all).
What are possible ways to achieve easy and flexible communication between both app services?
Do we need to establish a communication channel specifically?
Both Apps under IIS On-Premises are used to communicate without any problem over NET TCP protocol. But I think now we need both services to communicate over HTTP instead of Net TCP?
What problems we may have?
What things do we need to bear in mind?
First of all, we need to be clear that azure webapp only supports ports 80 and 443.
Secondly, in your project, you said that it runs normally in local, because I don't know what you mean by communicate, and I don't know how to achieve it. Normal communication may be socket or websocket, etc. It is not clear what communication you are talking about. If it is socket and you need to use a port other than 443/80, then it is not supported.
But I know clearly that azure web app supports wcf, which means that both of your webapps can be deployed successfully and run normally. If there is a problem, you can update the error details in the post, and we can provide you with further assistance.

Integrating an API that uses SSL to connect machines

I want to integrate an API that will allow my application to send requests to a web server. Unfortunately, this API is not well documented, and I have not gotten a reply from the person who supports the web service that released the API. The instructions on how to integrate the API are the following:
All API calls connect to the standard SSL port, and must begin with
https://www.websitename.com/api.php?username=username&password=password&,
followed by the list of parameters expressed as parametername=value&
I am new to C# development. Can you interpret this set of instructions and tell me how I'd go about integrating this API? I mainly am confused about connecting to the SSL port.
This sounds like it's just an HTTP request to that particular URL. You want to use the WebClient class, and possibly call the DownloadString method, depending on what the response contains.
The standard SSL port is 443, but if you're using a library that parses URLs correctly, it will connect to that port automatically if it sees https:// at the beginning. You can use a custom port by providing it after the hostname (separated by a :), such as https://websitename.com:8443/, if you had an SSL service running on port 8443 instead of 443.

basic doubts about asp.net mvc 4 and wcf

Well, I am thinking about creating a web application with C# and asp.net mvc 4. The idea is create an asp.net web application that can be use in any browser, so I can use my application anywhere and any computer.
But in the communication, can I use WCF or the communication is over HTTP and I can't choose other transport?
I mean that if WCF is only to communicate two desktop/mobile applications or it could be use for web applications too?
In a web application is possible to have a duplex communication?
You can use HTTP bindings in WCF if you want to use SOAP. Otherwise you may want to look into WebApi which provides a more natural abstraction over HTTP. For duplex communication over HTTP you can consider SignalR.
can I use WCF
Yes, WCF can be hosted in an asp.net application.
Duplex Communication cant induced easily in Client-server model where system is working in disconnected fashion.
What happen when when your client(browser) want some data.
1) Request comes to server
2) Server ask to WCF duplex service and forgot to wait for response as it is duplex in nature.
3) WCF duplex respond to Server with data
4) Now server can process that data either by saving or logging to Database but doesn't know who was the client that asked for and how to intimate them.
So what is the solution
User SignalR with Some weird coding.
OR
Call WCF service directly from Browser by jquery $.ajax call So your browser directly will have response from WCF service.

How to best expose methods in WinForm?

I have a C# form application that connects to a electronic device using the serial port.
The class "'SerialCommunicationManager'" hooks up to the serial port on application startup and handles the dirty business of talkning to the device.
What I would like is to expose the following methods.
Write()
SerialDataReceived event
SerialDataTransmitted event
Primarily a local website running on the same machine is the one I want to expose the methods for, but in the future I imagine the need for external applications as well.
What is the easiest way to expose the functionality?
TCPIP client server?
Web service? (Can I create a web service inside a WinForm?)
other?
Big thanks
//David
I would recommend self-hosting a WCF Service. This provides you a huge amount of flexibility in terms of how you serve and expose this information, including being able to change the method by which its served via configuration.
It seems to me, that if you would like to do it properly, you should break apart your forms app, and create:
a service that handles serial comm and has an API exposed through remoting
a Forms app that uses the API and makes a way with the service
Then, depending on the locality of your web site, if it will remain local (or near local - LAN):
web site should use remoting to call the service
else, if you plan to have multiple web sites:
web service hosted inside the IIS that will wrap remoting API
web site that will use web service
However, if it is too much work to do - just use remoting and expose needed methods to the web site.
In a recent project we did the following:
Write a Console application (or Windows Service, depending on your needs) that communicates with the electronic device.
Make the Console application host a .NET 4 WCF service.
Write a .NET 2 Windows Forms application to communicate through Web Services with the console application.
In this context, I could imagine the website you are mentioning to also use Web Services (WSDL) to communicate with the Console application.

web application c#

Im trying to make a a cleint/server web application where eventually the client application will be deploy on the internet. People told me to use visual studio WCF duplex but im confuse on how that works. I know for a simple server/client application using sockets, u can just use console for the server and then a windows form for the client.
So how does WCF duplex work, will it be used for both the server and the client side or do i sill use console for the server?? Also how do they establish a connection, in sockets you use ip address and a port.. HELP!
There are many WCF bindings that enable full-duplex communication. WCF servers and clients are able to use sockets among other means of communication. With WCF you can still use the IP Address:Port address system to expose and consume services.
With WCF you still can use Console Apps, Windows Forms or Web Applications to expose or consume those services. It brings no restriction at this point.
WCF will provide flexibility, since once you want to change the binding or the address, you can do it in the configuration file, instead of doing it by changing the code.

Categories