I have created a desktop application in WPF that I wish to supply to clients as a .exe file.
Currently the application has a web service referenced to it where the web service would be sitting on the clients web server.
There is a high possibility that the URL of the web service could change depending on the clients therefore is it possible to add an option for the user to add the service reference themselves once they know the web service URL?
In the app.config is where the endpoint address is set, so if when the application fired up, it presented the user with a text box to enter the url, then on button click the application updates the service reference. Is this possible?
I have come across lots of different articles however was not sure if it was possible without have to recompile the code?
Assuming it's a WCF service, if it's called Service1 you can set its address like this:
Service1Client wcfServiceClient = new Service1Client();
wcfServiceClient.Endpoint.Address = new System.ServiceModel.EndpointAddress("your uri here");
//now you will invoke the service in the address you defined
an ASMX service (still called Service1 in this example for consistency) can be setup like this:
Service1 asmxService = new Service1();
asmxService.Url = "your uri here";
Related
I am new to WCF development and I am trying to create a WCF service hosted in a console app.
I have already created the WCF service and tested it by running it on IIS Express. Doing so, the WCF service will be accessible from http://localhost:5576/MyFirstService.svc. Within the service, I have defined a GET endpoint /test/<param> just to test if it works. Upon visiting the url with Postman http://localhost:5576/MyFirstService.svc/test/123, it will echo back 123.
My console app that hosts the WCF on the other hand is super simple. I followed the tutorial (http://www.topwcftutorials.net/2014/05/wcf-self-hosting-console-application.html). The relevant code is below:
Uri httpBaseAddress = new Uri("http://localhost:4321/StudentService");
//Instantiate ServiceHost
studentServiceHost = new ServiceHost(typeof(StudentService.StudentService), httpBaseAddress);
//Add Endpoint to Host
studentServiceHost.AddServiceEndpoint(typeof(StudentService.IStudentService), new WSHttpBinding(), "");
//Metadata Exchange
ServiceMetadataBehavior serviceBehavior = new ServiceMetadataBehavior();
serviceBehavior.HttpGetEnabled = true;
studentServiceHost.Description.Behaviors.Add(serviceBehavior);
//Open
studentServiceHost.Open();
Console.WriteLine("Service is live now at : {0}", httpBaseAddress);
Console.ReadKey();
As I launched the console app and visited http://localhost:4321/StudentService, I am greeted with the standard page talking about wsdl. However, if I tried to visit http://localhost:4321/StudentService/test/123, I get a 400 bad request error.
Am I doing things right? What is the path that I should be using to get to my endpoints? I tried many variations of the URL and it just does not seem to work.
Your first service, http://localhost:5576/MyFirstService.svc, was running within the context of IIS (yes even though it's express) so that is what allows for the URL (REST style) routing like you were seeing in your "test/123" example.
But the code example, and posted reference link, is actually a self-hosted service from a console application which doesn't utilize IIS or WAS (Windows Activation Service) so routing won't be available.
Don't get me wrong, your StudentService will still work just fine if called via SOAP, just not from a REST perspective which is what Postman is used for.
There are free tools out there like SoapUI that work just like Postman to test your WCF services.
Different from the last command in doc https://learn.microsoft.com/en-us/dotnet/framework/wcf/how-to-create-a-wcf-client, the program hints me to use 'svcutil.exe /language:cs /out:generatedProxy.cs /config:app.config http://localhost:8000/ServiceModelSamples/service?wsdl' to generate client proxy code and the config file when I follow the tutorial exactly.
So I have two questions.
Does base address must start with 'http://'? Just like what is shown in https://learn.microsoft.com/en-us/dotnet/framework/wcf/how-to-host-and-run-a-basic-wcf-service. Can I use some other kind of base address if I don't use http binding?
If the answer to #1 is yes, what will the command be? It's better if you can give me an example.
Yes the base address must start with http or https because it needs to be hosted on a web server (like IIS). If you have done that you need to create a service reference to your project via: right click "Connected Services" --> add "Service reference" then type in your address choose your .svc file --> choose a name (e.g. ServiceRef) and click ok..
Then add your proxy to execute methods from the service like:
ServiceRef.ServiceRefClient proxy = new ServiceRef.ServiceRefClient();
bool testresult = proxy.TestConnection();
I'm working on an Azure service for a Windows Phone app. I need the Azure Service to access the users' OneDrive. Following this article, my scenario should be:
The user sign in to Windows Live on the WP app.
The Live web service sends the authorization code to a redirect URI that I defined, with the code appended as a query parameter named code, as:
http://www.example.com/callback.php?code=2bd12503-7e88-bfe7-c5c7-82274a740ff
I get the authorization code and access the users' data
After investigating a lot in Service, I still can't find a way to capture the query parameter in my web service. As I am new to this area, I don't know where to focus on. I'll be really appreciated if you can give my an advise or answer my following questions:
Can I access the service just using the url with parameter in a browser? How can I see if the service is working properly?
An article mentioned using WCF [Web Get] attribute to get Query Parameters, but I still don't know how to implement both the IService1.cs and Service1.cs file, could you give me a sample about how to access the value of Query Parameter?
Thanks!
I'm not sure if i understand your problem properly but if you want your RESTfull WCF service to be the callback receiver for the request code, your Service must be hosted with a WebHttpBinding and a ServiceContract similar to this one.
[ServiceContract]
public interface IService
{
[WebGet(UriTemplate = "callback?code={requestCode}")]
void OAuthCallback(string requestCode);
}
So if the base address of your Service is "http://service.mydomain.com/MyService.svc" the OAuthCallback Method will be called when a http GET request to "http://service.mydomain.com/MyService.svc/callback?code=RequestCode" is made.
I want to be able to consume a WCF Service endpoint in my Windows Phone 8 app.
Searching on Google only showed me that I had to Right-Click on the WP8 Project, select 'Add Service Reference'... Which is not a viable solution in my case.
I want to be able to consume a WCF service inside my Windows Phone 8 app, programmatically.
Where do I define my client endpoint certificate in a Windows Phone 8 app?
Imagine that I want to make a Windows Phone 8 app, which should be able to connect to a WCF service hosted on another device, i.e. a computer. Then the WP user needs to enter the hostname of that computer in order to be able to connect to the WCF service.
I advice you to use "Add Service Reference" to generate the proxy class.
The DTO and Client proxy will be automatically generated. You will benefit from a huge boost in productivity, type safety and name checking.
Then you can specify the url at runtime using the appropriate constructor. For instance :
private MyServiceClient GetMyServiceClient(string url)
{
Uri uri = new Uri(url);
BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
EndpointAddress address = new EndpointAddress(uri);
MyServiceClient client = new MyServiceClient(binding, address);
return client;
}
(MySericeClient being the generated proxy)
The .config stuff is optional, you can remove it.
When you add a Service Reference, your WP8 Project is auto generating a proxy class that wraps the WCF Service. Then your code uses this proxy class.
The other way of doing this is creating the proxy class manually, and believe me, you want to avoid this if you can...
Proxy Client class generated by Add Service Reference will use hostname (endpoint address) from config only when you use its parameterless constructor. You can specify endpoint adress manually at runtime of course.
You can create service contract portable class library and share it between client and server. Then you dont have to generate proxy classes, but you use ChannelFactory API: http://www.c-sharpcorner.com/UploadFile/ff2f08/channel-factory-in-wcf/
I need to access simultaniously multiple instances of a web services with the following Url. The web services is hosted in IIS and has SSL enabled.
https://services.mysite.com/data/data.asmx
Usually, when we do this process manually, we go one by one and update the Windows host file (c:\Windows\System32\drivers\etc\hosts) like this :
192.1.1.100 services.mysite.com
I would like to automate the process and do it with some multithreading. So I cannot change the Host file. Is there a way to simulate a host file when we do a HTTP request in C#?
Thanks!
If you know the IP address of the server's SSL endpoint (which isn't necessarily the same as the server's default IP address), then you could just aim you web-service at that? Obviously the SSL check will fail, but you can disable that through code...
ServicePointManager.ServerCertificateValidationCallback += delegate
{
return true; // you might want to check some of the certificate detials...
};
I think you get the same effect by setting the proxy server of that specific request to the IP address of the actual Web server you want to send the request to.
You can change the URL that your request is hitting at runtime, something like this:
svc.Url = "http://firstServer.com";
So if you create a program that loops through each of your desired servers, just update the URL property directly (that example is taken from WSE 3 based web services).