TechBubbles

WCF Service using ASP.NET AJAX Library

More often, the data to shown in an AJAX page is retrieved from the Web server using a Web service, a Windows Communication Foundation (WCF) service. The services that can also return JavaScriptObjectNotation(JSON) are potential candidates for AJAX pages.

This post explains about calling a WCF service using ASP.NET AJAX Library Data View control. The following are the steps to create and call the service using ASP.NET AJAX Library.

1. Creating a AJAX enabled WCF service

2. Loading the required scripts

3. Calling a service using ASP.NET AJAX Data View Control

Read more

Related Posts:

1 comment

Publishing Feed on URI using WCF

Introduction

In My previous article “Creating Web feeds with WCF” , I have explained about building a web feed with WCF. This post explains how to build a simple WCF service that exposes the feed data using both RSS and Atom.

We are using both formats to emphasize the separation in WCF between the data [SyndicationFeed] and the formatting [SyndicationFeedFormatter].We can write the methods to return the Atom10FeedFormatter or the Rss20FeedFormatter.

Read more

Related Posts:

No comments

Creating Web feeds with WCF

Introduction

A feed on the web is data format where users can read content from different web sites by subscribing using client applications. Building and exposing a web feed is not a new idea but in this post we are going to use WCF to build the feeds.WCF allows you to create feeds for both  Atom1 and RSS2 standards.

Building a Feed with WCF

The following are two important features in WCF allows us to build the feeds.

Read more

Related Posts:

1 comment

WCF and REST

Introduction

WCF is the Microsoft framework for building applications that communicate over network using different protocols. WCF allows the developers to build distributed applications using SOAP. WCF also had the ability to expose and consume REST services.This post explains how to build REST service using WCF.

Note: To build REST Service new infrastructure was added to WCF in .NET Framework 3.5 in the System.ServiceModel.Web assembly.

The two new attributes added to this programming model are

  • WebGetAttribute
  • WebInvokeAttribute

You can read REST Overview before building the RESTful service.

Read more

Related Posts:

1 comment

WCF vs ASP.NET Web services

Introduction

In this post I will explain the Difference between ASP.NET web service and programming WCF services like ASP.NET web services. It also discusses how we use the both technologies for developing the web services.

The development of web service with ASP.NET relies on defining data  and relies on the XmlSerializer to transform data to or from a service.

Key issues with XmlSerializer to serialize .NET types to XML 

  • Only Public fields or Properties of .NET types can be translated into XML.
  • Only the classes which implement IEnumerable interface.
  • Classes that implement the IDictionary interface, such as Hash table can not be serialized.

The WCF uses the DataContractAttribute and DataMemeberAttribute to translate .NET FW types in to XML.

[DataContract]
public class Item
{
    [DataMember]
    public string ItemID;
    [DataMember]
    public decimal ItemQuantity;
    [DataMember]
    public decimal ItemPrice;

}

The DataContractAttribute can be applied to the class or a strcture. DataMemberAttribute can be applied to field or a property and theses fields or properties can be either public or private.

Important difference between DataContractSerializer and XMLSerializer.

  • A practical benefit of the design of the DataContractSerializer is better performance over XMLserialization.
  • XMLSerialization does not indicate the which fields or properties of the type are serialized into XML where as DataCotratSerializer Explicitly shows the which fields or properties are serialized into XML.
  • The DataContractSerializer can translate the HashTable into XML.

Developing Service

To develop a service using ASP.NET we must add the WebService attribute to the class and WebMethodAttribute to any of the class methods.

Example

[WebService]
public class Service : System.Web.Services.WebService
{
    [WebMethod]
    public string Test(string strMsg)
    {
       return strMsg;
    }
}

To develop a service in WCF we will write the following code

[ServiceContract]
public interface ITest
{
    [OperationContract]
    string ShowMessage(string strMsg);
}
public class Service : ITest
{
    public string ShowMessage(string strMsg)
    {
        return strMsg;
    }
}

The ServiceContractAttribute specifies that a interface defines a WCF service contract, OperationContract Attribute indicates which of the methods of the interface defines the operations of the service contract.

A class that implements the service contract is referred to as a service type in WCF.

Hosting the Service

ASP.NET web services are compiled into a class library assembly and a service file with an extension .asmx will have the code for the service. The service file is copied into the root of the ASP.NET application and Assembly will be copied to the bin directory. The application is accessible using url of the service file.

WCF Service can be hosted within IIS or WindowsActivationService.

  • Compile the service type into a class library
  • Copy the service file with an extension .SVC into a virtual directory and assembly into bin sub directory of the virtual directory.
  • Copy the web.config file into the virtual directory.

Client Development

Clients for the ASP.NET Web services are generated using the command-line tool WSDL.EXE.

WCF uses the ServiceMetadata tool(svcutil.exe) to generate the client for the service.

Message Representation

The Header of the SOAP Message can be customized in ASP.NET Web service.

