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

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.

Related

How to use Asp.Net Web Api with window authentication?

I have a application on local network with window authentication. Application is working fine. There is a old version of application too that is running on the same network. New application will replace old one later on. But for time being i need service to be exposed for old application. What i did is i made an API in the same application where window authentication is being used. Now the problem is that i don't want any kind of authentication on Web API but i am unable to test it by any tool like postman and fiddler. When i hit the URI it shows 401 error. Even i have to give windows credentials in browser to test it. After window authentication in browser service is running fine but i want to without window authentication.
I have tried many things like i put [allowanonymous] attribute on controller level. I put location tag with anonymous users in web.config file but nothing is working for me.
Can anyone tell me is it possible to do this and how?

ASP.NET WEB API 2 Works Locally But Not On Production Web Server

I've developed a new WEB API 2 that works great locally, however when I upload the same code to my production server (Arvixe in this case) all I get is a 404 when I call it. I've spent HOURS searching the web, reading forums, etc.. and have been able to find no resolution, so I'm asking here as my last effort.
I'm currently only testing with the default project that gets created when you do New Project > ASP.NET Web API 2 Empty Project in Visual Studio. This creates an empty project with a single ValuesController. You should be able the JSON response by called /api/values, but this doesn't even work.
I'm using Fiddler to test the API locally and on the web server.
http://localhost:1993/api/values <--- works great
but
http://api.mydomain.com/api/values <--- returns 404
Note: I created a subdomain "api" in this case, but everything for the code for the API is unchanged from when it was created.
Why in the world does this work locally but not on the production web server?
That the server returns 404 (Not Found) may indicate a lot of things. However you can check using the following step:
Add a simple text document like readme.txt to your a folder sub-domain http://api.mydomain.com, and try to get access to that. If you can't access to that file, it means that the subdomain is not configured properly.
Publish the webservice using the "Publish" functionality, so that all DLLs will be copied.
After that,try to reach the Web ApI again.
Hope that help.
"Note: I created a subdomain 'api' in this case, but everything for the code for the API is unchanged from when it was created."
Above comment of your's is suspicious, you should publish your WEB API application in the root directory. Like if http://example.com is pointing to "MyExample" folder, then application should be published on "MyExample" folder.
After that you will be able access your api with http://example.com/api/{controller}/{action}
Just a simple suggestion which I'm sure you have already considered, but have you opened the http port 80 on the server's firewall?
Also stick a plain old html file in the root of your project and see if the server serves it up.
in your case, since you create a subdomain of 'api', you should try
http://api.mydomain.com/api/api/values
note that if you're using database for the function, you should change the connectionString in your web config
Please verify the .net framework on you hosted domain that may be old one.
Web api 2 is supposed on 4.5 framework.
One reason for web api 2 method working OK on local machine but not on production server is that the method you are calling is working on local machine but not on remote server. In such a case you will receive message 404 or 500, and you would be lost why this routing is failing.
Why a method would fail on remote server, well there may be many reasons. For me, I was querying database in my method and my connectionString was not set for remote server.
One way of resolving it would be to put some very simple code in that particular method and test that routing is working. Then check your original code for errors reasons.

How to send data to and from ASP.NET 4.0 website and C# WCF (Web Service)

