Introduction
Let’s establish what the purpose of code is in the first place.
For this article, the purpose of code is to use a WCF Service without adding a proxy or Service Reference in ASP.NET MVC Application. There is a way to do this by using the Service Model Metadata Utility Tool (SVCUTIL.EXE), but we have also one better solution by writing our code.
Using the code
First of all we need to place our Service Contracts and Data Contracts into a shared library (WCF Service Library), which can be consumed by our client application.
Step 1: Create a New Project (WCF Service Library)
1) On the File menu, click New Project.
2) In the New Project dialog box under project types, expand Visual C#, and then click on WCF and select WCF Service Library . Then in the Name box, type "ServiceLibrary" and in the Solution name, type "WCFDemo" then click on OK.
3) Now, delete the default created interface and class perspective "IService1.cs" and "Service1.cs"
4) Add a New Interface by right-clicking on ServiceLibrary(project) > Add > New Item then select "Interface", name it "IUser.cs" then click on ADD.
5) Set the public
access modifier to interface IPayment
public interface IPayment
{
}
6) We need to add the [ServiceContract]
attribuite to interface IPayment
[ServiceContract]
public interface IPayment
{
}
7) We declare a one operation named as ReverseString
in IPayment interface and add the [OperationContract]
attribute.
[ServiceContract]
public interface IPayment
{
[OperationContract]
string ReverseString(string orignal);
}
Step 2: Add a new project to solution Explorer
1) Right-click on solution explorer "WCFDemo"> Add> New Project
2) In the New Project dialog box under Project types, expand Visual C#, and then click on WCF and select WCF Service Application. Then In the Name box, type "WCFService" then click on OK.
3) Delete default created Interface and Service perspective "IService1.cs" and "Service1.svc"
4) Add a new WCF Service. Right-click on WCFService (project) > Add > New Item. Then Select WCF Service on Add New Dialogbox and name it "User.SVC"
5) Delete the interface IUser.cs from WCFService (project). Because we already added this interface on ServiceLibrary (project).
6) Add the reference of "ServiceLibrary" into "WCFService". Right-click on the reference of WCFService (project). Then click on Add Reference.
7) On the Reference Manager Dialog box, select "Solution" under "Project" then check into "ServiceLibrary" then click on OK.
8) Now, the reference of "ServiceLibrary" project is added to the "WCFService" project. Now it's time to implement out ReverseString
operation. Paste the below code in User.svc.cs
public class User : IUser
{
public string ReverseString(string orignal)
{
if (string.IsNullOrWhiteSpace(orignal))
return string.Empty;
char[] charArray = orignal.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
}
Step 3 - It's Final Step To Access WCF Service without Adding a Proxy or Service Reference in ASP.NET MVC Application
1) Right-click on solution explorer "WCFDemo" > Add > New Project
2) In the New Project dialog box under Project types, expand Visual C#, and then click Web and select ASP.NET Web Application. Then name it "WebApps" then click on OK.
3) Now in the dialog box, click on the "Empty" under the ASP.NET 4.5.2 Templates, then check on "MVC" checkbox then click on OK.
4) Create a controller by right-clicking on the "Controller" folder > Add > Controller. Then select "MVC 5 Controller - Empty". Then click on Add, and finally name it "HomeController".
5) Default one get action method created as Index
public ActionResult Index()
{
return View();
}
6) Add a reference of "ServiceLibrary" into "WebApps". Right-click on the reference of WebApps (project). Then click on Add Reference. On the Reference Manager Dialog box, select "Solution" under "Project", then check into the "ServiceLibrary" then click on OK. Now, the reference of "ServiceLibrary" project is added to "WCFService" project.
7) Finally, all done. Now it's time to access our created operation method without using proxy or Service Reference.
Paste the below code in your Index
method.
public ActionResult Index()
{
ChannelFactory<IUser> channelFactory = null;
try
{
BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress endpointAddress = new EndpointAddress("http://localhost:50419/User.svc");
channelFactory = new ChannelFactory<IUser>(binding, endpointAddress);
IUser channel = channelFactory.CreateChannel();
string result = channel.ReverseString("Suchit Khunt");
return View(result);
}
catch (TimeoutException)
{
if (channelFactory != null)
channelFactory.Abort();
throw;
}
catch (FaultException)
{
if (channelFactory != null)
channelFactory.Abort();
throw;
}
catch (CommunicationException)
{
if (channelFactory != null)
channelFactory.Abort();
throw;
}
catch (Exception)
{
if (channelFactory != null)
channelFactory.Abort();
throw;
}
}