WCF provides attributes MessageContractAttribute , MessageHeaderAttribute and MessageBodyMemberAttribute to describe the structure of the SOAP Message.

Service Description

Issuing a HTTP GET Request with query WSDL causes ASP.NET to generate WSDL to describe the service. It returns the WSDL as response to the request.

The generated WSDL can be customized by deriving the class of ServiceDescriptionFormatExtension.

Issuing a Request with the query WSDL for the .svc file generates the WSDL. The WSDL that generated by WCF can customized by using ServiceMetadataBehavior class.

Exception Handling

In ASP.NET Web services, Unhandled exceptions are returned to the client as SOAP faults.

In WCF Services, unhandled exceptions are not returned to clients as SOAP faults. A configuration setting is provided to have the unhandled exceptions returned to clients for the purpose of debugging.

Related Posts:

45 comments

Host the WCF Service in IIS

Introduction

This post explains hosting the WcfEmployee Service on IIS that we previously hosted with in .NET Executable. Hosting the WCF service in IIS provides a robust, efficient and secure features to WCF service.

1. Create a virtual directory that pointing to the Employee Service project in IIS

2. Configuring the service in IIS is identical to the configuration used for hosting the service in .NET Executable. The configuration file looks like in the following window

Web

3. Confirm the Availability of the service by browsing the .svc file in browser.

svc 

4. Run the client application to consume the service.

Related Posts:

No comments

Hosting the WCF Service in .NET Executable

Introduction

This post explains how to host a WCF service that we defined in the previous article WCF sample in VS 2008. Specifically we host the WCF service in .NET console application.

1. Add the Console application project called, Host, to the solution

2. Add System.ServiceModel .NET, System.Configuration Assemblies to the

    Host  Project.

3. Add the WCF Service project to the Host project

Solution

4. Write the following code which needed to provide a host for the WCFEmployee service

Host

5. The above code retrieves the base addresses from the configuration file and the addresses of WCF endpoints are relative to the base addresses.

6. Add the application configuration file named app.config to the Host project

    Modify the content as follows

App

appSettings section provides the address for the HTTP and TCP protocols.

System.serviceModel section for configuring the WCF endpoints. Here we add the address, binding and contract which constitutes an endpoint.

7. Run the Host Project it shows the following window

EmpHost 

8. You can test the host service by clicking the http://localhost:8000/EmployeeService/ and you will see the following window

Empservice

Related Posts:

1 comment

WCF Sample in Visual studio 2008

Introduction

This post explains the procedure to develop the sample WCF Service and WCF Client  in Visual studio 2008. Get an overview on WCF and WCF Terms.

1. Start open  Visual studio 2008, Select new project option from file menu

WCF Start

2. Select the WCF ServiceApplication template from the project templates.

3. WCF Service describes the operations that perform in a service contract.

WCF-Service

4.Declare the service contract in Iservice.cs interface and implement in the above Service class.

WCF-Interface

5. To test this service, you will need to create a client and use it to call the

    service. You can do this using the svcutil.exe tool from the command line with

    the following syntax: Generating a client using svcutil.

  svcutil.exe http://localhost:53391/Service.svc?wsdl

WCFClient

6. The above command generates a code file and configuration file contains the client class. Add the two files to your client application and use the generated client class to call the service.

Generatedclient

7. Client class which call the service looks like

callingservice

8. The generated client configuration file looks like

ClientConfig

9. The service configuration file looks like

service-config

Conclusion

The above post explains shows the standard way to create a service and client and calling the service. The Next posts explains each feature in depth. 

Related Posts:

7 comments

Service Model Metadata Utility Tool

The Service Model Metadata Utility Tool is used to generate service model code from metadata documents.

The following command generates client code from a running service or online  metadata documents.

svcutil http://service/metadataEndpoint

The following command generates client code from local metadata documents.

svcutil *.wsdl *.xsd /language:c#

The following command downloads metadata documents from running services.

svcutil /t:metadata http://service/metadataEndpoint

The following command generates metadata documents for service contracts and associated types in an assembly.

svcutil myAssembly.dll

Related Posts:

2 comments

WCF Terms

The following are the terms used in WCF

Message

A message contains data which consist of body and headers.

Service

A service is a construct which exposes one or more endpoints. Each endpoint exposing one or more service operations.

Endpoint

Endpoint contains a location (an address) that defines where messages can be sent, a specification of communication mechanism(a binding) that describes how messages should be sent.

Address

An address specifies the location where messages are received. It is specified as a URI.

Service operation

A service operation is a procedure defines in a services’s code that implements the functionality for an operation. This operation is exposed to clients as methods on a WCF client.

Hosting Process

A service must be hosted in some process which include IIS, WAS(windows activation service) and windows services.

WCF Client

A WCF client is client-application that exposes the service operations as methods.A WCF client can be automatically generated by using the ServiceModel Metadata Utility Tool (Svcutil.exe) and pointing it at a running service that publishes metadata.

Related Posts:

2 comments

Next Page »