WebService call works from WinForms app but not from WebApp - c#

I am trying to call a webservice to get information about specific objects. It works perfectly fine in my test WinForms application, basically after I added the service reference I did not have to do anything else than creating a local client in my C# class, setting up the input parameters and calling the method that was in the documentation and I received the response I wanted. Very simple webservive, no https, no magic.
I copied my code to my MVC web application, I made sure to copy EXACTLY the same url, web.config settings (bindings, endpoints), I even updated the service reference many times, but in MVC it does not work. The error message says:
"There was no endpoint listening at 'webservicename' that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details."
The inner exception is:
"No connection could be made because the target machine actively refused it 127.0.0.1:8888"
I have set up my hosts file, because I am using Local IIS and wanted to use the websie's name in the url, so I have this in my hosts:
"127.0.0.1 www.mywebsite.local"
I have a feeling that either my firewall or app pool setting causes this problem, but I could not figure out which one. Once again, the webservice is working perfectly fine from my basic test WinForms app.
I tried to change the Identity of my app pool to something else than ApplicationPoolIdentity (I tried NetworkService, LocalService, LocalSystem), but it did not help.
.Net version is 4.5 in WinForms and 4.5.1 in MVC.
Any ideas?
Edit:
It looks like there is a problem with the proxy settings. In the MVC web.config I had the following section:
<system.net>
<defaultProxy enabled="true">
<proxy proxyaddress="http://127.0.0.1:8888" />
</defaultProxy>
I do not know why, but when I set it to false, the problem goes away...I am not an expert of proxies. Does it mean I have to add special settings to my firewall?

Related

Programmatically changing defaultproxy instead of using app.config

I'm creating a desktop application in WPF. This application uses webclient instances to communicate with an API to collect data from.
In this desktop application I want to create a checkbox which should allow the user to ignore the internet options proxy or to use the default auto detect options.
At this moment I added this defaultproxy setting to my app.config to stop my application from trying to communicate through the proxy and instead ignore it.
<system.net>
<defaultProxy enabled="false" useDefaultCredentials="false">
<proxy/>
<bypasslist/>
<module/>
</defaultProxy>
</system.net>
When I don't have this code in my app.config, my application will try to use the default internet options proxy.
So in order to have this switchable by a checkbox in a settings menu I will have to change these settings programatically.
I am aware of the fact that when I set the default proxy to a new webproxy that the application will ignore the internetoptions proxy.
WebRequest.DefaultWebProxy = new WebProxy();
But I can't for the life of me figure out how I can set this back to the automatically detecting of proxy use, like before I inserted above defaultproxy settings in the app.config.
I am testing this by using a faulty proxy. This means that if I send an api request, the proxy can't be found and I receive a webexception. When I ignore the proxy with the app.config code, the request uses my normal internet connection, and returns API data.
It would really help me out if anyone could tell me how I can programatically set my application to ignore the proxy or, most importantly, to use the default auto detection settings.
If you want to set the proxy back to the default proxy, you can use this static method
WebRequest.GetSystemWebProxy();
see here https://msdn.microsoft.com/de-de/library/system.net.webrequest.getsystemwebproxy(v=vs.110).aspx

How to disable Expect:100-Continue when using WCF in Windows Store App

I need to get rid of the Expect: 100-Continue header in HTTP message, when communicating with WebService using WCF in Windows Store App.
I have found a lot of solutions, but none of them is possible in Windows 8:
ServicePoint.Expect100Continue = false; doesn't exist in Store App any more (No SericePoint or ServicePointManager class),
I cannot change configuration in web.config file, because it doesn't exist in Store App,
It is possible to change the flag in HttpClient, but I cannot access it, when using WCF,
I tried to manipulate with message headers using IClientMessageInspector, but the default HTTP headers are being added later, in higher layers, so my changes will be ovverriden.
Does anyone have any other ideas?
There are 2 ways to deal with this.
You can turn set expect100Continue="false" in you app.config or web.config, but this will be global. This could be an issue if some external services prefer to use the header expect100Continue. Here's how:
<system.net>
<settings>
<servicePointManager expect100Continue="false"/>
</settings>
</system.net>
Or you can do it in code for a specific endpoint as follows:
System.Net.ServicePoint servicePoint =
System.Net.ServicePointManager.FindServicePoint(myWcfService.Endpoint.Address.Uri);
servicePoint.Expect100Continue = false;
// now execute some service operation

401 Unauthorized error web api mvc windows authentication

