Access Asp.Net WebService from PHP Client

Creating and consuming webservices in asp.net is an easy task with the help of Visual Studio. Although most of the scenarios I worked with were always with asp.net applications consuming asp.net web services, which makes things even easier. This week however I learned something new when I had to get a PHP client application to connect to my asp.net webservices.

One of out clients was called saying that the service was not working. When I tested hisPHP client I noticed the the parameters that he was passing where not getting to my webservice even though the service was being called. The only problem then was passing the parameters. I Googled a little and got my own PHP client running.

For the sake of this post let’s say that our webservice gets an string as a parameters and returns the same value, this will let us test our code. So here is the asp.net webservice:

[WebService(Namespace = "http://tempuri.org/")]
public class Service : System.Web.Services.WebService
{

    [WebMethod]
    public string MyTestMethod(string myParameter)
    {
        return "The value of the parameter is: " + myParameter;
    }

}

As you can see there’s nothing to it.

The PHP code is not much harder once you Google a little. First you have to haveNuSoap Library which has the webservices infrastructure for PHP. You may put it in the same directory of your test page. Then you code a simple client to access our service:

<?php

    require_once('./nusoap.inc');

    $wsdl = "http://mysite/myapp/Service.asmx?wsdl";

    $mynamespace = "http://tempuri.org/";

    $theVariable = array('myParameter'=> 'gabriel');

    $s = new SoapClient($wsdl,true);

    $result = $s->call('MyTestMethod',$theVariable);

    echo "<h1>Resultado:</h1>";

    echo "<pre>"; print_r($result); print "</pre>";
?>

After I had the code I could verify for my self that my client was right. The parameters in the above code are not getting to the webservice. The coding is right, I checked in several sites in the Internet, so where could the problem be?

Well, SOAP specification allows for two formatting options: Style and Use. Style handles the formatting of the Body element of the envelope. Style allows for 2 values:
  1. RPC (Remote Procedure Call) which is the older format;
  2. Document which is a newer format and is VisualStudio default format.

By now I bet you have figured out the solution, right? If you said that PHP default encoding is RPC and asp.net default encoding is Document then you’re right on.

How to solve the problem? Well, you can solve the problem on both ends, it all depends on your choice. First let’s fix it on the asp.net side, it’s really simple. All you have to do is use the SoapRpcMethod attribute on WebMethod, like this:

[WebService(Namespace = "http://tempuri.org/")]
public class Service : System.Web.Services.WebService
{

    [SoapRpcMethod()]
    [WebMethod]
    public string MyTestMethod(string myParameter)
    {
        return "The value of the parameter is: " + myParameter;
    }

}

That’s it, the PHP client starts to work instantly.

This is a good solution if you only had PHP clients, since that was not my case I didn’t want to mess with my webservice and have to make changes to all other asp.net clients. I wanted to solve the problem on the PHP client, so let’s see how we could code the PHP client to fix the problem.

<?php

    require_once('./nusoap.inc');

    $wsdl = "http://mysite/myapp/Service.asmx?wsdl";

    $mynamespace = "http://tempuri.org/";

    $theVariable = array('myParameter'=> 'gabriel');

    $s = new SoapClient($wsdl,true);

    //here is the only change you need to do
    $result = $s->call('MyTestMethod',array('parameters' =>  $theVariable));

    echo "<h1>Resultado:</h1>";

    echo "<pre>"; print_r($result); print "</pre>";
?>

What I had to do in the PHP code was to add an extra array around the array with the original variables. This handles the difference in the Document encoding used by asp.net.

This problem really got on my nerves last week and I’m really glad to have found a solution. As usual I wanted to share this with the world and maybe avoid the same headache to others. Ohh, and I also got to learn a little php on the way…

Happy coding friends!

DataSet Designer (Editor) Missing

his week one of my co-workers had a strange problem with his VisualStudio 2008. All the sudden he couldn’t view the DataSet designer in any applications. When the DataSet file was clicked all that it showed was the XML file, no sign of the Designer.

We don’t know what happened but as I tried to figure out the problem I found that there’s very few information about this problem on the Internet, so I decide to blog about it so that it might help someone in distress.

If you’re having this problem you might first try to right click the DataSet file and then select the Open With… option. A list of editors will show, if you don’t see the DataSet Editor then you you have the same problem we did.

 

I have found a few posts about it, here are the solutions proposed:

  • Run the following command in the VisualStudio prompt: devenv /resetsettings
  • Run the following command in the VisualStudio prompt: devenv /setup
  • With Visual Studio setup disc select the Repair option.
  • Re-install Visual Studio.
All these are valid solutions and I found people on the web who claimed that this helped them but for my unfortunate teammate it didn’t help. The only thing that did it for us was:
  1. Remove VisualStudio
  2. Delete any folder left from VS installation
  3. Install VisualStudio

Finally, problem solved! I know this is not an optimal solution, far from it but at the end of the day it works. Nothing else did. I hope this post helps some else.

Problem with FormView inside UpdatePanel

Today I struggled for a while with a FormView that for no apparent reason wasn’t updating some of my fields to the database. After sometime I noticed that the fields that were not getting inserted into the database where the ones I had wrapped inside an UpdatePanel. Coincidence? I don’t think so…

I did a few tests and noticed that that was the problem indeed. I can’t say exactly what is the problem but it seems like that the FormView doesn’t like to have it’s fields inside an UpdatePanel.

The way I found to get around this issue is to populate the parameters manually in the events of the ObjectDataSource (ODS). So if you’re trying to insert a record you might using the Inserting event of the ODS to populate the problematic paramters:

protected void ObjectDataSource1_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
    {
        e.InputParameters["FirstName"] = ((TextBox)FormView1.FindContro("TextBoxFirstName")).Text;
        e.InputParameters["LastName"] = ((TextBox)FormView1.FindContro("TextBoxLastName")).Text;
        e.InputParameters["City"] = ((TextBox)FormView1.FindContro("TextBoxCity")).Text;
    }

In the Inserting event of the ODS the InputParamters have already been populated (at least the ones outside the UpdatePanel) but the record hasn’t been inserted yet, so you are intercepting the parameters, adjusting it’s values and then letting it continue with the insertion.

I hope this code spares someone to have to go through the tests I had to do.

Passed 70-528 today

Well, today I got one more certification out of the way. I have just arrived from the Prometric test center where I passed the 70-528 (aka Microsoft .NET Framework 2.0 – Web-Based Client Development). Now I’m a Microsoft Certified Technology Specialist (MCTS): .NET Framework 2.0 Web Applications.

Overall I considered the test to be easy compared to the 70-536. If you use the Microsoft certification books and you already have hands on experience you shouldn’t have any trouble passing this exam.

Now I will concentrate in the 70-529 (aka Microsoft .NET Framework 2.0 – Distributed Application Development) exam and I think this will keep me busy for another 2 months or so. My goal is to get as many certifications as I can until next year when I planning to move to Canada. Since I don’t know any one there I figure that the certifications are the most universal way of proving my knowledge.

Thanks to everyone that helped me!