Saturday, May 22, 2010

Calling WCF services via Perl environment

Hello everyone,

Let's see the advantages of Service Oriented Architecture (SOA). Recently, I was a part of development team for mobile application. We wanted to create a mobile version of a web application with extra features. It would had been easy for us to create a different client version if we had followed the SOA.

Windows Communication Foundation (WCF) is the service oriented programming which can be consumed by clients. One of the best feature of WCF services is interoperability.

Let's test this functionality of WCF services by consuming them via Perl environment.

1) Create a WCF service project

2) Add an operation contract.
[OperationContract(Name = "GetInputData")]
string GetInputData(int result);

3) Change the binding from default "wsHttpBinding" to "basicHttpBinding" in web.config. BasicHttpBinding is based on SOAP 1.1 specifications whereas WSHttpBinding is based on SOAP 1.2 specifications.

Perl script:

#!/usr/bin/perl -w
print "Content-type: text/html\n\n";

package main;
use SOAP::Lite;

my $ws_url = 'http://1160/WCFServices/SampleWCFService.svc?wsdl';
my $ws_uri = 'http://tempuri.org/';
my $ws_xmlns = 'http://tempuri.org';

my $soap = SOAP::Lite
-> uri( $ws_uri)
-> on_action(sub{sprintf '%sISampleWCFService/%s', @_})
-> proxy($ws_url);
#WCF service's method name and signature should match mapping wsdl file.
my $response = $soap->GetInputData(SOAP::Data->new(name => 'result', value => 5));

if($response->fault)
{
die $response->faultstring;
}
else
{
print $response->result;
}

References:
http://www.soaplite.com/
http://www.cpan.org/
http://www.perlmonks.org/?node_id=649135

8 comments:

  1. Good Explanation...I have this requirement will try and let you know if I need any help on this.

    ReplyDelete
  2. It doesn't work for me... It gives me a 404 Not Found error when I call on the method in WCF. My program dies when it encounters:

    my $response = $soap->GetWorkOrderList(SOAP::Data->new(name => 'siteId', value => 'BKK'));

    And in C# I have [OperationContract(Name="GetWorkOrderList")]
    List GetWorkOrderList(string siteId);

    Any ideas?

    ReplyDelete
  3. I have the same issue as outlined by PerlLearner.Can you help on the same.I too get 404 Not found error .PerlLearner If you have found any solution then please post the details here.It will be really very helpful.Thanks.
    digital signature Microsoft

    ReplyDelete
    Replies
    1. Hi , I think it so late but will help to solve problem.
      I also faced same problem but after changing web.config of service it works. It requires Message and Transport security modes. then it will work.
      By using this in perl script you can access method from service wsdl.
      #my $xmlns1="http://schemas.datacontract.org/2004/07/sampleWcfService";
      $response1= $soap->GetAddress
      (SOAP::Data->new(name=>'mailAddress',value=>
      [
      SOAP::Data->new (name=>'a:city'),
      SOAP::Data->new (name=>'a:name'),
      SOAP::Data->new (name=>'a:streetaddress'),
      SOAP::Data->new (name=>'a:zipcode')
      ])->attr({'xmlns:a'=>$xmlns}))->result;

      Delete
  4. If service method returns a complex datatype or array then how to read it in perl?

    ReplyDelete
  5. What a pleasant YouTube video it is! Awesome, I loved it, and I am sharing this YouTube record with all my colleagues.
    Photo retouching services

    ReplyDelete
  6. No matter if some one searches for his vital thing, thus he/she desires to be available that in detail, thus that thing is maintained over here.
    Chalkboard

    ReplyDelete