Showing posts with label webservice. Show all posts
Showing posts with label webservice. Show all posts

Thursday, September 15, 2011

WebService - PI - WebService scenario...

Scenario overview:
  • Third party app sending request to PI webservice.
  • PI passing the request to third party webservice.
  • Third party webservice returning response to PI.
  • PI webservice returning response to third party app.

Steps at Enterprise Service Builder
1) Create request and response Data Type for SENDER.

2) Create request and response Message Type for SENDER.

3) Create External Definition and import wsdl file generated from external webservice. By doing so you should have the request and response External Message for RECEIVER.

4) Create outbound Service Interface.
  • Category: Outbound
  • Mode: Synchronous
  • For Request and Response Role, use Type as Message Type and assign Name with request and response message type respectively

5) Create inbound Service Interface.
  • Category: Inbound
  • Mode: Synchronous
  • For Request and Response Role, use Type as External Message and assign Name with request and response external message respectively

6) Create Message Mapping for request.
  • Source: Request Message Type
  • Target: Request External Message

7) Create Message Mapping for response.
  • Source: Response External Message
  • Target: Response Message Type

8) Create Operation Mapping.
  • Source Operation: Outbound Service Interface
  • Target Operation: Inbound Service Interface
  • Request Mapping Program: Request Message Mapping
  • Response Mapping Program: Response Message Mapping

Steps at Integration Builder
1) Create Configuration Scenario.

2) Create Business Component.

3) Add SENDER Communication Channel to Business Component.
  • Adapter Type: SOAP

4) Add RECEIVER Communication Channel to Business Component.
  • Adapter Type: SOAP
  • Target URL: Third party webservice URL
  • SOAP Action: Third party webservice SOAP Action

5) Create Receiver Determination.
  • Sender
    • Communication Component: Business Component
    • Interface: Outbound Service Interface
    • Namespace: namespace defined in ESB

6) Create Interface Determination.
  • Sender
    • Communication Component: Business Component
    • Interface: Outbound Service Interface
    • Namespace: namespace defined in ESB
  • Receiver
    • Communication Component: Business Component
    • Receiver Interfaces:
      • Operation Mapping: Operation Mapping defined in ESB (important)
      • Name: Inbound Service Interface
      • Namespace: namespace defined in ESB

7) Create Sender Agreement.
  • Sender
    • Communication Component: Business Component
    • Interface: Outbound Service Interface
    • Namespace: namespace defined in ESB
    • Sender Communication Channel: SENDER communication channel

8) Create Receiver Agreement.
  • Sender
    • Communication Component: Business Component
  • Receiver
    • Communication Component: Business Component
    • Interfaces: Inbound Service Interface
    • Namespace: namespace defined in ESB
    • Receiver Communication Channel: RECEIVER communication channel

Thursday, May 5, 2011

Validate webservice client via SOAP header...

First, the SOAP header
    namespace SecureWebServiceDemo
    {
        using System;
        using System.Web.Services.Protocols;

        public class MySoapHeader : SoapHeader
        {
            private string _userName;
            private string _password;

            public MySoapHeader()
            {
            }

            public string UserName
            {
                get { return _userName; }
                set { _userName = value; }
            }

            public string Password
            {
                get { return _password; }
                set { _password = value; }
            }
        }
    }
Next, the webservice
    using ...

    namespace SecureWebServiceDemo
    {
        /// 
        /// Summary description for MyWebService
        /// 
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [ToolboxItem(false)]
        public class MyWebService : System.Web.Services.WebService
        {
            public MySoapHeader _header;
            private const string _userName = "testuser";
            private const string _password = "123456";

            [SoapHeader("_header")]
            [WebMethod]
            public string HelloWorld()
            {
                if (_header == null || _header.UserName != _userName || _header.Password != _password) throw new Exception("Invalid User");
                
                return "Hello World";
            }
        }
    }
Finally, the client side
    ...
    
    localhost.MySoapHeader header = new localhost.MySoapHeader();
    header.UserName = "testuser";
    header.Password = "123456";

    localhost.MyWebService test = new localhost.MyWebService();
    test.MySoapHeaderValue = header;
    Console.WriteLine(test.HelloWorld());
    
    ...

Monday, September 6, 2010

Exposing DTO to javascript in ASP.NET...

This post is a continuity from my previous post related to Calling a webservice from javascript in ASP.NET 2.0. Fundamentally it's sharing the same concept. This time I've written the code in ASP.NET 3.5 (Visual Studio 2008). There's no prerequisite required compare to when I did it in ASP.NET 2.0 (Visual Studio 2005).

Once I knew how to call a webservice from javascript in ASP.NET (regardless of version). I've search google to check whether it's possible to pass an object as a parameter to webservice method. Indeed it is possible to do that, but I must make sure that the object should be simple enough without any business logic and with limited behaviour. The object known as Data Transfer Object or in short DTO. Now here's the step by step on how to do it.

1) Using Visual Studio 2008, create new web site. Select ASP.NET Web Site.

2) To create DTO object, add new class under App_Code folder. Lets name it as Student. Paste the code below.
    using System;

///
/// Summary description for Student
///

public class Student
{
public Student()
{
}

public string Id
{
get;
set;
}

public string Name
{
get;
set;
}
}
3) Create new webservice call MyWebService. Add System.Web.Script.Services namespace then add GenerateScriptType and ScriptService attribute to the webservice class. Notice that what I did here is almost the same as my previous post. The only extra code is at line 7 which is to expose Student class to javascript.
    ...
using System.Web.Script.Services;

///
/// Summary description for MyWebService
///

[GenerateScriptType(typeof(Student))]
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class MyWebService : System.Web.Services.WebService
{
...
}
4) Add two methods call GetStudents and TestSubmit. GetStudents method will return a list of students to the caller. Since I want to return it as a JSON object, I've set the ResponseFormat to JSON.
    ...
[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
public List<Student> GetStudents()
{
var students = new List<Student>();

for (int x = 0; x < 10; x++)
{
students.Add(new Student
{
Id = x.ToString(),
Name = "Name" + x.ToString()
});
}

return students;
}

[WebMethod]
public string TestSubmit(Student student)
{
return String.Format("You've submitted Student object with Id: {0} and Name: {1}", student.Id, student.Name);
}
5) Go to Default.aspx, add ScriptManager (again another same step as my previous post in ASP.NET 2.0).
    ...
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference InlineScript="true" Path="~/MyWebService.asmx" />
</Services>
</asp:ScriptManager>
...
6) Now add the html control
    ...
<div>
<input id="btnLoad" type="button" value="Get Students" onclick="javascript:btnLoad_Click();" />

<input id="txtId" type="text" /><input id="txtName" type="text" />
<input id="btnSubmit" type="button" value="Submit" onclick="javascript:btnSubmit_Click();" />

<table id="myTable" border="1" cellpadding="1" cellspacing="1"></table>
</div>
...
7) and the javascript
    ...
<script language="javascript" type="text/javascript">
function btnLoad_Click()
{
try
{
MyWebService.GetStudents(OnGetStudentsComplete);
}
catch(e)
{
alert("error " + e);
}
}

function OnGetStudentsComplete(result)
{
var htmlVal = "";

for(var x=0; x<result.length; x++)
{
htmlVal += "<tr><td>" + result[x].Id + "</td><td>" + result[x].Name + "</td></tr>";
}

$get("myTable").innerHTML = htmlVal;
}

function btnSubmit_Click()
{
try
{
var stu = new Student();
stu.Id = $get("txtId").value;
stu.Name = $get("txtName").value;

MyWebService.TestSubmit(stu, OnTestSubmitComplete);

var htmlVal = $get("myTable").innerHTML;
htmlVal += "<tr><td>" + stu.Id + "</td><td>" + stu.Name + "</td></tr>";
$get("myTable").innerHTML = htmlVal;
}
catch(e)
{
alert(e);
}
}

function OnTestSubmitComplete(result)
{
alert(result);
}
</script>
Now I have a web site that can call a webservice and passing an object as a parameter via javascript.

Wednesday, September 1, 2010

Calling a webservice from javascript in ASP.NET 2.0...

Prerequisite:
In Visual Studio 2005, to make a web project template available, you need to upgrade to SP1. The "other alternative" is to install VS80-KB915364-X86-ENU.exe and WebApplicationProjectSetup.msi. If you choose SP1 than ignore the "alternative" way.

1) For Ajax-Enabled Web Site, install ASP.NET Ajax 1.0.

2) Create new ASP.NET Ajax-Enabled Web Site.

3) Add reference System.Web.Extensions ver 1.0.61025.0.

4) On server side, create new WebService, add System.Web.Script.Services namespace and add ScripService attribute to your webservice class.
    ...
using System.Web.Script.Services;
...

[ScriptService]
public class MyWebService : WebService
{
public MyWebService()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}

[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
5) On client side, create new web form and add ScriptManager to the aspx file that pointing to your webservice (MyWebService.asmx).
    ...
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference InlineScript="true" Path="~/MyWebService.asmx" />
</Services>
</asp:ScriptManager>
...
6) Add an html button and its eventhandler.
    ...
<input id="btnOk" type="button" value="Ok" onclick="javascript:btnOk_Click();" />
...

<script language="javascript" type="text/javascript">
function btnOk_Click()
{
try
{
MyWebService.HelloWorld(OnHelloWorldComplete);
}
catch(e)
{
alert("error " + e);
}
}

function OnHelloWorldComplete(result)
{
alert(result);
}
</script>
Done!!! Your first Ajax-Enabled Web Site.

Here's the full source code:
MyWebService.asmx.cs
    using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;

///
/// Summary description for MyWebService
///

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class MyWebService : WebService
{
public MyWebService()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}

[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
Default.aspx
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<!-- to include webservice proxy, set inlinescript=true, it's much more easier compare to adding script src=webservice.asmx/js -->
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference InlineScript="true" Path="~/MyWebService.asmx" />
</Services>
</asp:ScriptManager>

<div>
<input id="btnOk" type="button" value="Ok" onclick="javascript:btnOk_Click();" />
</div>
</form>
</body>
</html>

<script language="javascript" type="text/javascript">
function btnOk_Click()
{
try
{
MyWebService.HelloWorld(OnHelloWorldComplete);
}
catch(e)
{
alert("error " + e);
}
}

function OnHelloWorldComplete(result)
{
alert(result);
}
</script>