Create Simple WCF Service

Hello All,

Today, I want to tell you how to Create Simple WCF Service.

To create WCF service right click on project and add new item -> WCF service. It will create .svc file. Remember to declare Factory tag in .svc file. When you creating service if your web.config is read only it will ask to make it writable. It will not write anything in web.config.

Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory"

Now, go to your .cs file. It has created two files one for sampleClassFunction and another is interface file. We can code in both way write in sampleClassFunction or in interface. I prefer to write in class direct. So, i have remove interface from my class. In cs class file add following code

[AspNetCompatibilityRequirements
    (RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceContract(Namespace = "")]
public class sampleClassFunctions
{
    [OperationContract]
    public string GetFirstString(string sample)
    {
        return sample;
    }
}

Now, declare ScriptManager in .aspx.

    
        
            
        
    

To call this service from your code use following code(I am calling it from my js).

function sample()
{
    function getFirstStringCallback(data)
    {
         alert(data);
    }

    sampleClassFunctions.GetFirstString("My First WCF Service", getFirstStringCallback);
}

That’s it. Enjoy your WCF service….

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>