Search code examples
c#web-servicesproxyservice-reference

How should I call external wcf web service?


there is external web service added to project with Service Reference. Is it necessary to do some extra manipulation with that, or methods and properties provided by web service is enough? Here is how it's look like:

//Classes from web service
HHserviceClient h = new HHserviceClient();
HHPostPropertyRequest r = new HHPostPropertyRequest
  {
    //Web service properties
    Amenities = c.Amenities,
    ....
    MobileNo = c.MobileNo
  };
  //Method from web service
 h.PostProperty(r);

The problem with that is NullReference exception occurs when HHserviceClient calling, that's why I'm confused - should I call any .Net classes like HttpClient to do some extra job. I never did before any kind of job with external wcf web services and so I'm completely new to this and asking advise.

UPDATE.

      var rq = new HHPostPropertyRequest
                    {
                        Amenities = c.Amenities,
                        Area = c.Area,
                        BathRooms = c.BathRooms,
                        Bedrooms = c.Bedrooms,
                        City = c.City,
                        Company = c.Company,
                        Contact = c.Contact,
                        Country = c.Country,
                        CustomerID = 1000,
                        Description = c.Description,
                        Email = c.Email,
                        LandLineNo = c.LandLineNo,
                        Lattitude = c.Lattitude,
                        Location = c.Location,
                        Longitude = c.Longitude,
                        MobileNo = c.MobileNo
                    };
                BasicHttpBinding myBinding = new BasicHttpBinding();
                EndpointAddress myEndpoint = new EndpointAddress("http://198.38.94.85/hhsvc/hhservice.svc/postproperty"); 
                ChannelFactory<IHHservice> myChannelFactory = new ChannelFactory<IHHservice>(myBinding, myEndpoint);
                IHHservice HHservice = myChannelFactory.CreateChannel();
                var result = HHservice.PostProperty(rq);
                ((IClientChannel)HHservice).Close();
                myChannelFactory.Close();

Solution

  • Can you try approach with Channel factory? It doesn't use generated configuration etc.

                var r = new Object()
                //define binding 
                //assume your binding using basicHttp, change it if you are using something else
                BasicHttpBinding myBinding = new BasicHttpBinding();           
    
                //define endpoint url              
                EndpointAddress myEndpoint = new EndpointAddress("http://localhost:11234/HHservice.svc"); //change to real endpoint 
    
                //Use channle factory instead of generated one
                ChannelFactory<IHHservice> myChannelFactory = new ChannelFactory<IHHservice>(myBinding, myEndpoint); //Change to you WCF interface
                IHHservice HHservice= myChannelFactory.CreateChannel();
    
                //and call it            
                var result = HHservice.PostProperty(r); //input to your method
    
                ((IClientChannel)HHservice).Close();
                myChannelFactory.Close();