Why does console apps use proxy by default? - c#

I am currently having some issues with class library that doesn't behave as its console app.
The purpose of the console app is to send messages to an azure queue, which it does without any problem, and can see in ressource monitor that it makes calls through our web proxy and to our azure queue. This is done by default, I haven't told it anyway that it should use this proxy.
The class library on the other hand, does the same thing, but does not use the proxy, and therefore not able to send its data.
Both projects are identical, in the way they make the call to send a message, but for some reason is the console app, which intention is only to send a message, and library which intention is the same, act differently - why does the console app try to use proxy, and how do i force the class library to forcefully use the proxy?
conclusion:
How do i force a Microsoft.ServiceBus.Messaging.QueueClient.send
to use a proxy and not port 443

Per my knowledge, It is impossible to set the proxy while using service bus client.
The only connection options for Service Bus client are as following:
HTTP - port 80
HTTPS - port 443
TCP - 9350 to 9354
For more information we can refer to:
ConnectivityMode Enum
In your class library, please set the connectivity mode to Http as below code and try again:
ServiceBusEnvironment.SystemConnectivity.Mode = ConnectivityMode.Http;
Similar thread as yours: Azure Service Bus working behind proxy

To answer my own question.
I resolved the issue by creating a proxy.config , and add it to my app.config.
proxy.config format:
<?xml version="1.0"?>
<defaultProxy enabled="true">
<proxy autoDetect="False" proxyaddress="http://<proxyaddress>:<port>" />
<bypasslist>
<add address="localhost" />
</bypasslist>
</defaultProxy>
and add the proxy.config into my app.config as a system.net configuration:
<system.net>
<defaultProxy configSource="proxy.config" />
</system.net>

Related

ServicePoint.ConnectionLimit is not respected nor the default limit if there is a machine level proxy

Environment Windows11/.NET Framework 4.7.2
For some testing purposes, I setup an environment where all traffic was routed to a local proxy run mitmproxy using Windows proxy settings.
I am running a console app where it makes concurrent HTTP GET calls for some benchmarking and other study. The code is organized to use HttpClient pool. However I tried to limit connection limit for the outbound calls I was not able to do it unless I explicitly set ServicePointManager.DefaultConnectionLimit to a value. Even the default config value did not work:
<configuration>
<system.net>
<connectionManagement>
<add address = "*" maxconnection = "2" />
</connectionManagement>
<settings>
<servicePointManager expect100Continue="false" useNagleAlgorithm="false" dnsRefreshTimeout="30000" />
</settings>
</system.net>
</configuration>
The weirdest of all is that if I printed out ServicePointManager.DefaultConnectionLimit to console it would show 2 which is also happen to be the default if nothing is assigned; but it gets even weirder because if I just added:
ServicePointManager.DefaultConnectionLimit = ServicePointManager.DefaultConnectionLimit;
Then it would limit connection count to 2.
Anyways, I did check and check and check and stepped into .NET code then I found that if I add proxy's address to the list of service points then it will respect that. The issue is that if there is a proxy then it will ignore the actual server URI in service point table and uses proxy's value instead. This causing issue because I can no longer control connection limit per server while using a proxy. I tried to search internet for anything related but I was not able to find anything useful. Has anybody else faced this? Is this a .NET bug? Any answer would be greatly appreciated.

RESTcall with Restsharp does not work (using a Proxy)

I am trying to make a RESTcall in C# with Restsharp (V 106.6.10). When I am executing the code on my personal PC it works very well. But when I am executing the same code on another PC (for work), which is using a Proxy, it does not work anymore. I am getting the following error:
Anfragefehler: Error calling TestPut: Unable to connect to the remote server (0)
TestPut is the method which should call the API
To solve that I have written the following code in my app.config. This should declare the proxy:
<system.net>
<defaultProxy enabled="true">
<proxy bypassonlocal="True"
proxyaddress="http://[IP]:[Port]/"/>
</defaultProxy>
</system.net>
I also tried to assign the Proxy in the constructor of my APIclient with:
WebProxy myproxy = new WebProxy("[IP]", [Port]);
request.Proxy = myproxy;
I also logged the ProxyURL and this seems to be correct...
What could be the error? Do you need any additional information?
I am calling an endpoint from AWS and when I call this Endpoint in the Browser it works.
Thank you in advance
Lukas

Azure cloud service embedded FTP server

I want to host an embedded FTP server inside an Azure cloud service worker role.
To provide passive access to the FTP server, it uses port range 20000-21000.
Inside the ServiceDefinition.csdef I define all needed ports (see screenshot).
The main problem is the huge number of ports. If I try to upload the service into the cloud I get the following error.
Validation error: Invalid number of input endpoints - current 1002,
max. 25
How can I get this work with cloud service?
Here is a solution based on Azure support answer.
You will need to define a public IP in the .cscfg file and upload it the cloud service.
<?xml version="1.0" encoding="utf-8"?>
<ServiceConfiguration serviceName="ILPIPSample" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration" osFamily="4" osVersion="*" schemaVersion="2014-01.2.3">
<Role name="WebRole1">
<Instances count="1" />
<ConfigurationSettings>
<Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" value="UseDevelopmentStorage=true" />
</ConfigurationSettings>
</Role>
<NetworkConfiguration>
<AddressAssignments>
<InstanceAddress roleName="WebRole1">
<PublicIPs>
<PublicIP name="MyPublicIP" domainNameLabel="WebPublicIP" />
</PublicIPs>
</InstanceAddress>
</AddressAssignments>
</NetworkConfiguration>
</ServiceConfiguration>
More info: https://learn.microsoft.com/en-us/azure/virtual-network/virtual-networks-instance-level-public-ip#manage-an-ilpip-for-a-cloud-services-role-instance
After that you can use nslookup to get the public IP assigned to the instance. If you have multiple instances, you need to change the 0 to 1, 2, 3 etc.
nslookup WebPublicIP.0.<Cloud Service Name>.cloudapp.net
Then you can open the local ports in Windows Firewall of the instance and you will be able to connect the local ports directly from the internet.
You can create a startup task to open the local ports in the cloud service firewall.
Following is an example of how to configure firewall rules. The startup task is executed every time the instance is rebooted/reimaged.
https://learn.microsoft.com/en-us/azure/cloud-services/cloud-services-startup-tasks-common#add-firewall-rules
Something like below:
netsh advfirewall firewall add rule name="TCP ports" protocol=TCP dir=in localport=1000-2000 action=allow
When a client connects to an FTP server using passive mode, it will make 2 connections.
One using port 21, and another for transferring data.
So it looks like you need to open a single port in ServiceDefinition.csdef and then create a port forwarding rule on the firewall (load balancer) to redirect all of the passive ports to that single port.
<Endpoints>
<InputEndpoint name="FTP2Azure.Command" protocol="tcp" port="21" localPort="9003" />
<InstanceInputEndpoint name="FTP2Azure.Passive" protocol="tcp" localPort="9002">
<AllocatePublicPortFrom>
<FixedPortRange max="21000" min="20000" />
</AllocatePublicPortFrom>
</InstanceInputEndpoint>
</Endpoints>
This is untested, but might help.

App absolutely refuses to use proxy for localhost requests

I have Fiddler running on port 8888
Web.config:
<system.net>
<defaultProxy enabled="true" useDefaultCredentials="true">
<proxy bypassonlocal="False" proxyaddress="http://localhost:8888" usesystemdefault="False" autoDetect="False" />
</defaultProxy>
</system.net>
App code (specifies credentials for the API controller its talking to):
var wc = new WebClient();
wc.Proxy = new WebProxy(new Uri($"http://localhost:8888"), false);
wc.Credentials = new NetworkCredential("myuser", "mypass");
var response = wc.UploadString("http://localhost:11026/api/mycontroller/mymethod", "POST", request);
I cannot for the life of me get it to communicate via Fiddler so I can debug requests. Am I doing something wrong?
UPDATE
This information is supplied by Fiddler help but having used localhost.fiddler in both web.config and app code, still not being captured by Fiddler (and when Fiddler is closed it doesn't cause a connection failed error)
Solution 2: Use http://ipv4.fiddler
Use http://ipv4.fiddler to hit localhost on the IPv4 adapter. This
works especially well with the Visual Studio test webserver (codename:
Cassini) because the test server only listens on the IPv4 loopback
adapter. Use http://ipv6.fiddler to hit localhost on the IPv6 adapter,
or use http://localhost.fiddler to hit localhost using "localhost" in
the Host header. This last option should work best with IIS Express.
Rather than trying to send it via the proxy, send it via one of the fiddler loopbacks.
Such as http://fiddler.ipv4:11026 ... ...
Details are given here:
http://docs.telerik.com/fiddler/observe-traffic/troubleshooting/notraffictolocalhost
Shows how to set it up and capture traffic. It is the method I've used when I want to see traffic being passed between local web apis and my Mvc project

send message to azure service bus queue from web api

I am trying to get started with Azure Service Bus queues. following this article
https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-dotnet-get-started-with-queues
the only difference is that I am trying to do this from within a web api.
The error I get is:
No connection could be made because the target machine actively refused it 40.84.xxx.xx:443
I'd appreciate any help or pointers!
Note: Console app works just fine following the above guide.
Updated 7/24, this is the code in my action method:
try
{
var connectionString =
"Endpoint=sb://xxxxx-test.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=shared_access_key";
var queueName = "testqueue1";
var client =
QueueClient.CreateFromConnectionString(connectionString, queueName);
var message = new BrokeredMessage(eventMessage);
client.Send(message);
}
catch (Exception e)
{
//log exception
}
Update 7/25. I was able to make it work by setting defaultConfig entry as enabled in web.config:
<system.net>
<defaultProxy enabled="true"/>
</system.net>
No connection could be made because the target machine actively refused it 40.84.xxx.xx:443
Please check whether the outbound 443 port is blocked by your firewall.
Note: Console app works just fine following the above guide.
The default value of connectivity mode for Service Bus is AutoDetect. It will automatically selects between the Tcp and Http modes based on an auto-detection mechanism that probes whether either connectivity option is available for the current network environment. It maybe choose different modes for your Console App and Web API application. Try to set it to TCP explicitly in your Web API application before using the Service Bus Queue.
ServiceBusEnvironment.SystemConnectivity.Mode = ConnectivityMode.Tcp;
I was able to make it work by setting defaultConfig entry as enabled in web.config:
<system.net>
<defaultProxy enabled="true"/>
</system.net>

Categories

Resources