ScriptManager service reference generates JS code that forgets about the service host name - asp.net

I'm working on an ASP.NET project that uses AJAX to communicate with an ASMX web service. One of the pages uses a ScriptManager to reference the web service ASMX file. It's referenced using an absolute URI.
I recently changed both the ASP.NET project and web service to use IIS Express. Each now has a different port number. Since this change, the client-side code for the ASP.NET project can't access the web service.
The generated HTML code for the ASP.NET page contains a correct reference to the web service:
<script src="http://localhost:2837/Service.asmx/jsdebug" type="text/javascript"></script>
However, this script only refers back to the web service using a relative URL instead of an absolute one:
Service.set_path("/Service.asmx");
So AJAX calls to web service methods fail because the hostname from the ASP.NET application is used instead of the web service's:
POST http://localhost:2827/Service.asmx/WebServiceMethod 500 (Internal Server Error)
Notice that the port number above is different to the one used by the web service. The above port number is the one used by the web application.
The HTTP 500 is produced because the web server is reporting:
No web service found at: /Service.asmx
How can I fix this so that the web service is referenced correctly?

From Exposing Web Services to Client Script:
The ServiceReference object can reference a Web service only in the
same domain as the page. The Web service path can be relative,
application relative, domain relative, or absolute. For absolute
paths, you must make sure that the path is in the same domain.
So you can't call your web service method which is in another domain. What you can do is create another service in your asp.net project to call the original web service. then you can call newly created web service method from script manager

Related

Routing fails when api is created as a web application under another site

I developed a rest api using ASP.net web api. When I deploy this rest api on iis, I need to create it as a web application under an asp.net web site. When I was deploying the rest api as a new web site in iis, then things worked fine. I was using the following Route api/{controller}/{id}.
When I created the rest api as a web application underneath asp.net web site project, I named the web application as api. I can't access the api methods now. Can someone point what I do wrong?
WebApi is based on global.asax and web.config configurations and you can't have 2 of these files inside in the same application. In the scenario you described, it sounds like you need to create a "Virtual Directory" in IIS for the second application instead of making just a "Sub Folder".
The Virtual Directory will allow you to declare a new web.config and global.asax where you set the new routings and configurations method for your second application.
For a full walkthrough on how you can perform this please refer to this site:
http://msdn.microsoft.com/en-us/library/vstudio/bb763173(v=vs.100).aspx

Accessing a Web Service hosted on Client PC using ASP.net Web App

I have a client pc which accesses my website (CRM). The client PC is running an API toolkit as a background service and is accessed via SOAP calls, nothing tricky just standard Web Reference and some server-side methods to invoke the API toolkit's methods.
Everything is great when running the web application on my local IIS. The reference in the web.config points the address to localhost:8080 which is the address of the server. Having a custom web.config with a dynamic service url for all clients is not an option. I tried setting the url property so when the web application is started it retrieves the client machine's hostname and change the url property of the service reference however, this would only work if all the computers were on the company domain. The below reference is what works on my local pc, changing it manually to my hostname fails when run from the server and not my local dev IIS.
<applicationSettings>
<CRMIntegration.Properties.Settings>
<setting name="CRMIntegration_AgentBridgeService_WebserviceAgentBridgeService"
serializeAs="String">
<value>http://localhost:8080/AgentBridge</value>
</setting>
</CRMIntegration.Properties.Settings>
Despite several different ways of trying to map the service to the proper client it still fails to work.
(I'm thinking):
Create a method or function to set the correct path to the client pc's web service, strip out the web.config reference and write an extend the web reference class to override where the generated reference.cs is getting it's url property.
I have seen examples of this being done with the exact same API toolkit by several SaaS CRM's so I know it can be done. I don't have a clue what direction to look in after searching for a couple days.
Any help is appreciated.
The solution was easier than expected. I had two routes: use jquery with xml or an undocumented querystring in the api. I chose the querystring and all is well.

Silverlight Web.Config Setting for Web Service Reference URL?

I have a Silverlight control that is hosted within an ASP.NET application. The Silverlight control has a web service reference. The URL of this service is going to vary when we install the application for our customers. I need a way to be able to change this web service reference URL from within the web.config of the ASP.NET application. How can I do this?
You could do something like http://dev/foo/bar.svc where dev is an entry in your hosts file which will resolve to where the service resides.
If you are using Silverlight 4 you can use urls relative to the page the silverlight app is hosted on. So in ServiceReferences.ClientConfig you can change your endpoint addresses to be relative, say from http://localhost/foo/bar.svc to /foo/bar.svc.

Web service URL being overwritten with localhost

I have a reference to a web service on a remote server like such...
http://10.5.1.121/PersonifyWebServicePPROD/UniversalWebService/default.wsdl
The moment I invoke the web service and view its URL property it looks like...
http://localhost/PersonifyWebServicePPROD/UniversalWebService/default.asmx
Can anyone tell me why it's overwriting the remote server with localhost? The vendor that provided the web service said I have to reference the wsdl and not the asmx in order for it to work. I've tried running it both within IIS and as a web app on the filesystem and neither scenario work. This is on the .Net 3.5 framework.
Since this is a legacy ASMX web service, it is hosted by IIS. IIS determines the URL it thinks the service is at. Look at the IIS configuration for the site where the service is hosted. In particular, look to see if a host header is set.

Publishing an ASP.net Webservice

I have a solution with a number of projects in..
One of these projects is a web application, another is a web service.
The web app references the web service, and uses the methods in there.
In production, the web application will be hosted on one server, while the xml web service will be on another.
My question is, how do i deploy this?
I've "published" the application to the correct server, however what do i do with the xml web service? and how can i configure my web application to point to the other server, rather than try and use localhost....
You generally won't leave your web service proxy classes with their default URL in production code. You can configure the production web service url in your web.config class and then have something like
MyWebService svc = new MyWebService();
svc.Url = WebConfigurationManager.AppSettings["MyWebService"];
Something along those lines should get you what you need.
When you add the proxy to you web application, you can set it to be dynamic url. This will create a config file entry that can be set during your deployment. Click on the Web Reference in the Solution Explorer, change the URL Behavior from Static to Dynamic from properties. An app.config file will be created with a key.

Resources