I am getting 401 unauthorized error . My web service is written in mvc . in IIS configured to use windows authentication.
Below is screen shot of fiddler
When I hit URL from browser it gives me popup window to enter user name and password.
How can I avoid popup window?
I am calling this web api from another window service.
I suspect that the two web services may be hosted on the same server. In this case, the problem may be caused by the loopback check. In order to test, try referencing the service without using the fully qualified domain name and see if it works. If it does, use the following steps to specify which host names are on the local computer.
Method 1: Specify host names (Preferred method if NTLM authentication is desired) (https://support.microsoft.com/en-us/help/926642/)
To specify the host names that are mapped to the loopback address and can connect to Web sites on your computer, follow these steps:
Set the DisableStrictNameChecking registry entry to 1. For more information about how to do this, click the following article number to view the article in the Microsoft Knowledge Base: 281308 Connecting to SMB share on a Windows 2000-based computer or a Windows Server 2003-based computer may not work with an alias name
Click Start, click Run, type regedit, and then click OK.
In Registry Editor, locate and then click the following registry key:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0
Right-click MSV1_0, point to New, and then click Multi-String Value.
Type BackConnectionHostNames, and then press ENTER.
Right-click BackConnectionHostNames, and then click Modify.
In the Value data box, type the host name or the host names for the sites that are on the local computer, and then click OK.
Quit Registry Editor, and then restart the IISAdmin service.
https://learn.microsoft.com/en-gb/archive/blogs/sharepoint_foxhole/disableloopbackcheck-lets-do-it-the-right-way
**Edited to be in the form of an answer and include detailed steps from referenced links
If you are using WebClient you need to set the Credientials. How are you calling the web api from the windows service?
My 2 cents: I faced a scenario where we were baffled by an HTTP 401 when requesting an image when the web application was deployed. We use WiX as our packaging and install solution.
In this specific case, the image wasn't being packaged by the installer and hence the path did nor exist on the deployed instance.
One may wonder why this threw a 401 when a 404 (not found) would have been expected - my understanding is that since our path was not directly under the root but something like root/content/images/image.png, and I made an anonymous request, I got a 401 (unauthorized) as I did not have the access to browse the directory. I confirmed this by adding an Authorization header to my request and then as expected I got a 404.
You can specify the username and password as part of the URL:
http://username:password#www.example.com/foo/bar/baz
Note: Just because you can doesn't mean you should. While this can be a temporary solution to test things, I would not suggest doing this in production. And in the old days, this is how we did it. But as #DiskJunky points out, "URLs are easily visible to pretty much anything/anyone" which includes your browser history, server logs, and perhaps worse.
I added below lines in web config to fix the issue and it worked.
<security>
<authorization>
<add accessType="Allow" users="*" />
</authorization>
</security>

Error when trying to add a Service Reference

I´m trying to create a client in C# to a web service which (I suppose) is written in Java. It´s my first time trying to write a client, so I´m following the instructions on MSDN, but I´m stuck on a problem with Add Reference. When I open the Add Service Reference dialog and add the URL, an error occurs:
There was an error downloading 'http://geoportal.cuzk.cz/WCTService/WCTService.svc'.
The request failed with HTTP status 404: Not Found.
Metadata contains a reference that cannot be resolved: 'http://geoportal.cuzk.cz/WCTService/WCTService.svc'.
There was no endpoint listening at http://geoportal.cuzk.cz/WCTService/WCTService.svc that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
The remote server returned an error: (404) Not Found.
If the service is defined in the current solution, try building the solution and adding the service reference again.
What should my next step be? I don´t know what I should do with this!
(It is a coordinates-transformation service from the Czech Republic.)
For more information:
Property services (GetCapabilities)
http://geoportal.cuzk.cz/WCTService/WCTService.svc/get?
Localization services:
http://geoportal.cuzk.cz/WCTService/WCTService.svc/get?request=GetCapabilities&service=WCTS
I was facing a similar situation in which I had created a WCF Service (Employee.svc) and later changed the named to EmployeeService.svc. WCF project compiled just fine but when I was trying to add service reference from by UI Client, I was getting following error:
Metadata contains a reference that cannot be resolved: 'http://localhost:2278/EmployeeService.svc?wsdl'.
The document format is not recognized (the content type is 'text/html; charset=UTF-8').
Metadata contains a reference that cannot be resolved: 'http://localhost:2278/EmployeeService.svc'.
There was no endpoint listening at 'http://localhost:2278/EmployeeService.svc' that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
The remote server returned an error: (404) Not Found.
If the service is defined in the current solution, try building the solution and adding the service reference again.
I resolved it by replacing the correct service class name everywhere. In my case, it should have been EmployeeService and NOT employee. The left out place was in the markup code of svc file:
<%# ServiceHost Language="C#" Debug="true" Service="WCFServiceHost.**Employee**" CodeBehind="EmployeeService.svc.cs" %>
Changed it to
<%# ServiceHost Language="C#" Debug="true" Service="WCFServiceHost.**EmployeeService**" CodeBehind="EmployeeService.svc.cs" %>
And it started working again!!! Dont forget to build your WCF project after changing the service name.
I tried browsing to http://geoportal.cuzk.cz/WCTService/WCTService.svc?wsdl. It looks like this service is not exposing metadata.
I did a bit of googling on OpenGIS, and I think you need to have a look at this article:
OpenGIS with .NET
You won't be able to just add a service reference and go. It looks like you need to craft a concrete WSDL.
There may be a client-side library you can use / customize to assist with integration. Have a look at Stack Overflow question Using MySQL GeoSpatial data types in .NET.
I know this is an old thread and has already been resolved, but I just finished troubleshooting this exact issue, and none of the resolutions presented here worked for me. Wanted to share my resolution in case anyone else runs into this thread with a similar issue.
My ENTIRE issue stemmed from a bad Refactor->Rename operation. I recently purchased Resharper for my dev team and Resharper did not like the name of our service implementation name. We had named it "WCFAccess" and Resharper wanted the name "WcfAccess". I had just published an update, had the release safely isolated in its own release branch in git, and figured this was a good time to perform a rename on the develop branch and shut Resharper up about the naming. I used Refactor->Rename to change the name of the file to match the naming convention we had defined in the Resharper configuration. The rename operation completed, the solution compiled and ran, time goes on and the WCF rename was forgotten.
Fast forward a couple weeks, and its time to deploy out web services to the test environment for regression testing. The solution compiled successfully, published successfully, then gave me the EXACT error that the OP posted. What I ended up finding out is that the Rename operation from weeks ago ONLY UPDATED SOURCE CODE REFERENCES to the old name and did not rename MARKUP. When I navigated out to our web server where the service was published to and double clicked on the .svc file, it opened the markup in Visual Studio and I noticed that the character casing of the CodeBehind="ServiceNameHere.svc.vb" was inconstant with the new naming convention. Updating the markup and web.config files to reference the correct character casing resolved my issue.
I hope this helps someone. It was incredibly frustrating to troubleshoot
(Please don't hate me for using VB.Net, I inherited this application) :-)
Is the service definitely up and running before you try to add a service reference? If it exposes meta data, does it have a service behaviour or equivalent configured? Have you configured your firewall correctly?
While adding service reference to the client application, metadata is not accessible from service to client application. hence The remote server returned an error: (404) Not Found. Actually we can host the WCF service as follows:
Self hosting(console application)
IIS Hosting
WAS Hosting
window service hosting.
if you are using self hosting then you need to host the service in console application and run the service(run the console application) and then add the service reference to the client application, then metadata would be exchange. If service is not running then while adding service reference to the client application then 404 not found error would be getting. Same process would follow for all the hosting type. first run the service then add service reference.
I had the same problem happen to me earlier today. The webservice was running fine on local host but for some reason, I was having a 400 when trying to add the service reference in another project.
My error was caused by setting the [DataMember] annotation instead of the [EnumMember] annotation on an enum of the service. Changing it solved my issue.
The webservices doesn't run.
If you don't have access to the server where this service run, you're blocked.
Otherwise, you need to check if the server run, etc. As I don't know how the Java webservice is run, I can't help you further.

How to retrieve on which site the web application is currently running?

I am currently running a WCF service on an AppFabric server and my application needs to load a the web.config file dynamically to retrieve custom configuration sections.
On my development machine I can just load the configuration like this:
WebConfigurationManager.OpenMappedWebConfiguration(webMappedFile, virtualPath);
But on the test machine (AppFabric server) I am getting an exception and it seems that I need to specify a third parameter which is actually the site the web application is running on:
WebConfigurationManager.OpenMappedWebConfiguration(webMappedFile, virtualPath, "MySite");
So I tried to hard code it and it worked. Anyway this is not acceptable, so I need to dynamically provide the site to the WebConfigurationManager because I do not on which site the service will be running in the future. Do anybody knows how to achieve that?
Thanks.
If you are running this code as part of handling a request you could use:
Request.ServerVariables("server_name")
see: http://msdn.microsoft.com/en-us/library/ms525396(VS.90).aspx
Edit based on your comment
The parameter that you need is the Site Name, not the machine name, your code be running on many machines. If the code is running somewhere where it no longer knows that it is on a web site, then it is difficult for it to get the name of the web site that it is running on.
You then have two options:
Send the name as a parameter from a layer that has httpconext
Not sure if this will work: but you could try adding a reference to system.web to your project. It may compile, but you could get a null reference exception when you run it. Probably worth a try.
How about Server.MachineName

Categories

Resources