I am wondering - in my ASP.NET 4.0 website how to connect to the web service I have running?
This is essentially what I would like to accomplish, for testing before I apply it on a larger scale. I want the website to be able to send a value (int) from the website to the getData() method in the web service (auto generated method) and return its result (string) back to the website.
This is my setup for the Website/IIS 7/WCF:
Steps for creating website and service via VS 2010 (.NET 4.0)
Website:
File > New Website
I did not change anything on the default website template
Build > Publish Web Site
Set target location to specific folder
Other settings unchanged
IIS Settings:
Connections - Right Click > Add Web Site
Specified a site name
Specified physical path
Connect As: Set the username/password
Binding Default (http port 80)
Connections - Right Click on Default Web Site > Manage Web Site > Stop
...So as not to end up with two websites on conflicting port
Edit Permissions > Add user "Everyone"
MyNewSite > Manage Website > Start
...Clicked Browse Web Site > Brows *.80 (http)
And the website ran just fine, I was able to access it locally and through vpn with the IP address.
WCFService:
File > New Project > WCF Service Application
...I did not change any of the default settings in the WCF service
I ran WCF service as Admin, and it loaded the little test app where you can type in a value to the getData() method and it returns "You typed {some_number}".
So as far as I know - everything is working with the website and the wcf service. I tried also right clicking on the website in VS, and Add Service Reference and it was able to detect the WCF service.... however, I dont know that I should need to add a reference but simply connect to the WCF like I do to a database via a connection string of sorts.
To elaborate upon my comment, here's a stripped down example. You can find numerous examples of how to do this with a very simple Google search, by the way.
In order for your web site to call any of the methods, it must have a WCF client proxy. This proxy is used to establish the connection to the service and make calls to the methods the service exposes via the [OperationContract] attribute.
Taking your posted example above, if you created a basic no frills don't touch anything WCF Service Application, and you add a reference to it in your website from within Visual Studio, the WCF client proxy will be generated for you, as well as any necessary configuration file settings (in your Web.config file).
Once you've added the reference, you can then do something like the following in your website:
ServiceClient client = new ServiceClient();
string serviceResponse = client.GetData(5);
client.Close();
This would return "You entered: 5" for serviceResponse.
Alternatively, you could use the svcutil.exe to generate a *.cs file and and a config file. You would add the *.cs file to your website project and the <system.serviceModel> section of the config file to your Web.config, and then could use the above code to call the service.
A third way, and this is the way I prefer because of the nature of our application at work and it gives me more control, is to use ChannelFactory<T>, but I would suggest trying the above two approaches to get familiar with the basics first.
Again, see Accessing Services Using a WCF Client, and if that isn't sufficient do a Google search for more examples.
The key point here is that the client requires a proxy to communicate with the WCF service. How you generate that proxy is up to you and the requirements of your project.

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.

ASP.NET gone FUBAR on a production machine

Today we tried to put an ASP.NET application I helped to develop on yet another production machine. But this time we got a very weird error.
First of all, from all the ASP.NET pages, only Login.aspx was working. The rest just show a blank screen when they should have redirected to Login.aspx. The HTTP response is 200, but no content.
Even worse - when I try to enter the address of some inexistent ASPX page, I also get HTTP 200! Or, when I enter gibberish in some existing ASPX page code (which should have been accessible without login) I also get HTTP 200.
If I enter the name of some inexistent resource (like asdasd.jpg), I get the expected 404.
The redirect to login page is written manually in Global.asax. That's because the application has to use some alternate methods of authentication as well, so I can't just use Forms Authentication. I would suspect that Global.asax is failing, if not for the working Login page.
Noteworthy facts are also that this machine is both a Domain Controller and has SharePoint installed on it. Although the website in question is listed in SharePoint's exception list.
I would check the following:
Is the application within a virtual application or its own site and not just a virtual directory?
Does the application have it's own App Pool? If it does not then is the app pool shared by apps in a different .net version.
Is the .net version of the application the correct one? 1.1 or 2.0?
Do the files in the file system have the correct permissions to be accessed via IIS?
Have you performed an IIS Reset?
Create a stand alone test.aspx page within your folder that just displays the date/time and check it works.
Make this single test.aspx page perform an exception (eg. divide by zero) and see what the outcome is.
More information required.
What Op Sys?
What mode IIS running under?
What version of .Net?
What version of SharePoint?
(Why are you using your DC as a web host?)
Does it work on the other production machines you've deployed to?
If so what is different between this machine and the working ones?
Did you deploy the same way?
Are you sure your hitting the right machine?
Are you sure your hitting the right web site?
What ISAPI components are installed globally and for the web site?
Is .aspx mapped to the ASP.Net ISAPI filter?
Do you have any HTTP Modules or HTTP Handlers configured?
Can you change the global aspx to write out some messages so you can be sure the piece of code you interested in is reaching?
Anything coming up on the IIS log or the event logs?
Addition:
What version of .Net?
By the sounds of it the .jpg request is being dealt with by IIS directly which is why you get the 404, but the .aspx request is being dealt with by something else which except for you login page, is always returning 200.
Assuming .aspx is wired correctly to .Net the the order of processing is based on ISAPI filters (high to low then global before site), then the ASP.Net ISAPI Extension (sorry I said this was a filter earlier but it's actually an extension). Then we get into the ASP.Net pipeline based on your .Net configs, and calls the HTTP Application (which includes your global.asax code), any HTTP Modules followed finally by a HTTP Handler. Your ASP.Net web forms are just fancy HTTP Handlers.
However, the request can be responded to and terminated from any point.
Since your code works on other machines though, I'm tempted to point a finger at SharePoint if it isn't installed on the working machines. Is this SharePoint 2007? That is also an ASP.Net application (I don't think 2003 was).

Resources