WCF Service and ajax call in same project - c#

I hav a C# web application. In that I have added a WCF Service file (.svc) by Right Click project Add New Item >> WCF Service (wcfService.svc). ( Now I have IwcfService.cs and wcfService.cs in my App_Code folder) And also added a function WCFXmlData(string id) inside that.
I am trying to access the function inside wcf service file from an ajax call in my application (in an aspx file).
But I am not able to do that.
Also I tried to browse this .svc file directly. There I got a message like Metadata publishing for this service is currently disabled.

That really depends on your binding, if you use the WebHttpBinding you can simple access the data via a browser. In best case you should modify your contract in such a way that it returns JSON, this is less overhead than XML or even SOAP (which uses also XML).
It your webpage is also implemented in the webservice you have nothing special to care about, but if your service runs under another subdomain you need to implement JSONP or Cross-Origin Resource Sharing (CORS) to manage cross domain calls.

Related

Implementing a Web Service which sends/receives data via Form Get/Post

I recently got a requirement to develop a new web service, and I'm not entirely sure how to approach it.
I'm familiar with normal WCF web services, where the url is something like
http://server/site/Service.svc/SomeMethod
that you can post XML/JSON to.
The new service is supposed to accept an HTML file post, where the content type is
multipart/form-data
From what I understand, the form contains fields, one of which contains a bunch of XML data which I want to parse. I will then respond by posting my own similar html file to a remote location.
I'm not completely sure how to begin with this.
A WCF Service of some kind?
ASMX?
Or even an actual ASPX page that the client will post to?
You can use an .asmx file to create a web service and can use it through aspx pages

Webservices with dynamic urls

I have a project with several references to webservices and have made two different config files (one for dev/stage and one for production) with different URLs to the services. Whichever URL the webservices were originally added using works fine, but the other one fails.
For example, if I use the "add web reference" button to add a service at server1 everything works fine when I run my application. If I then switch out the config file so it's trying to use an identical service on server2 my application no longer works. Every call .net makes to the webservice returns null. If I use fiddler to see the XML the servers return they look identical.
Also, anytime the web services get rebuilt (even if nothing changes) I have to refresh all the references in my application. Don't know if that's related.
What would cause this and is there any way to fix it?
I'm doing a big project that has this.
A user creates an account with it's own web service endpoint, so I need them to be dynamic.
The trick is to always change the Endpoint address before each call to the service.
example of a Find service
FindClient wsFin = new FindClient();
wsFin.Endpoint.Address =
new System.ServiceModel.EndpointAddress(clientUrl.TrimEnd('/') + "/Find.svc");
just as a note: for WCF Services I have preferred to create a proxy using the svcutil instead of adding the reference, but for asmx web services, I did add reference :)

Web Service deployment in IIS?

I started learning web services. I learnt about web services, UDDI, WSDL, SOAP etc. and architecture of web services. Visual Studio is running the service in local system successfully.
Then I deployed the entire folder of that web service in IIS wwwroot, and tested. Its running successfully.
But when I remove the other file from the wwwroot\webService1 folder (I left only service1.asmx and bin folder) then also service is running.
Here I see that only two file are used in ruuning the webservice one is .asmx and another one is webService.dll in bin folder.
I'm not able to understand where is SOAP, WSDL, namespace or other things, that are required to run web service.
Please clarify.
SOAP, WSDL, Namespace are all handled by IIS and ASP.NET. In your scenario, your web service endpoint is your asmx file (no .cs file required in your deployment), and the DLL in the bin folder contains the code that you wrote for your webservice (so it does something).
If you call up your webservice in a web browser, you should see your web methods listed out to test. IIS knows how to process *.asmx files to do this. If you click on one, you should see a sample form (if input parameters are expected) and a button. Again, IIS knows how to serve this out to you. When you click the button, IIS and ASP.NET handle the work of SOAPing your request, handling it with your code, and SOAPing the response back to you.
If you create a "test" project in Visual Studio, and set a web service reference that points to your deployed web service, Visual Studio will create a proxy class and pull in some additional code from it's discovery of the service. Try it. You should get at least: a WSDL which defines your web service, a file called reference.cs which contains the code that does the heavy lifting of calling your webservice (SOAPing the request from your application and unSOAPing from the response).
If you download a tool called Fiddler, you should be able to intercept and inspect the SOAP call to your web service.
Take a look at Web Services with ASP.NET for additional information.
There are no such 'files' at all. The asmx and dll files contain all of the code for the service. You can see some of that in the URLs that are requested for the SOAP/WSDL info.
I believe if you append ?WSDL after .asmx you will see the definitions.
Such as this example:
WSDL Example
I think these are protocols and does not require anything. IIS and the requesting applications understand these protocols.

Custom Security For ashx Handler

In my WCF web service I have a custom ashx handler. It is designed so that a person can call the web service and get a dynamic link to download a file.
So, an IIS server which is serving up a page to a client calls the web service. This generates the link. The link can be handed to the client machine (i.e. they will be running a web browser) and their browser can open the link. The link will be to the ashx handler, and the result will be that the file gets downloaded.
For the WCF service over all it is using windows authentication because the service is not public, but I want to allow anonymous authentication for the ashx handler because that could be called from any number of client machines.
Any ideas?
Thanks.
I'd recommend moving the ashx handler and file download functionality to a separate application root so that you can configure it with anonymous access. Decoupling the web service from the file download service would also let the two live on different servers, potentially solving firewall issues that you may encounter later if the download service needs to be accessed externally but you need to keep the web service private.

Dynamic Webservice reference from Class Library used in Winforms app (c#)

Ok - pretty basic scenario, been there before, seemed all so simple - but can't recall enough to work out what's different about the setup at this particular existing codebase.
Winforms App calls Dll which calls Web Service. Reference in the Dll to the Web Service is dynamic. How do I get the URI for the Web Service into a Winforms app.config so I can easily change it for test, dev, live etc.
[Oh just to make it interesting, though I can't see it mattering, the proxy for the web service needs to NOT be regenerated as we have customised it...]
Set the URL directly in your code.
YourServiceProxy service = new YourServiceProxy();
service.Url = ConfigurationManager.AppSettings["YourURLKey"];
Can you configure the web service URI dynamically in code? That way you can easily modify the service to point to the desired location.
You can set the Url property of the webservice in code to point to the URI and use Proxy to set the proxy to your custom proxy.
What's wrong with just copying the URL from the app.config of the library into the app.config of the Windows Forms application?
Also, I'll suggest strongly that you do not modify generated code, ever. You can make many customizations of the proxy by using partial classes. See Ways to Customize your ASMX Client Proxy.

Categories