Adding data and exposing it in a WCF Web Service

VBBRett
VBBRett used Ask the Experts™
on
I am new to WCF web services an would like to know, how would I do this in a simple example of a WCF Web Service?  I would like to expose one line of data from a database.  Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior consultant
Commented:
For speed:

Grab an existing application you already contain that connects to the data base and simply wrap it within a wcf service by creating a host solution which will expose what you like then create a service project which simply calls into your existing architecture and returns the values.
see code below for example:

Host project simply add svc file remove the code behind and have the file point to your service layer service object:
<%@ ServiceHost Language="C#" Debug="true" Service="SL.Service"%>

IService I would create a separate project(Contracts) and put this in there and reference it from the service layer:
[ServiceContract(
    Name = "ServiceName",
    Namespace = "http://domain/schema/ServiceName"
  )]
    [WebService]
    public interface IService
    {
        #region Authorisation

        [OperationContract]
        string CallToDB();
    }

Service object:

[ServiceBehavior(
        InstanceContextMode = InstanceContextMode.PerSession,
        Name = "ServiceName",
        Namespace = "http://domain/schema/servicename",
        MaxItemsInObjectGraph = 2147483647, ConcurrencyMode = ConcurrencyMode.Multiple
        )]
    public class Service: IService
    {
public string CallToDB()
        {
            //Call into database assembly return value
            return string;
        }

}

Open in new window


Once created add web reference to consuming application.

I have also attached a screen shot of an SOA solution for your info.

  SOA

Author

Commented:
How about the scripting part on an HTML file?  How would I script everything so that it grabs all the data from the web service and displays it on an html web page?

Author

Commented:
Great, so all I have to do is take any program that I have and wrap it in a WCF Service to make it work?
Grant SpiteriSenior consultant

Commented:
For a fast solution yes

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial