I have just started looking into Windows Communication Foundation.
Pawel Glowacki created a really nice article on the CodeGear Developer Network titled Introduction to WCF Programming in Delphi.
I'm particularly interested in web development and I noticed that there is a WCF Service Template in Visual Studio 2005, so I have converted it to Delphi.
I have uploaded the template to CodeCentral.
If you are not sure how to use the template, I have a blog post about templates and how you install them,
Once installed you can now create a new WCF Service application
You should end up with a new ASP.Net Web Application with a single WCF Service.
Assuming that you have already installed Microsoft dotnet v3.0, you can now build and run the application.
The service description should now appear in a browser.
To exercise the web application you need to do the following:-
1) Create a new VCL.Net application
2) Add a reference to the System.ServiceModel assembly
3) Add
C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0
to the compiler search path
4) Your code might look something like
uses
System.ServiceModel, Service, System.ServiceModel.Channels;
{$R *.nfm}
procedure TForm1.Button1Click(Sender: TObject);
var
aEndpointAddress: EndpointAddress;
aBinding: WSHttpBinding;
aFactory: ChannelFactory<IMyService>;
aChannel: IMyService;
someValue: string;
value: DataContract1;
begin
try
aEndpointAddress := EndpointAddress.Create('http://localhost:8080/WCFServiceWebApplication1/Service.svc');
aBinding := WSHttpBinding.Create;
aFactory := ChannelFactory<IMyService>.Create(aBinding, aEndpointAddress);
aChannel := aFactory.CreateChannel;
try
someValue:=aChannel.MyOperation1('John');
ShowMessage(someValue);
value:= DataContract1.Create;
value.FirstName:='John';
value.LastName:='Moshakis';
aChannel.MyOperation2(value);
ShowMessage(someValue);
finally
IChannel(aChannel).Close;
IDisposable(aChannel).Dispose;
end;
except
on E:Exception do
begin
raise;
end;
end;
end;
A word of warning, if like me you use Cassini as your web server, I think you need to upgrade to the latest version. Below is a screenshot of the application running with the version of Cassini supplied with Delphi 2007.
Please read my blog post about upgrading Cassini.
Comments welcome
I also see that Pawel is doing a presentation at CodeRage II titled "Delphi for .NET WCF Programming: Deeper Dive", I'm looking forward to listening to that,