Accessing functions of added web service - vb.net

I have created my own WebService in VS 2010. My project is called sampleWebService and inside my project I have successfully added/connected to another WebService this is called practiceService.
sampleWebService has just the basic Hello World auto generated code in it, however, practiceService has Web Method Functions that handle database calls such as getFirstName, getLastName,...
My question isn't how to extract the data really since I know you have to use either JSON or SOAP. I'm just wondering what I have to type into my code to be able to see the functions and methods that are in my added web reference practiceService so I can connect to them.
Or maybe this is accomplished by using JSON or SOAP
Right now my code for my sampleWebService page is just as is:
<ToolboxItem(False)> _
Public Class Service1
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function HelloWorld() As String
Return "Hello World"
End Function
End Class

You can have a look at the proxy class that was generated when you added the web reference. Also when you create the object of practice service it will give you access to all the web methods exposed in that web service.
To access the web method you have to do something like
ServerName.WebServiceName CallWebService =
new ServerName.WebServiceName();
String sGetValue = CallWebService.MethodName();
Label1.Text = sGetValue;

Related

Using a Service Reference in VB.NET

i have added a service ref in my VB.NET application.
i can see the objects in the object browser. i need to log in first to obtain a session ID.
when I try
dim client as new serviceReference1.IGPSBulkData
I get an error
'New cannot be used on an interface'.
IGPSBulkdata is the only option that includes the login function so Im not sure how to make this call
any ideas?
When you add a service reference several classes are being generated from the service' WSDL. Take a closer look at the generated code 🤓 There will be something like GPSBulkDataClient.
This generated client class can be used to communicate with the service.
The error is correct; you cannot instantiate an Interface.
If you open Object Browser in Visual Studio and search for IGPSBulkData you should be presented with a list of classes which implement it. From there you can instantiate your client object (if that's what you need to do with that class).
So if there was a class called GPSBulkDataThing which implements IGPSBulkData your code might resemble:
Dim client as serviceReference1.IGPSBulkData = new GPSBulkDataThing(maybe with some arguments here)

Service Reference not reusing data types client side

This question has been asked many times but I can't find a solution.
I have a WCF service with a function that takes in a customer object. This customer object is in a separate project that both the client and server code reference. When I add the service reference to the client project, I choose the option to reuse data types. However, when I try to call the function on the client side and pass the customer object in, I get this error:
Error 39 Value of type 'Real.Namespace.Customer' cannot be converted to 'Service.Namespace.Customer'.
The "Real.Namespace" is the class it should be using. "Service.Namespace" is the auto generated class created by the service reference. I know this is supposed to work, so there must be some reason why it is unable to find the real class and reuse it.
I've tried this with very simple objects and it still won't work. Any ideas on why the auto generated code can't find the real class and use it?
Edit: I've tried using a very simple object just to see if I could get it to work. So right now I'm just using a test class that looks like this:
Namespace DTO
<DataContract>
Public Class Test
<DataMember>
Public Property Name As String
End Class
End Namespace

Accessing web services uniformly

I have three web services, which were developed by three different vendors and have different URLs; input parameters and output parameters. They are all ASMX web services. They are used to delete records from third party relational databases e.g. I supply a personID and a person is deleted from one system and everything linked to the person. In another system I supply an order ID and everything linked to the order is deleted.
I have several options:
Create a single wrapper class, which is responsible for accessing the web services; supplying common input parameters and accepting common output parameters. This class would have lots of responsibilities.
Create three wrapper classes i.e. one for each web service
Modify the proxies generated by Visual Studio
Which way is best?
I would recommend allowing Visual Studio to automatically generate the appropriate proxy classes. I would then implement a wrapper class for each web service so that all of the wrapper classes could implement the same interface. For instance, you may make a common interface that looks like this:
Public Interface IPersonBusiness
Sub DeletePerson(personId As String)
End Interface
Then, lets say you had two web services. The first, we'll call it WebService1, has a Delete method which takes a person ID followed by the deletion time. The second web service, we'll call it WebService2, has a DeletePeople method which takes an array of person ID's. We could wrap both of these web services using the above common interface, like this:
Public Class WebService1Wrapper
Implements IPersonBusiness
Public Sub New(proxy As WebService1)
_proxy = proxy
End Sub
Private _proxy As WebService1
Public Sub DeletePerson(personId As String) Implements IPersonBusiness.DeletePerson
_proxy.Delete(personId, Date.Now)
End Sub
End Class
Public Class WebService2Wrapper
Implements IPersonBusiness
Public Sub New(proxy As WebService2)
_proxy = proxy
End Sub
Private _proxy As WebService2
Public Sub DeletePerson(personId As String) Implements IPersonBusiness.DeletePerson
_proxy.DeletePeople({personId})
End Sub
End Classs
I would avoid writing your own proxy code unless you really need to. For instance, if you needed to dynamically call any web service based on some external settings which tell you the method name and parameters to pass, or something like that, then it would be worth looking into.
I would also avoid putting all of the logic to call any of the web services into a single wrapper class. Doing so will make the code unnecessarily ugly and confusing, especially if you need to add additional web services in the future.

Silverlight client got NotFound error from WCF

There are a lot of article on this subject, but neither helped me. I am trying to implement service which could be used without "Add Service Ref..." mostly with advice from hhttp://www.netfxharmonics.com/2008/11/Understanding-WCF-Services-in-Silverlight-2 .
I made small project to reproduce problem.
http://hotfile.com/dl/96710945/9991ac3/SilverlightApplication8.zip.html
I tried solution like :
- Handling Faults in Silverlight
- Cross domain policy
etc
All standard checks are done like, service is active and reachable, client succeed to create channel etc.
I am stack whole week with this problem and I can not figure it out.
Every help is appreciate.
Denis,
try to create service in your web host project. Add service there and then you'll have a choice to add it as reference in your silverlight application. Just add a service in SilverlightApplication8.Web. Right click on SilverlightApplication8.Web --> Add new Item --> On left choose Silverlight --> Silverlight-enabled WCF service. And then add reference to your SilverlightApplication8.
I did not go in deeper investigation, but I assume that service implementation class type was not good.
So my factory class looked like:
Public Class TimeServiceFactory
Inherits System.ServiceModel.Activation.ServiceHostFactoryBase
Public Overrides Function CreateServiceHost(ByVal constructorString As String, ByVal baseAddresses() As System.Uri) As System.ServiceModel.ServiceHostBase
Dim host As New ServiceHost(constructorString, baseAddresses)
It needs to be changed in:
Public Class TimeServiceFactory
Inherits System.ServiceModel.Activation.ServiceHostFactoryBase
Public Overrides Function CreateServiceHost(ByVal constructorString As String, ByVal baseAddresses() As System.Uri) As System.ServiceModel.ServiceHostBase
Dim host As New ServiceHost(GetType(TimeService), baseAddresses)
Difference is I did not pass constructorString (which has information of type of service implementation class) , I passed GetType(TimeService) instate, which provided correct type information.

.NET webservice using an instance of a parameter type?

I have a Windows forms project and a Web Service project in my solution, and I'm trying to call the web service and return a customer object as the result. The problem is that when I try to receive the return object, I get an error that it can't convert it. For example, here is the signature for my webservice:
Public Function GetDriverByID(ByVal DriverID As Integer) As Driver
And here is the code I'm using to call it:
Dim d As Driver = mywebserviceinstance.GetDriverByID(1)
But I receive this compile-time error (wsDrivers is the name of the web reference I've added to my form project): "Value of type ProjectNamespace.Common.wsDrivers.Driver cannot be converted to ProjectNamespace.Common.Driver"
This "Common" namespace contains the Driver class, and I'm not sure why the return class from the web service isn't just a generic "Driver", but is instead a "wsDrivers.Driver", and I can't convert it back. Anybody know how I can deal with this type mismatch?
EDIT: Thanks for the explanations - this actually makes it clear what it's doing. However, is there any way that I can force it to use the actual type instead of the proxy (or, rather, is there any way to convert between the "real" instance and the "proxy" instance), or do I have to serialize the properties before I send them over the wire, and then manually de-serialize the return values?
This is actually pretty common. What's happening is that the Web Service has defined in it the definitions of all the types used in the web service. When you add a reference to that web service, it auto-generates a proxy type in a sub namespace of your namespace. That is what is being returned by your web service when you call it.
However, you probably are also referencing the same library that the web service does seperately that contains the same type. That is the type that is expected when you Dim Driver. That's why there is a mismatch.
The web service reference in a VB.NET or C# project can reference any type of web service and is not limited to those provided by ASP.NET. That is why Visual Studio creates proxy classes for each object which can be retrieved from the web service.