Proxy WCF wizard
on client side I try to create a proxy to my WCF service.
It created cool. But only small thing trouble me.
The matter I want it has and IP address开发者_如何学运维 as service name. But really has my machine name. Briefly
I have http://mycomputer/blabla
in various site after generation
But I need it like this : http://93.48.56.74/blabla
where 93.48.56.74
is the IP address of my computer where I generate this.
How to solve this?
The reason it is using your machine name, is because that is the address advertised in the generated WSDL. As commented, you can just modify your Web.config or App.config and it will use the IP address.
So much for the simple answer :)
Alternatively, you can change the WSDL. If you're using .NET 4.0*, you can apply the
<useRequestHeadersForMetadataAddress>
behavior to your service configuration.
After the behavior has been added, the service metadata will advertise the same endpoint address that you use to retrieve the WSDL. So if you add a Service Reference to http://93.48.56.74/blabla, the proxy and configuration generated should use the IP address rather than the host name.
*: There is also a hotfix available for .NET 3.5: KB971842 (don't mind the description of the hotfix)
The regular approach would be to configure you client using the configuration file and specify the server name or ip address as it fits your needs here.
But from what I can tell you are doing redesigning of the serving calling for regular reconfigurations/updates of the client in which case you manual changes will be overwritten.
In this case you have two approaches:
- You can initialize the client and set the parameters (including the service url) programmatically.
- You might choose to overwrite the server name in you hosts file to point it to the desired ip address.
The most correct way would be to do the initialization programmatically. The specific depends on you implementation, but here is an example using the service EchoService
. It can likely be simplified; I have copy and pasted from a test initialization where I needed the running host.
public void SetUp()
{
// the service address
var baseAddress = new Uri("http://127.0.0.1:3123/");
host = new WebServiceHost(typeof(EchoService), baseAddress);
ServiceEndpoint sep = host.AddServiceEndpoint(typeof(IEchoService), new WebHttpBinding(), "");
sep.Behaviors.Add(new WebHttpBehavior());
echoFactory = new ChannelFactory<IEchoService>(new WebHttpBinding(), sep.Address);
echoFactory.Endpoint.Behaviors.Add(new WebHttpBehavior());
client = echoFactory.CreateChannel(sep.Address, baseAddress);
}
精彩评论