WP7 dynamically change webservice url - wcf

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.

Related

Web Api documentation with swashbuckle

We are currently trying to start writing WebApi services for our products switching from traditional WCF SOAP based services. The challenge we have got is how to provide the api documentation. I came across the SwaggerUi/swash buckle.
One limitation we have is we do not want to host the WebApi services in IIS but in a Windows Service. I am new to Web Api so I might be doing things the wrong way.
So for testing, I am hosting the web api in a console application. I can use the HttpClient to invoke the Get method on the Web Api but I can't access the same if I type the url in a web browser (is this normal for self hosted web api?).
So I installed the Swashbuckle.core nuget package and included the following code in the Startup class (Owin selfhosted).
var config = new HttpConfiguration();
config
.EnableSwagger(c =>
{
c.IncludeXmlComments(GetXmlCommentsPath());
c.SingleApiVersion("v1", "WebApi");
c.ResolveConflictingActions(x => x.First());
})
.EnableSwaggerUi();
private static string GetXmlCommentsPath()
{
var path = $#"{AppDomain.CurrentDomain.BaseDirectory}\WebApiHost.XML";
return path;
}
When I browse to the following location
http://localhost:5000/swagger/ui/index
I get "page cannot be displayed" in IE. Similar for chrome.
Is there anything special that needs to be done when hosting a WebApi in a console/windows service application to get the documentation automatically?
(I have enabled Xml documentation for the project)
I have now attached a test project. Please follow the link below:
Test project
Regards,
Nas
Your problem is not with Swashbuckle, which is configured correctly. Instead it is with the fact that your OWin web app has closed by the time the browser navigates to the swagger URI. Your using statement means that the web app is shut down at the end of it - well before Chrome has opened and navigated to the swagger path. You need to ensure the web app is still running - then your path will be valid (although in your source code you have different ports 9000 and 5000 in your url variables).

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.

WCF 3.5 find the url the client used to get to the service (server side)

I am trying to find the url the client used to get to the server, (on the server side). I have tried the following:
Uri url = OperationContext.Current.Channel.RemoteAddress.Uri;
Uri url = OperationContext.Current.Channel.Local.Uri;
Uri url = OperationContext.Current.EndpointDispatcher.EndpointAddress.Uri;
but none of these seemed to work correctly. They all return the server name, (with the fully qualifed domain. such as myserver.mydomain.local. Even if I used used myserver/myservice.svc it would get the full name.
At this point I don't have a service and domain available to test going to mywebsite.com/myservice.svc to see if it has the machine name or not.
The result I want to get (from the server would be):
http://mywebsite.com/myservice.svc
rather than
http://myserver.mydomain.local/myservice.svc
which is what seems to be happening at the moment.
If you have access to the HttpContext you can use HttpContext.Current.Request.Url.ToString().
OperationContext.Current.IncomingMessageHeaders.To
Should give the Uri that client used to connect to the server.

Call a web-service under current user credentials

I have a custom WCF web-service confugured with windows authentication and a WPF client application that needs to call the former. The service checks the username and pull some specific data from a database. So I have to call the service using credentials of the user running the application.
The problem is my service is hosted under another site with windows authentication and users can authenticate there with another accounts. Windows (or IE?) caches last accout used and then my client app uses it too!
Example:
I enter the website under "MYDOMAIN\AdminUser"
I run following code (from the client app, it's not web code)
var client = new TestServiceClient();
var currentUser = WindowsIdentity.GetCurrent(); // just informative field nothing more, i don't use it anyhow
// currentUser.Name = "MYDOMAIN\\MyUserName" - it's current value, i'm not trying to set it
client.ClientCredentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
var data = client.GetTestData();
Service gets called by "MYDOMAIN\AdminUser"..
I know I can create NetworkCredential with name and password but I then will have to store it somewhere, encript it and so on..
To clarify the problem: client process running under one account calls the service under another account by itself, just becouse windows supplies the call with another credentials under the hood.