Consume WCF service programmatically in Windows Phone 8 - c#

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/

Related

WCF Service Endpoints not reachable from local

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.

Allow user to specify endpoint

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";

Programmatically connect Silverlight to WCF

I have inherited a Silverlight application that consumes a WCF service, and is hosted in an ASP.NET web form. The application needs to run over HTTP and HTTPS, and will be installed on a customer's own server. The client code was originally generated using the Add Service Reference pointing to the locally-hosted service, so my ServicesReferences.ClientConfig obviously contains hard-coded references to localhost - not much use when deploying to another server, so obviously I need to be able to programmatically set the endpoint address that the client uses.
My code is currently:
var binding = new BasicHttpBinding
{
MaxBufferSize = 2147483647,
MaxReceivedMessageSize = 2147483647,
};
binding.Security.Mode = HtmlPage.Document.DocumentUri.Scheme.StartsWith("https")
? BasicHttpSecurityMode.Transport
: BasicHttpSecurityMode.None;
var documentUri = HtmlPage.Document.DocumentUri;
var builder = new UriBuilder(
documentUri.Scheme,
documentUri.Host,
documentUri.Port,
documentUri.LocalPath.Replace("hostpage.aspx", "MyService.svc"));
var client = new CustomerDetailServicesClient(binding, new EndpointAddress(builder.Uri));
client.ChannelFactory.Faulted += OnChannelFactoryFaulted;
client.DoSomething();
and while this works OK when I use HTTP, attempts to access via HTTPS fail with 404s for each call to the WCF service. Using Fiddler, I can see that it is only the URI scheme that is changing, and if I enter the HTTPS address of the service, I get the expected metadata page.
Is there something obvious that I am missing?
sigh slap me with a wet herring. The HTTPS bindings for the web service had been commented out in web.config (since not all our customers can/want to run HTTPS on their servers, and having WCF bindings for HTTPS when IIS is not configured for HTTPS breaks WCF in lots of noisy ways).
It's amazing what fresh perspective and clarity a good night's sleep can bring to a problem.

Adding a SOAP web service reference

I am trying to add a SOAP web service in the VS.NET 2010 interface, but I get that the server refused the connection. The people in charge of the service tell me it is working. I asked if they had a wsdl file, but supposedly they have none.
Is the problem caused by their lack of wsdl, or can I assume there is a problem on my side?
If they are not willing to expose their service metadata on the service then see if they will give you access to the assemblies containing the service contract, operations, and data contracts. Then you can create a proxy to the service without needing any metadata.
// Create service proxy on the fly
var factory = new ChannelFactory<IMyServiceContract>("NameOfMyClientEndpointInConfigFile");
var proxy = factory.CreateChannel();
// Create data contract
var requestDataContract = new MyDataContract();
// Call service operation.
var responseDataContract = proxy.MyServiceOperation(requestDataContract);
It also helps if you have access to the service-side config file so you can copy the endpoint details out of there into your client config.
It looks like their service is not exposing metadata. Try and browse to the wsdl url and see if you get back anything. http://server/blah/blah?wsdl

DataContract create in WCF

At client side, I have this class without [DataContract]:
public class UserEntity
{
public string login;
public string password;
}
when I put [DataContract] and refresh the reference of this class at WCF side, then I can't initiate the web service. It says an error:
cannot create metadata...
What's wrong?
Are you sure that you actually know, why you can't refresh the reference? I mean you add [DataMember] - and it fails, you remove it - it works? Or it works several days ago and now you add [DataMember] (and many other stuff) and it not works now?
But anyway, the easiest way to solve "refresh reference" issues - to refresh reference manually with SvcUtil.exe. In this case error message would be much more descriptive the simple "oops! error!".
What is client and server side in your case? What is refreshing reference on the WCF side? Your description is very uncommon. Here is description how to create service with complex data type and WCF Class library:
Create WCF class library
Add data contract to the class library
Add service to class library
Implement service contract and service in the class library
Add host project
Reference WCF class library from host project
Host service from class library in host project
Add Metadata endpoint to your hosted service
Create client project
Run the host project outside the visual studio
Use Add service reference to create proxy on WCF service hosted in your host project
Implment code to call your service through created proxy
As you see there is no modification of data contract on the client side and no refreshing WCF service.

Categories

Resources