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.
- //Declare Namespace: using ServiceLibrary;
- 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);
- }
- }


pms - Study MaterialPosted Jun 25, 2022, 5:04 AM
The target assembly contains no service types you may need to adjust the code access security policy of this assembly