WCF – Ajax Calling WCF Services

This is will demo a simple example on how Ajax calling WCF Services.

see what does it look like?

Ajax_Service

1. define service data contract

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
    // Define a service contract.
    [ServiceContract(Namespace = "ConfigFreeAjaxService")]
    public interface ICalculator
    {
        [OperationContract]
        double Add(double n1, double n2);
        [OperationContract]
        double Subtract(double n1, double n2);
        [OperationContract]
        double Multiply(double n1, double n2);
        [OperationContract]
        double Divide(double n1, double n2);
    }
 
    public class CalculatorService : ICalculator
    {
 
        public double Add(double n1, double n2)
        {
            return n1 + n2;
        }
 
        public double Subtract(double n1, double n2)
        {
            return n1 - n2;
        }
 
        public double Multiply(double n1, double n2)
        {
            return n1 * n2;
        }
 
        public double Divide(double n1, double n2)
        {
            return n1 / n2;
        }
    }

2. services.svc

1
2
3
4
5
6
<%@ServiceHost 
    language="c#"
    Debug="true"
    Service="Microsoft.Ajax.Samples.CalculatorService"
    Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory" 
%>

3. aspx file to call services hosting by IIS

the following javascript to call WCF data contract class and call methods:

1
2
var proxy = new  ConfigFreeAjaxService.ICalculator();
proxy.Add(parseFloat(n1), parseFloat(n2), onSuccess, onFail, null);

Here is all data.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Config Free AJAX Service Client Page</title>
 
    <script type="text/javascript">
    // <![CDATA[
 
    // This function creates an asynchronous call to the service
    function makeCall(operation){
        var n1 = document.getElementById("num1").value;
        var n2 = document.getElementById("num2").value;
 
        // If user filled out these fields, call the service
        if(n1 && n2){
 
            // Instantiate a service proxy
            var proxy = new  ConfigFreeAjaxService.ICalculator();
 
            // Call correct operation on proxy       
            switch(operation){
                case "Add":
                    proxy.Add(parseFloat(n1), parseFloat(n2), onSuccess, onFail, null);            
                break;
 
                case "Subtract":
                    proxy.Subtract(parseFloat(n1), parseFloat(n2), onSuccess, onFail, null);                        
                break;
 
                case "Multiply":
                    proxy.Multiply(parseFloat(n1), parseFloat(n2), onSuccess, onFail, null);            
                break;
 
                case "Divide":
                    proxy.Divide(parseFloat(n1), parseFloat(n2), onSuccess, onFail, null);            
                break;
            }
        }
    }
 
    // This function is called when the result from the service call is received
    function onSuccess(mathResult){
        document.getElementById("result").value = mathResult;
    }
 
    // This function is called if the service call fails
    function onFail(){
        document.getElementById("result").value = "Error";
    }
    // ]]>
    </script>
</head>
<body>
    <h1>
        Config Free AJAX Service Client Page</h1>
    <p>
        First Number:
        <input type="text" id="num1" /></p>
    <p>
        Second Number:
        <input type="text" id="num2" /></p>
    <input id="btnAdd" type="button" onclick="return makeCall('Add');" value="Add" />
    <input id="btnSubtract" type="button" onclick="return makeCall('Subtract');" value="Subtract" />
    <input id="btnMultiply" type="button" onclick="return makeCall('Multiply');" value="Multiply" />
    <input id="btnDivide" type="button" onclick="return makeCall('Divide');" value="Divide" />
    <p>
        Result:
        <input type="text" id="result" /></p>
    <form id="mathForm" action="" runat="server">
    <asp:ScriptManager ID="ScriptManager" runat="server">
        <Services>
            <asp:ServiceReference Path="service.svc" />
        </Services>
    </asp:ScriptManager>
    </form>
</body>
</html>
Posted in WCF | Comments Off on WCF – Ajax Calling WCF Services

Pet Shop 4.0 – SqlCacheDependency

Pet Shop uses SqlCacheDependency to syn with Database Table.

On all supported versions of SQL Server (Microsoft SQL Server 7.0, Microsoft SQL Server 2000, and SQL Server 2005) the SqlCacheDependency class monitors a specific SQL Server database table. When the table changes, items associated with the table are removed from the Cache, and a new version of the item is added to the Cache.

Posted in PetShop4.0 | Comments Off on Pet Shop 4.0 – SqlCacheDependency

Pet Shop 4.0 – Encrypt WebConfig section and Decrypt WebConfig Section “ConnectionString”

As we know, ConnectionString contains User Name and Password.
for security reason, Connection String needed to be Encrypted.

Here is sample code in Pet Shop.

EncryptWebConfig.bat
@echo off
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -pef “connectionStrings” “C:\Program Files\Microsoft\.NET Pet Shop 4.0\Web”
PAUSE

DecryptWebConfig.bat
@echo off
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -pdf “connectionStrings” “C:\Program Files\Microsoft\.NET Pet Shop 4.0\Web”
PAUSE

run EncryptWebConfig.bat the section will be like following:

1
2
3
4
5
  <connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider">
    <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"
     ....         
    </EncryptedData>
  </connectionStrings>

run DecryptWebConfig.bat web.config will be normally displayed.

Posted in Uncategorized | Comments Off on Pet Shop 4.0 – Encrypt WebConfig section and Decrypt WebConfig Section “ConnectionString”

dbml and xsd

Two example will put here to demonstrate following question
1. how dbml visit database
2. how to update dbml is new columns are added
3. dbml updated when tables are deleted or updated.

Posted in Uncategorized | Comments Off on dbml and xsd

pet shop 4.0 – source code analysis

pet shop 4.0 is very classic important example.
It is available under following url.
http://www.microsoft.com/en-au/download/details.aspx?id=16693

More details will put here to analysis it.

Posted in Uncategorized | Comments Off on pet shop 4.0 – source code analysis

Asp.net OutputCache Example

Output Cache Will determine when page will refresh

the following example shows that date time will be updated every 10 seconds, if client keeps on pressing F5 to refresh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="outputCacheTest.aspx.cs" Inherits="WebApplication.outputCacheTest" %>
<%@ OutputCache duration="10" varybyparam="None" %>
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <%= DateTime.Now.ToString() %><br />
    </div>
    </form>
</body>
</html>
Posted in Uncategorized | Comments Off on Asp.net OutputCache Example