3

I have a project that uses Entity Framework Model First. The data source is in the internet, and I need it work using a network/web proxy for it. I had google it but I can't find any useful result, because all results are about EF's Entities Proxies. Some helpful code:

The connection string:

<add name="ConnectionName" connectionString="metadata=res://*/Latin.csdl|res://*/Latin.ssdl|res://*/Latin.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=mssqlserver.server.com;initial catalog=DatabaseName;user id=admin;password=adminPass;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

The usage:

static void Main(string[] args)
    {

        //set the proxy
        var proxy = new WebProxy("109.32.24.24", 808);
        proxy.Credentials = new NetworkCredential("", "");
        WebRequest.DefaultWebProxy = proxy;

        var db = new ContextName();

        if (db.QNames.First().Name == "Testing")
        {
            Console.WriteLine("Success Connection!");
            Console.ReadKey();
        }

    }

This raises a network connection error.


EDIT


The proxy that the network have is running over http protocol. I know that http is the protocol you normally use to browse the web with, you do not usually connect to databases with it but it is the only available and this question is about it. You can use web proxies for several .net connections, even Web Services, and my question is about using it with EF, I could't make this working. One last solution could be to use a program like Proxifier and redirect all computer's connections to the specified web proxy, but the idea is making it a bit more configurable.

Raúl Otaño
  • 4,326
  • 3
  • 24
  • 59
  • What @flindeberg said. What _kind_ of proxy server do you need to connect to the internet? Is it a SOCKS proxy? – CodeCaster Nov 03 '15 at 21:55
  • @flindeberg and #CodeCaster sorry for the delay. Hope my edit helps for finding answers. This is what I'm trying to do, and can't find the way for doing it. – Raúl Otaño Nov 03 '15 at 22:13
  • @CodeCaster `Why do you think you need this?` Well, the only way for access internet is using that Proxy, and the database is in the internet. I really found this comments usesless, If your are not going to help please stop making this kind of questions. Thanks – Raúl Otaño Nov 04 '15 at 14:32
  • Your question is unclear and makes little sense, that's why I'm asking for clarification. You can find my comment useless, but that's your loss. :) I'm not asking those questions to annoy you, but to make your question answerable. Currently it is not answerable. – CodeCaster Nov 04 '15 at 14:33
  • Well @CodeCaster, I really don't see why this question is not clear, what I want is very simple: connect an EF application to one SQL server in the internet, with the only restriction that this application may be running in a internetless network, that is the only way for getting internet is using an Http proxy. I you think this is not posible, please, make an answer, and try to explain why. I will appreciate it. – Raúl Otaño Nov 04 '15 at 14:40

1 Answers1

1

this application may be running in a internetless network, that the only way for getting internet is using an HTTP proxy

Then you cannot do this, or at least not easily.

When a network is configured such that only HTTP traffic may leave the network, then you are restricted to ... only making HTTP calls. SQL Server's wire protocol is a custom, proprietary binary protocol, that does not resemble HTTP in any way. Hence you cannot connect to a remote instance of SQL Server through an HTTP proxy, if HTTP is your only way out. That's why I asked what kind of proxy you're talking about, because for example a SOCKS proxy does allow arbitrary TCP connections to run through it, and Windows supports it natively.

You can however set up an HTTP tunnel through the proxy. See Tunnel any kind of TCP traffic through HTTP/s. This assumes you control both sides of the connection, and can run or install arbitrary software (namely the tunneling software) next to your application software.

This also may or may not work depending on the configuration of the client's proxy server.

However, this looks like an XY problem. Depending on the actual problem, there are various approaches.

You do not want to expose, nor connect to SQL Server over the internet anyway.

Perhaps you could write a webservice instead, that your application then consumes. The web service talks HTTP, so it can be connected to through the HTTP proxy without any additional configuration or software. The webservice runs on, or nearby the SQL Server and you can expose your business logic through the service - without having to expose your SQL Server.

Another alternative would be a VPN connection, but again, all that depends on the actual problem and which sides of the connection you can control.

Community
  • 1
  • 1
CodeCaster
  • 131,656
  • 19
  • 190
  • 236
  • Great answer!!! @CodeCaster, I have one question about the answer, when you said `This also may or may not work depending on the configuration of the client's proxy server` talking about the tunneling over http, what exactly do you means? in other words, it is possible that the client's proxy web server disable the tunneling, do you have any useful link or doc? – Raúl Otaño Nov 04 '15 at 15:14
  • 1
    Yes, that's exactly what it means. A well-configured HTTP proxy server will detect and disallow tunneling of non-HTTP traffic over HTTP. – CodeCaster Nov 04 '15 at 15:17