Sunday, 7 January 2018

Consuming WCF Service In MVC Application Using Entity Framework

Here we post the how to consuming wcf service in mvc application using entity framework then follow the following steps.
1. Make sure that Entity framework is already installed with Visual Studio.otherwise,install it using NuGet Packages.
Create a table in your database with the name of "test" as the following.















Step 1. Create a blank solution.
File-New-Project

type the name of the solution "WCFMVCEF". Then select the template MVC then create the solution.



























Step 2: creating an WCF Application.
Right click on the WCFMVCEF solution go to Add->New Project then open "Add New Project" window select Visual C# then display the WCF Service Application.
Shown in below screen.















Select WCF Service Application and type the name as WCFServiceApp click on OK.

Step 3:
Creating a service for CRUD operation.
Right click on the WCFServiceApp project and select Add->New Item.
Choose Web option from left pane and select "WCF Service".
Type the name as "MyService.svc" and click on Add button.
















Step 4: Creating an Entity Framework Model.
Right click on the WCFServiceApp project and select Add-->New Item.





















Choose Data option from left pane and select "ADO.NET Entity data model".

























Type the name as "EntityModel.edmx" and click on Add button,same as in the following images.




















Step 5:
Write a service for CRUD operation.
  1. Open MyService.csv page from WCFService application and write the following code:
















  1. Now ,open IMyService and write the "ServiceContract" and "DataContract",as follows.

















Service has been completed .now build the service.
Run the service and copy the service URL,as shown in the following image(http://localhost:52911/MyService.svc), for creating the reference.

















Step 6 : Creating an MVC Application
Now right click on the "WCFMVCEF" solution in solution Explorer again.
Select a new project.
Select ASP .NET MVC4 Web Application.
Enter the name of the application as "MVCApplication".

Click on the OK.

Adding a project priority and setting the reference.
  1. Right click on the MVCApplication and click on add service refrence as shown in the below image.
  2. Paste the copied Service URL in the given address and press Go button.
  3. All the Services will display ,as in the following screen just give the namespace as "ServiceRefernce1" and click on OK button.























Since WCF service application and MVC application both are in the same solution ,we have to build the service first and then the MVC application in order to consume the service in MVC application .
Do the following steps for that.
  1. Right Click on the WCFMVCEF solution in solution Explorer and click on properties.
  2. Check the "Multiple Startup Project" and set the application priority for WCF and MVC application as shown in the following screen.
















Create a Model:
right click on model folder and click on class.write the class name as "User" and create the following properties.

















Create a controller:
Right click on the Controller folder and click on add controller. Give the name of controller as "HomeController" and write the following action for CRUD operation.





















Creating a View:
Creating a view is very simple just right click on All action of the controller and click on Add View .the following is the code for all views (Index).


@model IEnumerable<MVCApplication.Models.User>

@{
ViewBag.Title = "Index";
}


@using (Html.BeginForm())
{
<div>
<h2>User Details</h2>
@Html.ActionLink("Add User", "Add", "")
</div>
<div>
<table>
<tr style="background-color: #FFFACD; text-align:center">
<th style="text-align:left">
ID
</th>
<th style="text-align:left">
Name
</th>
<th style="text-align:left">
Author
</th>
</tr>

@{
foreach (var item in Model)
{
<tr style="background-color: #FFFFF0">
<td>
@item.id
</td>
<td>
@item.name
</td>

<td>
@Html.ActionLink("Edit", "Edit", new { id = @item.id }) /@Html.ActionLink("Delete", "Delete", new { id = @item.id})

</td>

</tr>
}
}

</table>

</div>


}  


Now press F5 to run the Application.

Hope your Application view will be like the following image.



























Thursday, 4 January 2018

MVC Application Using WCF

Step 1 :
Ensure that the WCF project is open then start another instance of VS 2015. 
Then go to "File"-->"New"->"Project" then create an ASP .NET MVC Web Application . 
Name it "MVCWCFApp" then click "OK" button .
Then Select the project template "Internet Application" then click the "OK" button.




Step 2:
Create a Proxy using "Add Service Reference" by right clicking on Application in the Solution Explorer.
Note : Proxy is a class that converts a local request into a remote request.

A new window will open .paste the URL that you copied from the WCFMVCApp project into the address TextBox then click "GO".

In other words ,a proxy class is created successfully...
A ServiceRefernce will added into your project.


















Step 3:
Now add a new Controller then in solution Explorer select "Controller" then right click on it then select "Add Controller.
Name it UserController click the "Add" button.












Step 4:
Add the following code in the UserController.cs file.
Please build the project.

















Step 5:
Now right click inside the Index() method then select "Add View" then click on "Create a strongly Typed View" checkboc then select the model class we created,in other words "User(WebApplication.ServiceRefernce)" then select "Scaffold template List" then click the "Add" button.



















Step 6 :
Run the application
And see the results all the data in the user table is shown below the using WCF service.

Monday, 1 January 2018

MVC Application Using WCF Service

Creating ASP  .NET  MVC App With  WCF Service.

Step 1 :


Go to VS 2015 and Select "File"--> "New Web Site" then select "WCF Service" then provide the specific root path and provide the name "WCFMVCApp".

















Click on "OK" then create a solution.




















Step 2 :

Go to the solution Explorer then go to "WCFMVCApp" then right click on it then select "Add"->"Add new item"-> "Add ADO .Net Entity data model". Then click the "Add" button then place the file in the App_code folder.



















Step 3 :

Then the Entity Data Model Wizard will be shown. Select "Generate data from database " then click the "Next" button.



















Step 4:

Choose your data connection. I am selecting my existing database of SQLServer 2016.
Activate the radio button "Yes include the sensitive data in the connection string".
Save the connection string in the config file by enabling the check box "Save entity connection setting in web.config as then click the Next button.




















Step 5 :
Choose which database object you want in your model.here i am selecting the one of the table

Create table Users
UserID int constraint pk primary key,
FirstName varchar(30) not null,
LastName varchar(30),
Email varchar(30));

you can use the above table or create your own table and use it.
Then click the "Finish" button.
Please ensure that in the Solution Explorer under the App_code folder the Model.edmx file has been created.
Your entity data model is ready for use.

























Step 6 :

Go to Solution Explorer then expand the App_code folder then go to the Iservice.cs file.




















Step 7:

Delete the Getdata() and GetDataUsingDataContract() methods.
Then i am writing one method GetUser() as follows.


















Step 8:

Then go to the Service.cs file and implement the method declared in the IService.cs interface.
In the Service.cs file right click the Iservice Interface then select "Implement Interface"-->"Implement Interface".

Delete the already present method in the Service.cs the file. And write the following code in the "public List<ASPUser>GetUser()" method.
Is showing below code.


























Step 9:

Open the Service.svc file and now run the project.
A new window will open,copy the URL present in that window and paste it into any text file. This URL will be used in the next project.





















http://localhost:55708/Service.svc