Tuesday, 8 April 2008

WCF Hello World Part -2

In my previous post, I have created a WCF service. In this part, I will create a client to consume the WCF Service created. Since this is an hello world example, Let's create a web method in WCF service. Todo that, create a method first on interface and then implement it in the service as shown in code.


[ServiceContract]
public interface IHelloWorldService
{

[OperationContract]
string GetData(int value);

[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);

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

public class HelloWorldService : IHelloWorldService
{
...

public string GetGreetings(string name)
{
return string.Format("Hello {0}, Welcome to WCF World!", name);
}

....
}



Now, lets create a client to consume this service. I prefer to create a web client. In order to do this, one needs a service proxy, which will communicate to the service using Soap as transport mechanism. There are 2 ways to create a proxy.
1. by using Visual Studio
2. by using svcutil.exe utility by WCF framework.


for this example, i will simply use Visual Studio as we used to do earlier versions of .net. Add a new website by name WCFHelloWorld-Web and add a service reference and point to the WCF service created as show shown fig below.







Add namespace of WCF Service to codebehind of aspx file, in this case ServiceReference1 is the namespace. using this namespace, create a proxy object as class variable in the aspx.cs file and in aspx file create some server controls in order to type some input and display as label. I have created UI as follows:



and in button event handler, pass the textbox input to webservice's GetGreetings method and display on UI with label as shown below:


using ServiceReference1;

public partial class _Default : System.Web.UI.Page
{
HelloWorldServiceClient proxy = new HelloWorldServiceClient();

protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnSumbit_Click(object sender, EventArgs e)
{
lblGreetings.Text = proxy.GetGreetings(txtName.Text);
}
}



Hit F5 to see the aspx page in browser. Input some name and hit Get Greetings button, you should be able to see display output as shown in fig below:





that's it.. simple to create a web cilent and consume a WCF service. I will take you in depth in next part of this series, until then bye.

Thursday, 3 April 2008

WCF Hello World Part -1

This is my first posting on WCF, i thought I would start with Hello World on WCF and i dont know to what extent WCF will take me!. For a reader who finds this posting, this is would be a head start on WCF and am gonna discuss challenges and issues on WCF in part by part.

so, here i am.

first of all I would like to brief What tools i will be using at the moment.

1. Visual Studio 2008 for this posting.

I may wish to revisit this part again for my next posting.

Ok then. Lets fire up Visual Studio 2008 and create WCF Service by name WCFHelloWorld as shown in fig below:



So When i have created, Visual Studio creates some files for me. those are

1. Service1.svc
2. IService1.cs
3. Web.Config
4. App_Code folder with web project settings and references.

What might interests is 2 files 1 and 2 from above. In eariler version of .Net, you would have created Web Services and seen asmx files instead.

now, I am gonna rename those files to fit into my demonstration as WCFHelloWorld.

So, I rename Service1.svc as WCFHelloWorld.svc and IService1.cs as IWCFHelloWorld.cs.

I leave it with you, how you rename the files and organise and make it compilable at least for now. finally i get my compilable as shown below:





Note: Keep in mind that WCF is a runtime for Web Services. In earlier versions of .Net, Asp.Net was runtime for Web Services. now WCF is runtime for Web Services and .Net remoting!. Those who have worked on .Net Remoting, here is a good news, now they can enjoy exploring separate runtime!.


When you build is succeeded, press F5 and opens in browser as shown below:



At this moment, I have created web service and launched it. In next part of this demo, will create a client and consume this web service.

I have tried to make it simple, hence have not been able to delve in detail but taking this as an example, in some part later of this demo will go in depth, until then bye...

----------------end of part -1 ------------

Postings coming soon! on .Net 3.5 Framework concepts

Its been a while I haven't posted anything on my blog after started working at Cambridge Assessment. Though i had so many things in my mind, could not post even a post!. Today, i am making an effort to put as a start up and have decided to put some articles on 3.5 Framework's WCF, ASP.Net MVC, Linq and Sql Server 2008. I hope i can maintain a consistency in posting at least one everyday!.

Wish me good luck!