简体   繁体   English

我应该如何调用外部WCF Web服务?

[英]How should I call external wcf web service?

there is external web service added to project with Service Reference. 带有“服务参考”的外部Web服务已添加到项目中。 Is it necessary to do some extra manipulation with that, or methods and properties provided by web service is enough? 是否有必要对此进行一些额外的操作,或者Web服务提供的方法和属性就足够了? 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. 问题是当HHserviceClient调用时会发生NullReference异常,这就是为什么我很困惑-我应该调用诸如HttpClient之类的任何.Net类来做一些额外的工作。 I never did before any kind of job with external wcf web services and so I'm completely new to this and asking advise. 我从来没有做过任何关于外部wcf Web服务的工作,因此我对此是全新的,并寻求建议。

UPDATE. 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();

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM