Tuesday, October 11, 2011

ASP.NET AutoMapper Application

Models;

public class Customer
    {
        public string Name{ get; set;}
        public string Surname{ get; set;}  
        public int ID{ get; set;}
       
    }

 public class Person
    {
        public string FullName{ get; set;}
        public int Age { get; set; }
    }


Auto Mapper Class;
public class CustomerToPersonConverter:ITypeConverter<Customer,Person>
    {
 
        public Person Convert(ResolutionContext context)
        {
            Person person = new Person();
            Customer customer = context.SourceValue as Customer;
            person.Age = customer.ID + 15;
            person.FullName = customer.Name + " " + customer.Surname;
            return person;
        }
    }




In Page_Load;
 protected void Page_Load(object sender, EventArgs e)
        {
            Customer customer = new Customer()
            {
                ID=1,
                Name="Engin",
                Surname="Dotnet"
            };
            //Set The Map - #1
 Mapper.CreateMap<Customer, Person>().ForMember(per => per.FullName, mus => mus.MapFrom(opt => opt.Name + " " + opt.Surname)).ForMember(per => per.Age, mus => mus.MapFrom(opt => opt.ID + 14));
 
            //Set The Map - #2
Mapper.CreateMap<Customer, Person>().ConvertUsing<CustomerToPersonConverter>();
 
 
            //Set The Map - #3
 Mapper.CreateMap<Customer, Person>().ConvertUsing(cus =>
   {
                    Person per = new Person();
                    per.Age = customer.ID + 15;
                    per.FullName = cus.Name + " " + cus.Surname;
                    return per;
     });
           
 
 
            ///Then Use
            Person person = Mapper.Map<Customer, Person>(customer);
            Response.Write(person.Age + " " + person.FullName);
 
        }

Monday, October 10, 2011

Simple SQLite Application


protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            
            SQLiteConnection conn = new SQLiteConnection("Data Source=" + Server.MapPath("sqlitedb.db") + ";Version=3;");
            SQLiteDataAdapter adap = new SQLiteDataAdapter("select * from table", conn);
            conn.Open();
            DataTable dt = new DataTable();
            adap.Fill(dt);
            grd.DataSource = dt;
            grd.DataBind();
            conn.Close();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }

Change WCF Service From Soap To Rest

In Service.svc change Factory Property;
Factory="System.ServiceModel.Activation.WebServiceHostFactory" 

Then;
Add WebGet Attribute WCF Method

Custom UserName-Password Validator in WCF Service

1-

public class MyCustomValidator : UserNamePasswordValidator
    {
        public override void Validate(string userName, string password)
        {
//get from db or anywhere
            if (userName != "engin" && password != "dotnet")
            {
                throw new SecurityTokenException("Validation failed.");
            }
        }
    } 


2-In WebConfig
 <bindings>
      <basicHttpBinding>
        <binding name="basicBinding" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Basic">transport>
          security>
        binding>
      basicHttpBinding>
    bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="mexBeh">
          <serviceMetadata httpGetEnabled="true" />
          <serviceCredentials>
            <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="WcfServiceLibrary2.MyCustomValidator,WcfServiceLibrary2"/>
          serviceCredentials>
        behavior>
      serviceBehaviors>
    behaviors>
3-Finall write your code;
           WcfUserPassClient svc = new WcfUserPassClient();
            svc.ClientCredentials.UserName.UserName = "engin";
            svc.ClientCredentials.UserName.Password = "dotnet";
            string result = svc.DoWork();

With a single click install or unistall windows service


To Install;
Create a text file and named install.bat. Then type the following code

@ECHO OFF
 
set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v2.0.50727
set PATH=%PATH%;%DOTNETFX2%
 
echo Installing MyService...
echo ---------------------------------------------------
InstallUtil /i "ServicePath.exe"
echo ---------------------------------------------------
echo Done.
pause


To Unistall;

Create a text file and named uninstall.bat. Then type the following code

@ECHO OFF
 
set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v2.0.50727
set PATH=%PATH%;%DOTNETFX2%
 
echo Installing MyService...
echo ---------------------------------------------------
InstallUtil /u "ServicePath.exe"
echo ---------------------------------------------------
echo Done.
pause

ASP.NET Language Problem


 <globalization
           fileEncoding="iso-8859-9"
           requestEncoding="utf-8"
           responseEncoding="utf-8"
           culture="tr-TR"
           uiCulture="tr-TR"  />

Is Secure Connection In ASP.NET


            HttpContext.Current.Request.IsSecureConnection
or
            Request.ServerVariables["SERVER_PROTOCOL"];