Let’s first establish what the purpose of this code is.
For this article, the purpose of the 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 one better solution also; i.e., by writing our 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)
- On the File menu, click New >> Project.
- In the "New Project" dialog box, under Project types, expand Visual C#, and then click on WCF. Select WCF Service Library and in the Name box, type "ServiceLibrary". In the Solution name, type "WCFDemo" and then click on OK.
- Now, delete the default created Interface and Class perspective "IService1.cs" and "Service1.cs".
- Add a new interface by right clicking on ServiceLibrary(project) > Add > New Item then select "Interface", name it as "IUser.cs". Then, click on Add.
- Set "public" access modifier to interface IPayment.
- public interface IPayment
- {
-
- }
- We need to add [ServiceContract] attribute to interface IPayment.
- [ServiceContract]
- public interface IPayment {
-
- }
- We declared one operation named "ReverseString" in IPayment interface and added [OperationContract] attribute.
- [ServiceContract]
- public interface IPayment {
- [OperationContract]
- string ReverseString(string orignal);
- }
Step 2 - Add a new project to Solution Explorer
- Right click on the Solution Explorer of "WCFDemo" and go to Add > New Project.
- In the "New Project" dialog box, under Project types, expand Visual C#, and then click on WCF. Select "WCF Service Application" and in the Name box, type "WCFService". Click "OK".
- Delete the default created Interface and Service perspective "IService1.cs" and "Service1.svc".
- Add new WCF Service. Right click on WCFService (project) > Add > New Item. Then, select WCF Service on "Add New" dialog box and name it "User.SVC".
- Delete interface IUser.cs from WCFService (project) because we already have added this interface on ServiceLibrary (project).
- Add Reference of "ServiceLibrary" into "WCFService". Right click on Reference of WCFService (project). Then, click on "Add Reference".
- On the "Reference Manager" dialog box, select "Solution" under "Project". Then, check into "ServiceLibrary" and click OK.
- Now, Reference of "ServiceLibrary" project is added to the "WCFService" project. It is time to implement our "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 - Access WCF Service without adding a proxy or Service Reference
It's the final step to access WCF Service without adding a proxy or Service Reference in ASP.NET MVC application.
- Right click on Solution Explorer "WCFDemo" > Add > New Project.
- In the "New Project" dialog box, under Project types, expand Visual C#. Then click Web >> ASP.NET Web Application. Name it as "WebApps" and click OK.
- Now, in the dialog box, click on the "Empty" under the ASP.NET 4.5.2 Templates. Then, check on "MVC" checkbox and click OK.
- Create a Controller by right clicking on "Controller" folder > Add > Controller. Then, select "MVC 5 Controller - Empty" >> Add >> Name it as "HomeController".
- By default, the "Get" Action Method is created as "Index".
-
- public ActionResult Index() {
- return View();
- }
- Add Reference of "ServiceLibrary" into "WebApps". Right click on Reference of WebApps (project).
Click on "Add Reference". On the "Reference Manager" dialog box, select "Solution" under "Project", check into "ServiceLibrary", and then click on OK.
Now, the Reference of "ServiceLibrary" project is added to "WCFService" project.
- Finally, we are 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("*******"); //Enter Your string which you wanted to reverse.
-
- 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;
- }
-
- }
That's it; it's all been done successfully.