Include WCF Service as part of an ASP.NET Application - asp.net

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.

Related

Call web service and passing a cookie in an ASP.Net Web Application

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

How to use wcf ServiceBehavior attribute for InstanceContextMode in Web API?

How can I use ServiceBehaviorAttribute in my Web API 2 project to create & recycle instance context object on every request? I tried to set it traditional way by configuring in web.config but didn't work. I think I may need to set custom attribute so any suggestion/thoughts are appreciated!
[ServiceBehaviorAttribute(InstanceContextMode = InstanceContextMode.PerCall)]
I am new to Web API so not having much exposure on it.
The ServiceBehaviorAttribute is in the System.ServiceModel namespace and is used exclusively for WCF not for Web API. The following link seems to address your issue: Programmatically set InstanceContextMode

Can we expose public properties of a webservice on the client side?

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.

ASP.net web services

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.

Is there a way to find out if a web service has security?

I've got a web service:
http://machine001/Services/conversionService.asmx
This web service has one method in it called convert(string pInput).
I am trying to find out if there is a way to figure out, without logging into machine001 and without actually calling the convert method, if this web service has security applied.
If I am able to reach http://machine001/Services/conversionService.asmx, see the service description, create the proxy class and instantiate the web service object from any client does that mean there is no security?
Use your browser and go to:
http://machine001/Services/conversionService.asmx?wsdl
And see if the description contains WSE Security declarations. And to you're last paragraph, yes if you can do all of that and you did not do anything else to authenticate, it is unsecured.

Resources