This will require 4 main components: MyService.svc, MyService.svc.cs, IMyService.cs, Web.config. For me, the Web.config endpoint configuration is the only component that is not straight forward. If you want to view the markup of the MyService.svc file, then: Solution Explorer -> right click MyService.svc -> View Markup.

Lets get started creating the project.

In Visual Studio: FILE -> New -> Project -> WCF Service Application. I entered ‘MyNamespace’ in the ‘Name:’ textbox and clicked the ‘OK’ button.

In the Solution Explorer area, right click IService.cs and rename it ‘IMyService’. Choose ‘Yes’ if the Visual Studio message box promps you to change all references. If you are not prompted, then open IService.cs, right click the ‘IService’ in ‘public interface Iservice’ then: Refactor -> Rename. Then enter ‘IMyService’ in the ‘New name:’ textbox and click the ‘OK’ button.

In the Solution Explorer area, right click Service1.svc and rename it ‘MyService’. Choose ‘Yes’ if the Visual Studio message box promps you to change all references. If you are not prompted, then open Service1.cs, right click the ‘Service1’ in ‘public class service1 : IMyService’ then: Refactor -> Rename. Then enter ‘MyService’ in the ‘New name:’ textbox and click the ‘OK’ button.

Open Web.config. Add the following code directly below ‘<behaviors>’:

<endpointBehaviors>
<behavior name=”webHttp”>
<webHttp/>
</behavior>
</endpointBehaviors>

Add the following code directly below ‘<serviceBehaviors>’:

<behavior name=”myServiceBehavior”>
<serviceMetadata httpGetEnabled=”true”/>
<serviceDebug includeExceptionDetailInFaults=”false”/>
</behavior>

Add the following code directly below ‘<system.serviceModel>’:

<services>
<service name=”MyNamespace.MyService” behaviorConfiguration=”myServiceBehavior”>
<endpoint name=”webHttpBinding” address=”” binding=”webHttpBinding” contract=”MyNamespace.IMyService” behaviorConfiguration=”webHttp”>
</endpoint>
<endpoint name=”mexHttpBinding” address=”mex” binding=”mexHttpBinding” contract=”IMetadataExchange”></endpoint>
</service>
</services>

Open IMyService.cs, delete the existing [OperationContract] and ‘string GetData(int value);’ and the following code in it’s place:

[OperationContract]
[WebInvoke(Method = “GET”, UriTemplate = “GetData/{value}”)]
string GetData(string value);

[OperationContract]
[WebInvoke(Method = “GET”, ResponseFormat = WebMessageFormat.Json, UriTemplate = “GetJsonDataUsingDataContract”)]
CompositeType GetJsonDataUsingDataContract();

Open MyService.svc.cs, delete the existing GetData method and paste the following code in it’s place:

public string GetData(string value){

return string.Format(“You entered: {0}”, value);

}

public CompositeType GetJsonDataUsingDataContract(){

return new CompositeType();

}

We will be running the code next, so you need to know the project url that will be used. Go to Solution Explorer -> right click MyNamespace -> Properties -> Web -> Project Url. Copy the url.

Press Ctrl + F5 to run the web service. Paste the url you copied, followd by ‘MyService.svc/GetData/hello world’, into the a browser and you should see the result that gets returned by the web service. Paste the url you copied, followd by ‘MyService.svc/GetJsonDataUsingDataContract’, into the a browser and you should see the Json result that gets returned by the web service.

Advertisements