Callling a normal web service in WP7

2020-05-07 15:03发布

问题:

I have normal .NET web service(not a WCF service).

I have added this service to my WP7 Project using Service Reference because we don't have . Normally we will add the WCF services using Add Service Reference option but here i added normal Web Service using Add Service Reference option.

For example I have Service like this:

public class Service1
    {
        //local class variable
        public MsgHeader msh;

        //I have two functions like below:
        [WebMethod]
        public int Fun1()
        {
            return 1;
        }
        [WebMethod]
        public int Fun2()
        {
            // Here i am checking msh(MsgHeader) values with the database. 
            //If this information is  not correct i am not proceeding further.
            // some calculations
            return result; //returning some results
        }
    }

I am calling the methods like this in WP7:

Class TestModel
{
     public void TestFun1()
        {
           RS.Service1SoapClient objRS = new RS.RSService1SoapClient();
            objRS.Fun1Completed += new EventHandler<RS.Fun1CompletedEventArgs>(objRS_Completed);
            objRS.Fun1Async();
        }

    private void objRS_Completed(object sender, EventCompletedEventArgs e)
        {
             string str = e.Result;
             responseEventArgs = new ResponseEventArgs();
                responseEventArgs.response = e.Result;                
                System.Windows.Deployment.Current.Dispatcher.BeginInvoke(responseEventHandler, responseEventArgs);
        } 
}

Here i am able to get the Result Successfully for Fun1. But i have a one more function(Fun2) in same service which is using Service class variables(like variable msh in Fun2). When i add the service reference, i am getting my Service class with the name as Service1SoapClient(as shown in TestModel in TestFun1 function) and i have created the object for that class (in wp7) in TestFun1() function.This object(ServiceSoap1Client) does not have the variable called msh, but Service1SoapClient class has the function Fun2Async() and event Fun2Completed.

I have added same service using Add Web Reference in visual studio 2010.

Here i am getting my Service class with same name and i have created object for that Class, here i am able get that variable msh But same variable i am not able get in WP7.

Code For Web Reference in visual studio 2010

Private void Test()
{
   SR.Service1 objS=new SR.Service1();
   SR.MsgHeader msh=new SR.MsgHeader();
   msh.Name="test";
   // I have given some more values to msh
   objS.msh=msh;
  int result= objS.Fun2();
}

My questions are:

1) I have added normal web service using Service Reference because we don't have add web reference in visual studio 2010 express for windows phone. Is it Right?

2)I have added normal web service using Service Reference, If it is a right way how can i get that variable msh?

3)I have added normal web service using Service Reference, If it is not a right way how can i call the normal web service in WP7?

Please help me. Thanks in advance.

回答1:

I think you are confusing the web service class(es) that run on the server, with the proxy class that is generated by the 'Add Service Reference' dialog in Visual Studio.

The proxy class exposes methods that provide a way to call your [WebMethod] methods on the web service. It doesn't do the same for fields (even public ones).

You wouldn't want to do this anyway as it would cause big scaling problems - what if two different clients both changed the value of 'msh'?

You would be better off passing whatever state you require to each service method.