Search This Blog

Thursday, November 14, 2013

Create new WCF Service Application Example

WCF > Create WCF application

Example: use WCF method to return connection string to windows forms application in order to protect discover sensitive data using decompiling software.






1. Create new WCF Service Application

Start New Project > WCF > WCF Service Application



Go to Web.config file and set directoryBrowse to false to avoid directory browsing.

<directoryBrowse enabled="false"/>

2. Add your own methods

Open IService1.cs and add method GetConnString method


     public interface IService1
     {
        [OperationContract]
        string GetData(int value);
        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);
 

        // TODO: Add your service operations here

        [OperationContract]
        string GetConnString(string username);
    }

Open Service1.svc and add method GetConnString
 

 
    public string GetConnString(string username)
    {
            string connstring = "Your connection string set in Wcf Service";
            // check user, etc
            return connstring;
    }
  

Press F5 and than stop  debugging.
   
3. Create new Windows Form  Application in the same solution



Go to Service reference and add (press Discover button!)



Set this project as Start-up project in your solution.

Add the following code:

ServiceReference1.Service1Client sc = new ServiceReference1.Service1Client();
MessageBox.Show(sc.GetConnString("xxx"));

Run your application: