Thursday 8 August 2024

.Net Core with Data First approach

 Code for Create Model in Models folder in Project as same as in Database table.



Scaffold-DbContext "Data Source=DESKTOP-L20DF1I\RAMUDB;Initial Catalog=EventDatabase;Integrated Security=True; user id=sa;Password=********" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models










Wednesday 7 August 2024

.Net Core Image

 


Tuesday 15 December 2015

How to Create Restful Service in WCF


How to create restful service in WCF

Web .config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5">
      <assemblies>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name ="wcfInformation.Service1">

        <endpoint address ="" binding ="webHttpBinding" contract ="wcfInformation.IService1" behaviorConfiguration ="restfulBehavior" ></endpoint>
        <endpoint address ="mex" binding ="mexHttpBinding" contract ="IMetadataExchange"></endpoint>
      </service>
     
     
    </services>
    <standardEndpoints >

      <webHttpEndpoint>
        <standardEndpoint name ="" helpEnabled ="true" automaticFormatSelectionEnabled ="false" maxReceivedMessageSize ="9147483647"></standardEndpoint>
      </webHttpEndpoint>
    </standardEndpoints>
    <bindings>
      <webHttpBinding>
        <binding name="webHttpBinding"   maxBufferSize="2147483647" maxBufferPoolSize="9147483647" maxReceivedMessageSize="9147483647" openTimeout="00:25:00" closeTimeout="00:25:00" sendTimeout="00:25:00" receiveTimeout="00:25:00">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"  />
          <security mode="Transport">
            <transport clientCredentialType="None"/>
          </security>
        </binding>
        <binding name="HttpStreaming" maxReceivedMessageSize="2147483647" transferMode="Streamed"/>
      </webHttpBinding>
    </bindings>
    <behaviors>
      <endpointBehaviors>
        <behavior name="restfulBehavior">
          <webHttp helpEnabled="true"/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false before deployment -->
          <serviceMetadata httpGetEnabled="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>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
      multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true" />
  </system.webServer>
  <connectionStrings>
    <add name="mvcEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=RAMULAPTOP-PC\RAMUSQL;initial catalog=mvc;persist security info=True;user id=sa;password=mysachi;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  </entityFramework>
</configuration>





IServic.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace wcfInformation
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract(Name = "List")]
        [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "List?id={id}")]
        List<student_info> List(int id);

        // TODO: Add your service operations here
    }

    [DataContract]
    public class student_info
    {
        [DataMember(Name = "ID", Order = 1)]
        public int id { get; set; }
        [DataMember(Name = "Student Name", Order = 2)]
        public string name { get; set; }
    }
    // Use a data contract as illustrated in the sample below to add composite types to service operations.


}

Sevice.svc.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace wcfInformation
{
        public class Service1 : IService1
    {
        List<student_info> lststuentInfo = new List<student_info>();
        student_info objst_info;
        student dModel = new student();
        public List<student_info> List(int id)
        {
            using (var ctx = new mvcEntities())
            {
                if (id == 0)
                {
                    var v = ctx.students.ToList();
                    foreach (var vd in v)
                    {
                        objst_info = new student_info();
                        objst_info.id = vd.id;
                        objst_info.name = vd.name;
                        lststuentInfo.Add(objst_info);
                    }
                }
                else
                {
                    var v = ctx.students.Where(x => x.id == id).Select(x => x).ToList();
                    foreach (var vd in v)
                    {
                        objst_info.id = vd.id;
                        objst_info.name = vd.name;
                        lststuentInfo.Add(objst_info);
                    }
                }
                return lststuentInfo;
            }

        }
    }
}