Skip to main content

#AT Repository and Unit of Work Pattern - 7

Presentation project

create a WCF service application by right clicking on solution.
we will get Service1.svc and IService1.svc. rename the service if required.

change the web config.

<endpointBehaviors>
        <behavior>
          <webHttp />
        </behavior>
      </endpointBehaviors>

<services>
      <service name="ContactMangWCF.ContactManagerService" behaviorConfiguration="myBehaviour">
        <endpoint address="" binding="webHttpBinding" contract="ContactMangWCF.IContactManagerService"></endpoint>
      </service>
    </services>

<serviceBehaviors>
        <behavior name="myBehaviour">
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
---------------------------------------------------------------------------------------
 [ServiceContract]
    public interface IContactManagerService
    {
        [WebInvoke(Method = "GET",
                    ResponseFormat = WebMessageFormat.Json,
                    UriTemplate = "Users")]
        [OperationContract]
        List<DTO.LoginUser> GetUsers();

        [WebInvoke(Method = "GET",
                   ResponseFormat = WebMessageFormat.Json,
                   UriTemplate = "UserById/{userId}")]
        [OperationContract]
        DTO.LoginUser GetUserById(string userId);

        [WebInvoke(Method = "GET",
                   ResponseFormat = WebMessageFormat.Json,
                   UriTemplate = "User/{userName}")]
        [OperationContract]
        DTO.LoginUser GetUserByName(string userName);

        [WebInvoke(Method = "GET",
                   ResponseFormat = WebMessageFormat.Json,
                   UriTemplate = "Contacts/{userId}")]
        [OperationContract]
        DTO.UserContacts GetContacts(string userId);

        [WebInvoke(Method = "POST",
                    ResponseFormat = WebMessageFormat.Json,
                    UriTemplate = "Register")]
        [OperationContract]
        DTO.LoginUser RegisterLoginUser(DTO.LoginUser lu);
   
    }

----------------------------------------------------------------------------------------
 public class ContactManagerService : IContactManagerService
    {
        #region Contacts
        public DTO.UserContacts GetContacts(string userId)
        {
            ContactService contactService = new ContactService();
            return contactService.GetContacts(Convert.ToInt64(userId));
        }
        #endregion

        #region User
        public List<DTO.LoginUser> GetUsers()
        {
            UserService us = new UserService();
            return us.GetAllLoginUsers();
        }
        public DTO.LoginUser GetUserById(string userId)
        {
            UserService us = new UserService();
            return us.GetLoginUser(Convert.ToInt64(userId));
        }

        public DTO.LoginUser GetUserByName(string userName)
        {
            UserService us = new UserService();
            return us.GetLoginUser(userName);
        }
   
        #endregion
        #region Register
        public DTO.LoginUser RegisterLoginUser(DTO.LoginUser lu)
        {
            UserService us = new UserService();
            var a = us.RegisterLoginUser(lu);
            return a;
        }
        #endregion
    }
------------------------------------------------
set this project as start up project. run

http://localhost:35848/ContactManagerService.svc/users
output :  [{"Id":1,"Name":"AAA","Password":"aaa","Status":"A"},{"Id":2,"Name":"BBB","Password":"bbb","Status":"A"},{"Id":3,"Name":"CCC","Password":"ccc","Status":"A"},{"Id":4,"Name":"DDD","Password":"ddd","Status":"A"},{"Id":5,"Name":"FFF","Password":"fff","Status":"A"},{"Id":6,"Name":"KKK","Password":"kkk","Status":"A"}]

http://localhost:35848/ContactManagerService.svc/UserById/6
output : {"Id":6,"Name":"KKK","Password":"kkk","Status":"A"}

post methods need to be checked using Postman or through application...

----------------------------------------- End -----------------------------------------------------

Comments

Popular posts from this blog

Use Log4Net in C# windows form Application

we are going to learn on how to use the Log4Net library for creating logs. Create a new windows form application in VS. Install Log4Net library Add to AssemblyInfo.cs  Configure in  App.config Use in code  Install Log4Net library Then install the Log4Net library from the Nuget Package library.           log4net by The Apache software foundation 2.0.8 (i installed the latest). Add to AssemblyInfo.cs  After installing this package, open up AssemblyInfo .cs file under the Properties folder and add the log4net assembly information into it (under the other assembly information.).    [assembly: log4net.Config.XmlConfigurator(Watch= true )]  Configure in  App.config Now, open the App.config file and enter required details for LogNet to work. <configSections>       <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net, Version=1.2....

#EF : DbEntityValidationException - How can I easily tell what caused the error?

While calling  SaveChanges  on my  DbContext , I get the following exception: System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. This is all fine and dandy, but I don't want to attach a debugger every time this exception occurs. More over, in production environments I cannot easily attach a debugger so I have to go to great lengths to reproduce these errors. How can I see the details hidden within the  DbEntityValidationException ? Answer :  The easiest solution is to override SaveChanges on your entities class. You can catch the DbEntityValidationException, unwrap the actual errors and create a new DbEntityValidationException with the improved message. Create a partial class next to your SomethingSomething.Context.cs file. Use the code at the bottom of this post. That's it. Your implementation will automatically use the overriden Save...

#MVC : Why does Html.Label() not work with periods? or Why is @Html.Label() removing some characters

You are misusing the  Html.Label  method. It is for: Returns an HTML label element and the  property name of the property  that is represented by the specified expression. That's why it gets confused if you have a point  .  in the first parameter because it expects a property expression there. However, you can use the second overload: @Html . Label ( "" , String . Format ( "{0}. someText" , 1 )) Or just write out the HTML: <label> @String . Format ( "{0}. someText" , 1 )</ label > or <label class="WelcomeText" style="float: left">Welcome @Html.Label("", Model.USERID + " ( " + Model.ROLE + " )", new { @class = "WelcomeText" })</label>