JonBull2013
JonBull2013

Reputation: 315

Dynmically consume a web service in Monotouch

I have an issue that hopefully someone could advise me on.

I am creating an enterprise iPhone application that will capture information and write it to a local server.

Depending on which of our sites the user is at will depend on which server the information is written to.

I have created a webservice which I can consume in monotouch and pass it information which it will then write to a SQL database on the local server. The problem I face is how do I go about doing this for all of our locations? If i put a web service at each location then each location will need its own version of the app that write using their webservice (all the servers are on the same network and are not separate)

Can I dynamically consume a webservice by passing the relevant URL?

Should I be taking another approach?

Any advice would be appreciated.

Upvotes: 1

Views: 578

Answers (2)

Luke
Luke

Reputation: 3655

EDIT - I probably should mention we use WCF-style web services that require bindings being generated with SISvcUtil.exe (that is where the WebServiceClient class comes from).

We do something similar where we have a test server and production server and it is simple to change the URL where the web service points to. Obviously this does assume that the web service hosted at all the different locations are the same...

When creating the Client object which consumes the web service you need to specify bindings and an endpointaddress, you can simply change the endpointaddress string to point at the appropriate server. The code below should give you an idea of how to do this...

BasicHttpBinding binding = new BasicHttpBinding();
binding.OpenTimeout = new TimeSpan(0,1,0);
binding.CloseTimeout = new TimeSpan(0,1,0);
binding.SendTimeout = new TimeSpan(0,1,0);
//snip - any other bindings you need to specify...

string fullDomain;
string domain;

if (local)
    domain = "local.server.com";
else
    domain = "production.server.com";

fullDomain = string.Format("https://{0}/WebService/Service.svc", domain);

EndpointAddress endpointAddress = new EndpointAddress(fullDomain);

WebServiceClient client = new WebServiceClient(binding, endpointAddress);

Upvotes: 1

tomfanning
tomfanning

Reputation: 9670

Yes. There should be either a constructor or a Url property you can set which enables you to pass the URL to your different service endpoints at runtime.

(I presume here that you are talking about multiple instances of the same web service hosted at different URLs)

Upvotes: 0

Related Questions