I understand that webservices are stateless. I want to know if there is any way we can expose the public properties (getters and setters) of a webservice on the client side (client side being a vb consumer not javascript)?
Web services are method-based, so they're not designed to access properties.
But there's no reason you couldn't make GetX/SetX methods which are exposed like regular service methods - just make sure you include the [WebMethod] attribute.
As others have suggested, you will need to use get/set methods instead of properties.
As for accessing the web service from JavaScript, just specify the method name in the URL and do an XmlHttpRequest.
The only thing you can "expose" from a web service are the [WebMethod].
You might access your web service with code like the following:
Dim svc as New WebReference.MyWebService()
Dim result As Integer = svc.GetSomeInteger()
svc.SetSomeInteger(result)
Dim result2 As Integer = svc.GetSomeInteger()
You may think that you have created an instance of the web service class. You have not. You have only created an instance of the proxy class in your VB.NET code. In the above code, each call to the web service goes through the same client proxy instance, but will go to a different instance of the server-side web service class.
Even if the web service had properties, or just fields, since you would have a different instance of the web service for each call, you would have a different version of "SomeInteger" each time.
Related
We have to call a web service hosted by our client. We were able to add a web reference to our ASP.Net web application and use the web service. The client just sent us a text file and said we need to pass this as a cookie to get access to the web service. I ask for their help and they sent me this.
SoapHttpClientProtocol clientProxy = new T();
clientProxy.CookieContainer.Add(uri, cookie);
Is there a way to do this using a web reference? Or do I hav eto make a soap call?
The web reference you have generated should be derived from System.Web.Services.Protocols.SoapHttpClientProtocol (for details see this link). The ancestors of this class also provide a property named CookieContainer so that you can use the following code:
webRefInstance.CookieContainer.Add(uri, cookie);
I have created a WFC RIA Service based on ASP.Net Website and adding the nuget packages for RIA service. I have also created a Service named "FactoryService" by extending DomainService class.
I have tested the service by creating a GridView with DomainDataSource pointing to the service. The service is working.
Now I want to access the service from other clients as I have enabled SOAP endpoint. But I cannot find the service's url to the svc file. I need this url to add service reference to my other projects. How do I find the service url?
I have tried the following urls and all returns 404. (namespace "WebApplication3", DomainService class "FactoryService").
- http://localhost:15066/WebApplication3-FactoryService.svc
- http://localhost:15066/services/WebApplication3-FactoryService.svc
- http://localhost:15066/ClientBin/WebApplication3-FactoryService.svc
- http://localhost:15066/FactoryService.svc
- http://localhost:15066/services/FactoryService.svc
- http://localhost:15066/ClientBin/FactoryService.svc
I have found the problem. In the DomainService class, I missed to annotate it with [EnableClientAccess()].
A domain service class must be marked with the
EnableClientAccessAttribute attribute to make the service available to
the client project. The EnableClientAccessAttribute attribute is
automatically applied to a domain service when you select the Enable
client access check box in the Add New Domain Service Class dialog
box.
As I'm using VS2013, the wizard is not available and missed to annotate it with the attribute.
Normally it has the following form
Base-Address + ClientBin + FullName of DomainService (Namespace+TypeName separated by -)
So in your case it should look like
http://localhost:15066/ClientBin/WebApplication3-FactoryService.svc
When you access this link in a Browser you will be provided a page that looks similar to this
Service
You have created a service.
To test this service, you will need to create a client and use it to call the service. You can do this using the svcutil.exe tool from the command line with the following syntax:
svcutil.exe http://localhost:15066/ClientBin/WebApplication3-FactoryService.svc?wsdl
You can also access the service description as a single file:
http://localhost:15066/ClientBin/WebApplication3-FactoryService.svc?singleWsdl
This will generate a configuration file and a code file that contains the client class. Add the two files to your client application and use the generated client class to call the Service. For example:
C#
class Test
{
static void Main()
{
HelloClient client = new HelloClient();
// Use the 'client' variable to call operations on the service.
// Always close the client.
client.Close();
}
}
Visual Basic
Class Test
Shared Sub Main()
Dim client As HelloClient = New HelloClient()
' Use the 'client' variable to call operations on the service.
' Always close the client.
client.Close()
End Sub
End Class
I'm a little bit confused, I want a WCF service that takes values and returns them as XML, to be bound to an ASP.NET project. So I guess I should create WCF Service Library instead of Application, then bind it to ASP.NET via "Add Service Reference"? And, if I got it correctly, that allows me to use service without any proxy classes?
P.S. Service method code that returns XML is something like
[System.ServiceModel.OperationContract]
[System.ServiceModel.Web.WebGet(
UriTemplate = "men",
ResponseFormat = System.ServiceModel.Web.WebMessageFormat.Xml)]
Human[] getAll();
getAll() just generates an array of objects of Human class.
Actually, you've got it partially correct, and partially sideways.
Yes, use a WCF Service Library to create the service.
No, don't use "Add Service Reference".
Instead, add the WCF Service Library as a normal project reference to the ASP.NET web application. Then create a "YourService.svc" file as the endpoint for the service.
<% #ServiceHost Service="MyNamespace.MyServiceImplementationTypeName" %>
See "Deploying an Internet Information Services-Hosted WCF Service".
You will also need some configuration in web.config. I don't know the best way to do this.
I got .asmx a web service on my app. I need to call a method from an other app to get statistics from my app. I need it to return XML. the call to the webmethod is done with javascript soap.
EDIT
I got the web service working. I can execute code and return a string but it stops there. When I try to pass parameters into the method it wont work and when I try to return a string[] or any other type it wont work either. any ideas? Is there something I need to do passing in parameters?
I think you could do two things.
One: enable [ScriptService] attribute on web method. This allows you to call the webservice
with javascript.
Example
Two: enable http-post/http-get webservice calls
How to enable
I am using a web service which sets the Thread.CurrentPrincipal object while logging in and soon later when another webmethod of the same web service accesses Thread.CurrentPrincipal, its different/resets
Can someone tell me if this is expected or can different webmethod calls from the same client can access the same Thread.CurrentPrincipal object
Thanks
As soon as you stop using a thread it goes back into the thread pool.
The next call will take a thread from the thread pool, but you have no control which one you get.
You need to send information about which user is making the call, with each request.
This is expected, every new web request is actually new thread. And every web request reset stuff like CurrentThread, CurrentCulture and so on.
What are you trying to do is authentication session. There are many possible solutions. But to suggest something I have to specify technology you use.
For example, ASP.NET ASMX Services can use Forms Authentication. Also they are aware about ASP.NET Session.
With WCF, you can enable ASP.NET support, so you will have same stuff like for ASP.NET ASMX Services. But you also can leverage on Windows Communication Foundation Authentication Service.
Anyways need more info from you.
If you are using the built-in ASP .NET authentication for your website and then just calling the web service from a web page, you may be able to enable session variables and user context information in the methods of the web service with a decoration. Like this:
[WebMethod(EnableSession=true)]
public void MyWebMethod()
{
string mySessionVar = HttpContext.Current.Session["sessionVar"].ToString();
IPrincipal currentUser = HttpContext.Current.User;
...
}
If that doesn't solve your problem, tell us what are you using the Thread.CurrentPrincipal object for (what you are pulling out of the Thread.CurrentPrincipal object). Perhaps there is another solution.