0

The application application I'm currently working on is required to interface with a web service using SOAP. The service providers want to restrict access to the service via a firewall using BOTH an IP address and a Port. I'm using VS 2010 and the service has been added as a .NET 2.0 Web Service.

Right now the firewall rule for my connection's port is set to 'ANY' and the service team wants to tighten it down to a specific port. I can't seem to find any way to set a specific outgoing port (port used when exiting my web server) in my service.

Is it even possible to do this?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
D. Dubya
  • 189
  • 1
  • 12
  • The app was built in .NET 4. The service reference was added as a Web Service Reference (.NET 2.0 supported), not simply a Service Reference. – D. Dubya Oct 24 '12 at 18:52
  • I don't actually know how to do this using a Service Reference, but it's probably easier than my answer below. Is there a reason you're using a web reference? – John Saunders Oct 24 '12 at 19:06
  • I'm using a Web Reference because that's how the reference was added earlier in the project. I tried changing it to a Service Reference but that seemed to completely change the auto-generated objects. With where we are in the dev lifecycle it will have to remain as Web Reference for now. – D. Dubya Oct 25 '12 at 12:34

1 Answers1

1

It is possible to do this, but it's a non-trivial customization.

  1. See Ways to Customize your ASMX Client Proxy for the general techniques. Near the bottom, you'll find "Heavy-Duty Customization".
  2. By overriding the GetWebRequest method, you can gain access to the HttpWebRequest instance being used by the request.
  3. HttpWebRequest has a ServicePoint property.
  4. ServicePoint has a BindIPEndPointDelegate property.
  5. Set this property to point to a method that will decide which IP address and port to use.
John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • Thanks for the reply. I tried what you suggested: `protected override WebRequest GetWebRequest(Uri uri) { HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(uri); request.ServicePoint.BindIPEndPointDelegate = new BindIPEndPoint(DefineIpPort); return request; } private IPEndPoint DefineIpPort(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount) { return new IPEndPoint(IPAddress.Any, 1234); }` However, I tested the result using `netstat -ano` and it isn't using port 1234. Seems the app is still choosing an arbitrary port. – D. Dubya Oct 25 '12 at 12:35
  • Should I be able set a breakpoint in the extended class code? Each time I try I get the "Breakpoint will not currently be hit..." message. Wondering if I messed something up... – D. Dubya Oct 25 '12 at 12:39
  • Yes, you should be able to set a breakpoint. Be careful of what namespace you created the partial class in. It has to have the same name and namespace as the original. – John Saunders Oct 25 '12 at 14:31
  • Had to uncheck the "Debugging=>Enable Just My Code" option for the breakpoint. Namespace is identical, confirmed by removing `partial` keyword from class definition and receiving "another partial declaration of this type exists" error. Still can't get the app to use port 1234 for outgoing communication. :- / – D. Dubya Oct 26 '12 at 12:08