Showing posts with label Web Services. Show all posts
Showing posts with label Web Services. Show all posts

Monday, June 23, 2014

Web service with DB interaction (Entity framework 6), Part 1

Lot of people have problems with database interaction with Web services. connection problems with databases , retrieving, inserting etc. In this post I will show you hoe to connect DB and manage connections efficiently with Web service.

To archive this goal entity frame work is very helpful tool.


Lets see how to use it.

Here is my SQL server table that im going to use. Insted of SQL server You can use any Database type such as Oracle, Mysql etc.













Lets Code . :)

1. Create new WCF application in Visual Studio












2. R-Click on Solution and add -> new Item -> ADO.NET Entity data Model






 

 3. then select EF designer from database











 4. Connect your database with New Connection button and select relevant schema that you going to use

5. Then -> NEXT and select Entity framework version.. here im using 6.0 then click NEXT again

 6. Select relevant Tables , Views or stored procedures you need to use and click FINISH
 














 7. Then you can see that it automatically added classes and additional libraries that need to use the database . and also it will have separate classes for each Tables, Stored procedures and Views that you selected in the initialization space. Check  those classes in your project










8. Then create separate function for our database select and add it to interface([OperationContract]). In here I need to output all the data in the Contact table. Entity Framework creates relevant mapped class for my table. I just need to do is use that class as my return type. Like this , My function will return all the contacts because of that I'm using list of that contact objects


IEFDemo.cs:
 public interface IEFDemo
    {
        [OperationContract]
        string DoWork();


        [OperationContract]
        List<Contact> myContacts();
    }


EFDemo.svc.cs:
 public List<Contact> myContacts()
        {

        }

9. Then we need to create instance of database connection and initialize the instance. than open the connection using that instance and get the data from database then close the connection. We are done
Here is full code.


 public class EFDemo : IEFDemo
    {
        //instance of database entities 
        private demoEntities DB;
        public string DoWork()
        {
            return "hello";
        }

        public List<Contact> myContacts()
        {
            DB = new demoEntities();

            //database connection open and close
            DB.Database.Connection.Open();

            //add all items in DB to items variable
            var items = DB.Contacts;
            DB.Database.Connection.Close();

            return items.ToList();
        }
    }

Run it and test ...

Here is my results,














Enjoy.







Full Code In Media Fire 
http://bit.ly/1qDPrBF

 

Monday, March 10, 2014

Making HTTP Post Request

Here is the correct way to send POST using HTTP Client

  var client = new HttpClient();
            var postData = new List<KeyValuePair<string, string>>();
            postData.Add(new KeyValuePair<string, string>("param1", "value"));
            postData.Add(new KeyValuePair<string, string>("param2", "value"));

            HttpContent content = new FormUrlEncodedContent(postData);

            var response = await client.PostAsync("Post URL", content);
        

Monday, February 17, 2014

Web Services What and Why ??




A Web Service is a software program that uses XML to exchange information with other software via common internet protocols. In a simple sense, Web Services are a way for interacting with objects over the Internet.





Why?
  • Language Independent.
  • Protocol Independent.
  • Platform Independent.
  • It assumes a stateless service architecture.
  • Scalable (e.g. multiplying two numbers together to an entire customer-relationship management system).
  • Programmable (encapsulates a task).
  • Based on XML (open, text-based standard).
  • Self-describing (metadata for access and use).
  • Discoverable (search and locate in registries)

Web Service Technologies
  • XML - Describes only data. So, any application that understands XML-regardless of the application's programming language or platform-has the ability to format XML in a variety of ways (well-formed or valid).
  • SOAP - Provides a communication mechanism between services and applications.
  • WSDL - Offers a uniform method of describing web services to other programs.
  • UDDI - Enables the creation of searchable Web services registries.



Friday, February 14, 2014

Consume .asmx Service References Using Windows Strore Apps

Please note that this is not database related post. I'm recently working on windows store app development. In one scenario I have been pushed to consume .asmx web services. At that time there were no other resources to follow for me. I found this methods using some unorthodox way. In last few days several of individuals ask about consuming web services in Store apps specially .asmx.

Here we go,

1. Add service reference to the project. (Service1)

2. Create   Service1SoapClient object for consume service







3.  Assume that your imported Service1 contains method that returns string and method is GetServiceData(). Therefore you need to create object called GetServiceDataResponse nameOfResult  

nameOfResult variable gets response asynchronously  from web service






 4.  Then you can get the response body (actual response we need) fro called nameOfResul like this.

 



5. Then you can use this string as you wish. This string is the actual string that return by GetServiceData()

Enjoy..... :)