问题
Our web services are accessed through a router which uses port forwarding to connect to the actual web server. When we are trying to establish a web reference in Visual Studio it seems the .NET framework returns the full URL with a port to Visual Studio for the reference during discovery. Visual Studio then tries to make a connection back to that web service to create a proxy object (or whatever) and is denied because we don't allow access on that non-standard port.
Original web service URL
http://webservices.example.com/SomeService.asmx
Returned web service URL from router
http://webservices.example.com:25001/SomeService.asmx
My question is if you know how to make the .NET framework NOT include the port when returning the URL from the discovery call. Here's a diagram depicting this process:
回答1:
This might do the trick:
Manually setting soap:address in ASMX
Here's the code snippet just in case the link dies:
using System.Configuration;
using System.Web.Services.Description;
namespace Thinktecture.Tools.Web.Services.Metadata
{
public class SoapAddressReflector : SoapExtensionReflector
{
public override void ReflectMethod()
{
ServiceDescription sd = ReflectionContext.ServiceDescription;
foreach (Service service in sd.Services)
{
foreach (Port port in service.Ports)
{
foreach (ServiceDescriptionFormatExtension extension in port.Extensions)
{
if (extension is SoapAddressBinding)
{
SoapAddressBinding address = (SoapAddressBinding)extension;
// Set the address here:
address.Location = ConfigurationManager.AppSettings["SoapAddress"];
}
}
}
}
}
}
}
Config:
<configuration>
<appSettings>
<add key="SoapAddress" value="http://www.thinktecture.com/Test/"/>
</appSettings>
<system.web>
<webServices>
<soapExtensionReflectorTypes>
<add type="Thinktecture.Tools.Web.Services.Metadata.SoapAddressReflector, SoapAddressReflector"/>
</soapExtensionReflectorTypes>
</webServices>
</system.web>
</configuration>
Update:
The technique above seems to work just fine but has some limitations if you have more than one service. You also need to be running in Full Trust so doing this in a partial trust environment isn't going to work.
I'm not sure there's any other magic config switch or attribute to make this happen other than using the SoapExtensionReflector. After a good Google around, all the results point to the SoapExtensionReflector method.
But how about this? If the web services don't change all that often you could take a copy of the generated WDSL, edit the SOAP/HTTP bindings, and then publish it on it's own as a static XML file?
来源:https://stackoverflow.com/questions/640641/how-can-i-use-net-web-services-from-non-standard-ports