Reputation: 61
I have created a simple WCF Service for purpose of displaying a table details. My service is ready.
Now in my external MVC 3 Application i need to consume my WCF Service. So i have done Add Service Reference.
How can i write the proxy code in my MVC 3 app to consume my service? I am new to WCF and ASP.NET MVC ..what should i do next to consume wcf service?
My Code:
Interface
[ServiceContract]
public interface IBooksService
{
[OperationContract]
string GetBooksInfo(int BookId);
}
Class
public class BooksService:IBooksService
{
public string GetBooksInfo(int BookId)
{
string ConnectionString = "myDB";
SqlConnection con = new SqlConnection(ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("Get_BooksInfo", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@BookId", BookId));
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
return dr[0].ToString();
else
return "-1";
}
}
In mvc 3 app, i added service reference.
Upvotes: 0
Views: 4241
Reputation: 11458
First of all, you want to make a using
reference to your WCF Service in your controller to whatever name you set when you added it to your application. So if you called it WCFService
and your project was called MyMvcApplication
you'd so something like this: using MyMvcApplication.WCFService
Then you'd instantiate that with the name of your service class in your controller, so if your .svc
class was called ServiceClass
you'd do something like this: ServiceClass myService = new ServiceClass();
Then in your Actions/Methods you'd simply call the methods you created on your ServiceClass
like so: myService.MyMethod(myParam);
In your controller you'd have something like:
using MyMvcApplication.WCFService
public class MyController : Controller
{
private BooksServiceClient myService = new BooksServiceClient();
public ActionResult Details(int id)
{
var book = myService.GetBooksInfo(id);
return View(book);
}
}
Does this not work then? As your code looks fine...
If this doesn't work then I suggest looking at the new ASP.NET Web API
as that will replace WCF in the future and is far easier to implement and make call's too..
Upvotes: 2
Reputation: 3199
The simplest way is to generate a proxy for the service and then call methods on that proxy. For simple cases the following code should be more than enough or you can use it as a starting point anyway:
BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress endpointAddress = new EndpointAddress(//url for the service goes here);
_proxy = ChannelFactory<ImplementedInterface>.CreateChannel(binding, endpointAddress);
Please note that this does not even require adding the service reference (as long as you have a copy of the interface implemented by the service) and it will give you a proxy that you can use like any other class in your project. Just make sure to catch wcf faults (FaultException, FaultException, TimeoutException, CommunicationException) and not the usual .Net ones when you call methods on the generated proxy.
Upvotes: 3