Steps are following for "SERVICE".
Step 1. Open visual studio and click on file menu -> New -> Project
Step 2. Select "Asp.net Empty Web Application" with .Net Framework 4.5 and give Name "SimpleWCF" and checked checkbox on "Create directory for solution." and then click OK.
Step 3. Now, Right click on project and Add New Item "AJAX-enabled WCF Service" and give name "Hello.svc" and click ADD.
Step 4. By default, it will add some code, so first delete all the existing code and make that file as following.
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Activation; using System.ServiceModel.Web; using System.Text; namespace SimpleWCF { public class Hello :IHello { public string getResponse(string name) { return "Hello " + name; } } [ServiceContract] public interface IHello { [OperationContract] string getResponse(string name); } }
Step 5. Open web.config file and make changes like following.
<?xml version="1.0"?> <!-- For more information on how to configure your ASP.NET application, please visit http://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5" /> </system.web> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior> <serviceMetadata httpGetEnabled="true"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>
That's it! Service is ready.
Now, Steps are following for "CLIENT".
Step 1. Right click on the same solution -> Add -> New Project
Step 2. Select "Asp.net empty web application" with .net framework 4.5 and give name "SimpleWCFClient" and click OK.
Step 3. Now, right click on this project and "Add Service Reference".
Step 4. In this dialog box, add Address, in my case, i have "http://localhost:54304/Hello.svc" and click GO.
Step 5. You can see your Service and give name "HelloClientService" and Click OK.
Step 6. It will automatically add all the necessary code in web.config like following.
<!-- For more information on how to configure your ASP.NET application, please visit http://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5" /> </system.web> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="BasicHttpBinding_IHello" /> </basicHttpBinding> </bindings> <client> <endpoint address="http://localhost:54304/Hello.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IHello" contract="HelloClientService.IHello" name="BasicHttpBinding_IHello" /> </client> </system.serviceModel> </configuration>
Step 7. Now right click on project and add New Item "Web Form" and name it "Default.aspx"
Step 8. In code behind of this default.aspx page, write following code.
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace SimpleWCFClient { public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { HelloClientService.HelloClient hc = new HelloClientService.HelloClient(); string result = hc.getResponse("John Miller"); hc.Close(); } } }
Result will be:
Hello John MillerYou can see, it will return response with "Hello" in prefix. This is very simple example and project structure will look like following:
No comments:
Post a Comment