How to use Wcf web service in MVC using C# - asp.net-mvc

I have use wcf web service for data connectivity and all data related operation.
So, In my application, i need to integrate MVC controller pass or get model using the WCF webservice.
Please help how to pass model to WCF webservice from controller and How to get that Model or list of items into Controller ???

It is a bad idea to pass your actual controller to the WCF service. You don't want to couple your view tightly with the data contract of an external service.
Create a separate ViewModel that fulfills the needs of the view. Then implement code in the ViewModel to call the web service to update data and a static factory method on the ViewModel that creates a new ViewModel instance by retrieving data from the web service and populating a ViewModel instance.

Related

How to pass object from webrole.cs to the controller of MVC?

I am working on web role in the Azure cloud service. Basically my web role is an MVC application and how can I make the controller in MVC communicate with the webrole.cs class. For example, in the run() method in webrole.cs I have received a message and I want to pass it to the MVC controller, how can I do that?
Any help is appreciated!
You'd better use a azure service bus queue to do that. Send a message on Run, and take the message from queue in your mvc app.

Entities in MVC and WCF

I am working in an MVC web application. This application also use WCF service and Entity-framework.
In solution explorer I have two projects 1. MVC 2. WCF Service.
I am little bit confuse how should I use entities in MVC and WCF service.
For instance I have created Employee.cs class in Model folder in MVC.
And in controller I call a method of Get Employee() of WCF service which returns employee class type.
In WCF service how I will return as employee type because I added that class in Model folder of MVC project which is not accessible to MY WCF service project.
So in this case how I should use that entity in such way that it can be accessible to MVC as well to my WCF service.
Please let me know you need more clarification..
To share the same classes between the projects,create another class library and and the shared code in there.
Then reference the class library from both projects.

Model v/s Controller - wcf communication

In a typical ASP.net mvc application which layer should talk to WCF service ? Is it model or the controller ? Which approach is benefical ?
Thanks
Josh
Normally that should be the data layer. The controller then talks to the data layer. Whether the data is stored inside a database, flat file or coming from a remote web service, the data layer should be responsible for retrieving and manipulating it. You could then of course have different implementations for the data layer depending on where your data is coming from.
You need to have an abstraction (interface) over the operations you need to perform with the data and then have your Controller take this interface as constructor argument. For the case of WCF, you could directly use the interface that was created for you when you imported the definition of the WCF service - the client side proxy service contract.
Here's a similar answer I wrote with an example.

ASP.NET MVC, WCF Service, and validation?

My ASP.NET MVC controller constructors create an instance of a service class by supplying it with (among other things) a ModelStateWrapper (similar to this article). Since this object is handled through a reference, any changes made to it in the service are reflected when the controller accesses that ModelStateWrapper object.
I need to convert that logical service layer into WCF services that will be hosted in IIS.
Assuming I want to continue performing validation in the service layer, which approach is better?
Pass the ModelStateWrapper object by reference (possible according to MSDN) with each service operation
Keep track of errors in the service and have the client explicitly retrieve a list of errors after any/all service operations are performed
Any other suggestions or links to articles that would help me perform validation behind a WCF service?
It should be noted that these services will eventually be consumed by a variety of UIs (ASP.NET MVC, Winform, Console, maybe even third-party).
Since you already have everything in place and working, pass the ModelStateWrapper over the wire to WCF service. Let it fill it out and send back.
The other platforms can use this same wrapper to get the error information.

Using WCF with wsHttpBinding with Web Forms vs MVC

I have used WCF in the past with my Webforms so my solution was
MyWebformApp = WCF(Model+business) + Web Forms
So when I want to work with the MVC for presentation arch. How do u use WCF with ASP.net MVC ?
Are your data contracts a part of the model ? How do you register the datacontracts as properties?
Roughly here are a few steps:
Create a proxy of the service using svcutil.exe and include it in your application
Create an interface which will abstract all the necessary methods you need to call from the application (IRepository)
Implement this repository and call your WCF service (work with the generated client)
Inject the repository into the controller constructor
In the meantime think about the view models you would set and the mapping between the objects coming from the web service and those view models.

Resources