inforakesha

A fine WordPress.com site

WCF Series 1

Leave a comment

1.Create a new WCF projects

2.Add one Interface

 [ServiceContract]
    public  interface IMyWCFService1
    {
        //this is fir the test WCF service
        [OperationContract]
        int TestMethod(int t);

        [OperationContract]
        List<UserInfo> GetUserInfo(string UserID);

        [OperationContract]
        IQueryable<UserInfo> GetUserInfo1(string UserID);

        [OperationContract]
        IQueryable<UserInfo> GetUserInfoByCompiledQuery(string UserID);
    }

3.Add one class

 [DataContract]
 public    class UserInfo
    {
        [DataMember]
       public String Name { get; set; }
        [DataMember]
       public Double Balance { get; set; }
        [DataMember]
        public DateTime DOB { get; set; }
        [DataMember]
       public String Address { get; set; }
    }

4.Implement that interface

 public  class MyWCFService : IMyWCFService1
    {
        public TZIProdTestEntities db = new TZIProdTestEntities();

        public int TestMethod(int t)
        {
            return t;
        }
        public List<UserInfo> GetUserInfo(string UserID)
        {
            List<UserInfo> UserInfo = (from u in db.UserProfiles
                                       where u.UserId == UserID
                                       select new UserInfo
                                       {
                                           Name = u.FirstName + u.LastName,
                                           Balance = u.CardBalance,
                                           DOB = u.DateOfBirth,
                                           Address = u.Address1
                                       }).ToList();

            return UserInfo;
        }

        public IQueryable<UserInfo> GetUserInfo1(string UserID)
        {
            IQueryable<UserInfo> User = from u in db.UserProfiles
                                        where u.UserId == UserID
                                        select new UserInfo
                                        {
                                            Name = u.FirstName,
                                            Balance = u.CardBalance,
                                            DOB = u.DateOfBirth,
                                            Address = u.Address1
                                        };
            return User;
        }

        public IQueryable<UserInfo> GetUserInfoByCompiledQuery(string UserID)
        {
           var v= MyCompiledQueries.getCustomers(db,UserID);
           return v;
        }

    }
    public static class MyCompiledQueries
    {

        public static Func<TZIProdTestEntities, string, IQueryable<UserInfo>> getCustomers =
            CompiledQuery.Compile((TZIProdTestEntities db, string UserID)
                                 => from u in db.UserProfiles
                                    where u.UserId == UserID
                                    select new UserInfo
                                    {
                                        Name = u.FirstName,
                                        Balance = u.CardBalance,
                                        DOB = u.DateOfBirth,
                                        Address = u.Address1
                                    });
        static TZIProdTestEntities d = new TZIProdTestEntities();
    }


5.Add this configurationin App.COnfig

<service name="WcfServiceLibrary1.Class1">
        <host>
          <baseAddresses> <add baseAddress="http://localhost:8734/WcfServiceLibrary1/Class1/" /></baseAddresses>
        </host>
        <endpoint address="" binding="wsHttpBinding" contract="WcfServiceLibrary1.Interface1">
          <identity> <dns value="localhost" /></identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s