How to instantiate a WCF DataServices Client without hard-coding the URI - wcf

I'm fairly new to WCF DataServices (OData) and I need to know the best way to instantiate the entity container on the client without hard-coding the URI. It seems like all of the examples on MSDN describe instantiating the client like this:
Uri uri = new Uri("http://www.someservice.svc");
DataServiceContext svc = new DataServiceContext(uri);
However, I know I must be missing something somewhere, because it doesn't make any sense to hard-code a service address like this. For one thing, how do you dynamically change the address when you move from Development to Test to QA to Production, when each environment is likely to have a different URI?
Thanks for any insights into this.

Put your DataService URL into e.g. your Settings file or just plain app.config:
Uri uri = new Uri(ConfigurationManager.AppSettings["ServiceURI"]);
DataServiceContext svc = new DataServiceContext(uri);
And in your app.config (or web.config for a web app):
<appSettings>
<add key="ServiceURI" value="http://www.someservice.svc" />
</appSettings>
Or grab it from a database config table..... or or or or or..... plenty of choices!
The URI is just a string - you can grab that from just about any configuration source you might have.

If you are working with a Silverlight app you can access the Uri of the xap with application.current.host
Then you can add a relative Uri to get the service Uri:
Uri base = application.current.host;
Uri relService = new Uri("..\someservice.svc", System.UriKind.Relative);
Uri service = new Uri(base, relService);
DataServiceContext svc = new DataServiceContext(service);

Related

How to store configuration in Window8 RT Application?

I need to connect Win8 app to WCF service. The WCF client is created using Add Service Reference.
The requirement is to change the End point URL at runtime. Win 8 does not have app.config and instead use Application Data settings.
The problem is that App needs the URL at the startup. If URL is invalid, or cannot be reached, the app is terminated.
The question is, How to manage scenarios, where configuration settings are required for app startup, and to be set up at runtime?
When you create an instance of the client, before you start using it you can change the address for the service by accessing the Endpoint property of the client. Something like the code below.
var client = new ServiceReference1.ServiceClient();
client.Endpoint.Address = new EndpointAddress(yourNewUri);

how to dynamic binding of web service URL

i store web service url in web.config or app.config. when i call web service like
ServiceAvailabilityTestClient.TestClient servAvailClient = new ServiceAvailabilityTestClient.TestClient();
servAvailClient.url= myapp.config url here.
servAvailClient.CallValidateCityPostalCodeZip();
the problem is the property called url is not appearing. sp please tell me what to do.
i just add wsdl file location as service reference because web service path called not being added. web service url which i try to add as web service reference is
https://devwebservices.purolator.com/EWS/V1/ServiceAvailability/ServiceAvailabilityService.asmx
1) i just not being able why i am not being able to add this web service url as service reference
2) why url property is not exposed in client side.
can anyone guide me what is the matter. thanks
You could do it through your generated client or ChannelFactory:
var client = ChannelFactory(IWcfService).CreateChannel(Binding, ServiceModel.EndpointAdress)
or
var client = New Client(binding, RemoteAdress)
EndpointAdress just takes string or uri in constructor e.g.http://yourservice.asmx
Are you using the <System.ServiceModel/> config section? If so you shouldn't have to do anything - the URL will be loaded from the <client/> section of the config when you create the channel.

specify location in wsdl:import

I'm trying to have my WCF services run completely off HTTPS. However when WCF generates the WSDL it provides locations that are not secure (http) and not allowed (rejected) by server.
How do i get this:
<wsdl:import namespace="https://www.mydomain.com/ogc/csw/ebrim/wsdl1.1" location="http://www.mydomain.com/ogc/csw?wsdl=wsdl0"/>
to read this (notice location attribute now using https):
<wsdl:import namespace="https://www.mydomain.com/ogc/csw/ebrim/wsdl1.1" location="https://www.mydomain.com/ogc/csw?wsdl=wsdl0"/>
I can specify my namespace in my interface file but i don't' see how to specify location.:
[System.ServiceModel.ServiceContractAttribute(Namespace = "https://www.mydomain.com/ogc/csw/ebrim/wsdl1.1", ConfigurationName = "MyDomain")]
public interface ICatalog
{
I've tried modifying my baseAddress, but that didn't do anything.
You have to specify the httpsGetEnabled configuration property on the wcf stack. Check the following link to the MSDN

WP7 dynamically change webservice url

I'm relying on a server backend for my WP7 app.
The server part is to be installed by the administrator on a host computer (not important).
I have to change the url in the app dynamically the first time the app is running on the phone (the user will be prompted to enter the url, and it will be saved in the isolated storage on the phone)
Question is: How do I change the url from the default app to point to the new url given by the user.
The webservice is a WCF service which holds all the data properties and collections, those I need in my app.
Let me know if
Actually I needed to change the url on the generated *Client.
So it looks like:
client.Endpoint.Address = new EndpointAddress(new Uri("http://server/service.svc);
If you're using c# you can change the service url by changing the url property on you serviceobject.
Service myService = new Service();
myService.Url = UrlToService;
myService.doLotsOfStuff();
Assuming its the same as full .NET, try this
WebSerivce.url = UrlFromUser.

maven, wsgen and dynamic url

After implementing a test solution described here :
Use Maven to trigger a wsgen & wsimport in a row, using wsdlLocation
I wonder if there is a way to generate the client jar without knowing the WS URL, so that it would be usable against any similar ws deployed somewhere else.
Any idea?
I wonder if there is a way to generate the client jar without knowing the WS URL, so that it would be usable against any similar ws deployed somewhere else.
Whatever WSDL URI has been used to generate the JAX-WS client artifacts, you can override a service endpoint address from the client code by using the appropriate constructor:
...
URL newEndpoint = new URL("http://new/endpointaddress?wsdl");
QName serviceName = new QName("http://targetNamespaceURI","EchoService");
EchoService service = new EchoService(newEndpoint, serviceName);
Echo port = service.getEchoPort();
System.out.println("Server said: " + echo.echo(args[0]));
...
Related question
How to change webservice url endpoint?
See also
Changing WSDL url (endpoint) in JAX-WS client