|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Services
Chapters
Feature Zones
|
IntroductionThis article shows you how easy it is to setup a web service using WCF (Windows Communication Framework). Using the source I've provided as a template, you can quickly create your own web services. This article does not deal with a client consuming the web service (you will have to wait until part 2). BackgroundI've been playing around with WCF for a while, since the early CTP's. I found it difficult to find good samples/examples. Either they didn't work or I was either too lazy or too stupid to get them working. All I wanted was something that worked with very little effort (or understanding) on my part. I just wanted something I could install and get running!!! Of course at some point everyone will have to understand the ABC's (but that can wait for another day). When you start to learn a new technology, especially a Beta or CPT - you just want it to work, figuring out how it works can wait for another day..... So I put together a simple WCF Web service, that you can just download and get running in a few minutes (for lazy developers - like myself!) Getting StartedYou are going to need Visual Studio 2005 (it might work with other versions of Visual Studio, but I've not tested in and I'm not going to!!), and .NET 3.0 (I would get the entire package from here instead). Then just download the source from above. Using the codeDownload the example code and open up the solution in Visual Studio 2005. The are two projects, the Web service and the Implementation of the class. [Fig 1]
Fig 1 - showing the 2 projects I have chosen to use the dev web server that is built into Visual Studio, it's just easier, less setup and mucking around. But there is no reason not to use IIS (if you know how to). When your code goes into production you will be using IIS, but for now I'm going to leave it alone. There are two parts to the web service, the .svc file and the web.config. WCFService.svc <% @ServiceHost Service="WCFSample.EchoImplementation"%>
Web.Config <system.serviceModel>
<services>
<service name="WCFSample.EchoImplementation"
behaviorConfiguration="returnFaults">
<endpoint contract="WCFSample.IEchoContract"
binding="basicHttpBinding"/>
<endpoint contract="IMetadataExchange"
binding="mexHttpBinding" address="mex">
</endpoint>
</service>
</services>
The The WCF Project Template is made up of 3 parts, the The Service ContractThe WCFContract.cs contains the interface for this service. [ServiceContract]
interface IEchoContract
{
[OperationContract]
EchoMessage Echo(EchoMessage Message);
}
The Data ContractThe [DataContract]
public class EchoMessage
{
private string _OutMessage;
private string _ReturnMessage;
[DataMember]
public string OutMessage
{
get { return _OutMessage; }
set { _OutMessage = value; }
}
[DataMember]
public string ReturnMessage
{
get { return _ReturnMessage; }
set { _ReturnMessage = value; }
}
}
The ImplementationThe implementation of the web service is in WCFImplementation.cs class EchoImplementation : IEchoContract
{
public EchoMessage Echo(EchoMessage Message)
{
EchoMessage _returningMessage = new EchoMessage();
_returningMessage.ReturnMessage = Message.OutMessage;
return _returningMessage;
}
}
For this example, I used the Your Go...Now it's your turn. The code sample is a very simple service, but the structure can be copied for other web services, the example can be scaled out so there is a one to many or many to many relation between the .svc files and classes. History
References
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||