google analytics

Tuesday, March 31, 2009

Creating WCF Service

Hello all

Today I will try to demonstrate how to create simple web service through WCF. To get into WCF first we have to know some basic principal of WCF and its "ABC".

Address : the location of the service.

Bind: how to get service.

Contract: service action.

In this tutorial I will not describe details about WCF.My basic focus will be on creating a WCF service through Visual Studio 2008

First open a new WCF project:

In my sample project I have named "TestWCFService" for creating the service.

In the solution explorer you will found IService1.cs interface , Service1.svc file and a code behind file Service1.svc.cs

In my project I have renamed those as IDataService.cs, DataService.svc and DataService.svc.cs accordingly. To change the markup file of DataService.svc right click on the DataService.svc file in solution explorer and press "View Markup" as bellow

and change the Service according to you code as bellow.

<%@ ServiceHost Language="C#" Debug="true" Service="TestWCFService.DataService" CodeBehind="DataService.svc.cs" %>

In the solution my IDataSevice.cs is as bellow:

using System.Runtime.Serialization;
using System.ServiceModel;

namespace TestWCFService
{
    
    [ServiceContract]
    public interface IDataService
    {

        [OperationContract]
        UserInformation GetUserDataFromService();

        [OperationContract]
        string GetUserRestrictionFromService(string userID);
        
        // TODO: Add your service operations here
    }


    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class UserInformation
    {

        [DataMember]
        public string UserName { get; set; }

        [DataMember]
        public string UserLocation { get; set; }

      
        public string UserRestriction(int userID)
        {
            if (userID.Equals(1))
                return "approve";
            else
                return "rejected";
        }
       
    }
}

My DataService.svc.cs file is as bellow :

using System;

namespace TestWCFService
{
    // NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config and in the associated .svc file.
    public class DataService : IDataService
    {
        #region IDataService Members

        public UserInformation GetUserDataFromService()
        {
            return new UserInformation() { UserName = "Tanvir", UserLocation = "Dhaka" };
        }

        public string GetUserRestrictionFromService(string userID)
        {

            UserInformation userInformation = new UserInformation();

            if (userInformation.UserRestriction(Int32.Parse(userID)).Equals("approve"))
                return String.Format("your user id is {0} and you are approved", userID);
            else
                return String.Format("your user id is {0} and you are not approved", userID);

        }

        #endregion
    }
}

And the most important thing is to change in system.servieModel section in Web.config file as bellow :

<system.serviceModel>
        <services>
            <service name="TestWCFService.DataService" behaviorConfiguration="TestWCFService.Service1Behavior">
                <!-- Service Endpoints -->
                <endpoint address="" binding="wsHttpBinding" contract="TestWCFService.IDataService">
                    <!-- 
              Upon deployment, the following identity element should be removed or replaced to reflect the 
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
              automatically.
          -->
                    <identity>
                        <dns value="localhost"/>
                    </identity>
                </endpoint>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
            </service>
        </services>
        <behaviors>
            <serviceBehaviors>
                <behavior name="TestWCFService.Service1Behavior">
                    <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above 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>
    </system.serviceModel>

We have to change the underline and bold section up above according to you code and naming in our solution.

Thanks

That's all for the day.
BYE

User ScrumPad for your Agile based projects.

Wednesday, February 25, 2009

Custom Event in .NET

Hello All,

Today I will try to deliberate my experience on custom event creation in .NET , Actually I was trying to find something like hands on for event creation in .NET application, suddenly I got a quick help from one of my brother Sadique, who helped me out to resolve the thirst.

We are going to create a custom control where we will define our event class and event handler.To do that first take a  user control named InputControl.ascx  . The ascx page is as bellow.

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="InputControl.ascx.cs" Inherits="InputControl" %>
<asp:Label ID="lblLabel" runat="server" Text=""></asp:Label>
    <br />
<asp:TextBox ID="txtTextBox" runat="server"></asp:TextBox>
    <br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" 
    onclick="btnSubmit_Click" />

In that page I put a textbox control to take input , a button control to fire our custom event handler and a label to show our input result through our custom event.

The InputControl.ascx.cs code behind file is as bellow :
using System;

public partial class InputControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public string EventInputBox
    {
        get {
                return this.txtTextBox.Text;
            }
        set {
                this.txtTextBox.Text = value;
            }
    }

    public string EventLabel {
        get {
                 return this.lblLabel.Text;
            }
        set {
                 this.lblLabel.Text = value;
            }
    }

    public event EventHandler<CustomEventArgs> CustomEventSubmit;
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (this.CustomEventSubmit != null)
            this.CustomEventSubmit(this, new CustomEventArgs("your text is:" + EventInputBox));
    }

}

public class CustomEventArgs : EventArgs
{
    public string EventText { set; get; }

    public CustomEventArgs(string eventTextData)
    {
        EventText = eventTextData;

       
        /*
         Perform other event tasks.
         
         */
    }
}

Now I register the control in my Default.aspx page as bellow .

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register src="InputControl.ascx" tagname="InputControl" tagprefix="uc1" %>

<!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 runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <uc1:InputControl ID="icEventBox" runat="server" OnCustomEventSubmit="icEventBox_CustomEventSubmit"  />
    </div>
    </form>
</body>
</html>

In the file above we have to focus on OnCustomEventSubmit="icEventBox_CustomEventSubmit" which is pointing our custom event.

The code behind file Default.aspx.cs is as bellow.

using System;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void icEventBox_CustomEventSubmit(object sender, CustomEventArgs e)
    {
        icEventBox.EventLabel = e.EventText;
    }
}

In the above article I didn't go for much brief discussion , I think the code is sufficient to describe a custom event itself.

That's all for the day.
BYE

User ScrumPad for your Agile based projects.

Monday, December 29, 2008

Namespace in ASP.NET

Commonly Used Types and Namespaces in ASP.NET

System.Web
HttpApplication
HttpCookie
HttpRequest
HttpResponse
HttpRuntime
HttpServerUtility

System.Web.ApplicationServices *NEW
AuthenticationService NEW
ProfileService NEW
RoleService NEW

System.Web.Caching
Cache

System.Web.ClientServices *NEW
ClientFormsIdentity NEW
ClientRolePrincipal NEW
ConnectivityStatus NEW

System.Web.ClientServices.Providers *NEW
ClientFormsAuthenticationMembershipProvider NEW
ClientRoleProvider NEW

System.Web.Compilation
BuildProvider

System.Web.Configuration
WebConfigurationManager *NEW

System.Web.Hosting
ApplicationManager

System.Web.Management
WebBasedEvent

System.Web.Security
FormsAuthentication
FormsIdentity
Membership
Roles

System.Web.SessionState
HttpSessionState

System.Web.UI
Control
MasterPage
Page
ScriptManager NEW
System.Web.UI
UpdatePanel NEW
UpdateProgress NEW
UserControl

System.Web.UI.HtmlControls
HtmlButton
HtmlControl
HtmlForm
HtmlInputControl

System.Web.UI.WebControls
Content
DetailsView
FormView
GridView
LinqDataSource
ListView
LogIn
Menu
ObjectDataSources
TreeView
Wizard

System.Web.UI.WebControls.WebParts
WebPart

*New - New namespaces in .NET framework 3.5

[Source]

That's all for the day.
BYE

User ScrumPad for your Agile based projects.