Consuming a WCF REST Service in .NET? - wcf

I've this WCF service hosted in a Sharepoint 2010 web application. The WCF has been created using the following factory:
Factory="Microsoft.SharePoint.Client.Services.MultipleBaseAddressWebServiceHostFactory, Microsoft.SharePoint.Client.ServerRuntime, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
As you can see this is creating a REST-type service. Because we needed to consume it client-side via jQuery.
Now we also need to access it from a .NET project (WinForms). I've tried adding the Service Reference in VS but it doesn't find anything. I guess that's because it's not a SOAP service right?
So how can I consume it in C# .NET project without breaking the existing jQuery support?

Yes, Add Service Reference does not work for WCF REST services. You have a couple of options here:
Add another endpoint to your service, a SOAP-based one, so that you'll be able to use Add Service Reference on your client. I don't know exactly what the MultipleBaseAddressWebServiceHostFactory does, but you could look how it creates its endpoint(s) and recreate that in another factory - and then add the additional SOAP endpoint
Another alternative is to share the interface definition (the service contract plus any existing data contracts) between the service implementation and the .NET client. Having that interface you can use the WebChannelFactory<T> to create a proxy to the REST service (like you'd have one for a SOAP service).

Related

How to consume wcf in .net core without adding service reference?

I want to consume a WCF service in my .NET Core application without adding service reference?
I am able to consume WCF by installing WCF connected service and adding service reference but I don't want to add any service reference.
Please refer to the follow link. It has some minors issues, but is easy to fix.
Calling Web Method Without a Service Reference
if you are using wcf service then you can expose it as a restful service. then you can simply consume it. actully i have been use it for many years and its been really useful.
here you can read more about that: REST / SOAP endpoints for a WCF service
you can create a service proxy using svcutil.exe and use the proxy class as a reference.
here is a sample:
SvcUtil.exe http://www.temperatureservice.com:8080/TemperatureService.svc /ser:DataContractSerializer /s /language:cs /out:TemperatureServiceProxy.cs /n:*,WCF.Client.Proxies.Proxy
It will generate TemperatureServiceProxy.cs file, include this file in your project and create a service client by referencing this file.

How do I add a WCF Service Reference for my .NET Core App (.NET Framework)

I'm only using a .net framework with it and I can add a pure c# project that contains the service reference - but I can't create a Service Client with this approach, it tells me it cannot find an endpoint with the proper values
(Contract, Endpoint name, both are identical to the endpoint I copied over to my web.config.)
Is there a specific procedure in which you can add a service reference when it comes to ASP .NET Core? or is it just like regular .NET Framework?
Thanks in advance.
There are multiple ways to access wcf service, If you have control over wcf service implementation side. you can change your service to restful wcf service. if you don't have access to that use seviceutil.exe to get proxy class and config file.
1. first copy code from proxy class.
2. second add class reference to your ASP.NET code behind.
Check how to use serviceutil.exe on google.
Also go to http://www.wcftutorial.net/ for more details.

Consuming WCF service (without metadata) on a non-.net platform

I have created a WCF service and hosted it through self hosting. This service doesn't have any metada published.
First Question
Can I consume it through Visual Studio, Add Service Reference? Hopefully not.
Can I consume it by creating manual proxy, i.e. ChannelFactory<ServiceContract>....?Hopefully yes.
Now in the second scenario, the client must be .Net, right?
So it implies that, to consume a wcf service on a non-.net platform, we have to expose its metadata?
Can't a WCF service without metadata, consume by Ajax client, or say Java client??
There are 3 options to consume a WCF Service:
If the service exposes a WSDL use "add service reference" from VS (or an equivalent from another platform). Note that if you do not want to expose the WSDL you could expose it just temporarly, save the WSDL in a file, and then send it to user in any platform to generate proxy from it. You can turn off the WSDL immediately after you save it. Also note that even if the WSDL is not exposed still you need to protect the web service from unauthorized access.
If this is a .Net client it can compile with the same Service Contract assembly and use ChannelFactory etc.
Any platform can send raw soap message (e.g. XML) to the service. Of course they need to know what is the right format. A WSDL can help but even without it if they have a working sample they can imitate it.
WCF provides REST (Representational State Transfer) support to consume it by non .NET client like JavaScript (AJAX), java, Objective C, web browser, etc...
Basically WCF REST is exposes methods and transferring data over the HTTP protocol and it supports all HTTP operations (GET, POST, PUT, and DELETE). This feature is making it platform independent as well as it doesn’t require metadata exposed.
Please refere below links to get more about WCF REST:
An Introduction To RESTful Services With WCF
WCF REST Programming Model Overview
WCF Rest vs. WCF SOAP
Create RESTful WCF Service API: Step By Step Guide

Can I add ony specific WCF endpoints to a .NET 2.0 project as web references?

I'm developing a .NET 2.0 client application that needs to connect to a WCF service. I add a web reference to a basicHttpBinding WCF service that we've developed and VS creates the proxy class and the config paraphenalia which is fine. The problem is that I only want to use a small fraction of the methods that the WCF service implements and not carry around the extra implementations that the client app doesn't need.
I was thinking of creating a different basicHttpBinding endpoint and put the methods there. Is there a way for only one endpoint of a WCF service to be referenced by a .NET 2.0 project?
Regards,
Frank
When you add a web reference to a service, you always get all the service methods. It's the service (implementation) that defines the scope of what ends up in the WSDL.
The only option to limit the scope of the method your client generates would be to create a second WCF service on the backend, which only implements those few methods that you want in your client - just having a second endpoint won't really help.

Converting ASMX to WCF Web Service

I need to upgrade our web services to use WCF instead of ASMX. If the signatures of the web services stays the same, will existing clients that already call the ASMX service have to change anything on their end? Is there anyway to still use WCF but not force them to change anything?
Option 1 :
Using the current ASMX's WSDL, generate the client using svcutil.exe
Grab the generated interface and create a WCF service based on this interface
Output : One new WCF endpoint configured with basicHttpBinding. Clients need to update the URL at which they're sending the messages.
Option 2 :
Refactor your ASMX code. Move all the logic into a separate DLL.
Create a WCF service and use the logic in the refactored DLL.
Output : 2 endpoints, one for ASMX and another one for WCF
If you use the BasicHttpBinding for your new WCF service, and implement the same methods with the same message structure, existing callers should be able to call into this new WCF service without any change on their part.
There's also an AspNetCompatibilityRequirements attribute in order to get around some potential compatibility issue - see the MSDN documentation on it.
Marc