Tabish Sarwar
Tabish Sarwar

Reputation: 1535

Consuming WCF Service from Asp.net WebApi

I am creating a Asp.net Webapi for our services . On the other hand I have to pull some data from an existing WCF Service.

I am creating a web reference in my asp.net webapi project . But it seems there is no proxy generated.

I tried with VS 2012 RC and then with VS 2010. I can not reference it from my model/controller any where in the project. Is there a different way of creating proxy in MVC webapi. I dont know what is wrong.

Does it has to do something with like i can not consume a wcf service from a rest service

What is that i am missing here .

Thanks.

Upvotes: 4

Views: 15825

Answers (2)

sanjay
sanjay

Reputation: 11

If your service reference is added but the reference.cs is generated empty then it could be some issue with type conflict. Following work-around worked for me.

To correct it, right click the service reference, click Configure service reference. On the configuration screen, uncheck the "Reuse types in referenced assemblies". It should automatically update the service reference, if not update forcefully.

This should generate the reference.cs file appropriately.

Upvotes: 1

Martin4ndersen
Martin4ndersen

Reputation: 2876

Try adding a service reference rather than a web referance, which is used for the deprecated ASP.NET web services (ASMX).

I just tried this and it works as expected. Here is what I did:

  1. Created a new ASP.NET MVC 4 Web Application and selected the Web API project template.
  2. Added a WCF Service Application project to the solution. Remember to build this before adding a service reference to it.
  3. In the Web APi project, I added a service reference to the WCF service.
  4. Replaced the code in ValuesController.Get(int id) with

    public string Get(int id)
    {
        var serviceReference1 = new ServiceReference1.Service1Client();
        return serviceReference1.GetData(id);
    }
    

When browsing

http://localhost:<port>/api/values/2

I receive

<?xml version="1.0" encoding="utf-8"?><string>You entered: 2</string>

Upvotes: 3

Related Questions