Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SocketException when calling WCF service from MVC on same server

I have a Windows 2008 Server with IIS 7.5 and one IP. There are two Application under the root, /web and /service. /web is a MVC4 application, and /service is a WCF 4.0 service.

When I consume the service from MVC, I use the following code:

// Create the web request  
HttpWebRequest request = WebRequest.Create(TripServiceUrl + id) as HttpWebRequest;

// Get response  
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
    // Get the response stream  
    StreamReader reader = new StreamReader(response.GetResponseStream());

    // Console application output  
    tripJson = reader.ReadToEnd();
} 

I get the following SocketException:

[SocketException (0x274c): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 10.243.6.43:80]
System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) +273
System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception) +584

However, I can see 10.243.6.43:80 (an internal address) from a browser on the server and successfully call the service. The IP is not publicly accessible.

What configuration should I have to do to make a self referential call like that work?

like image 710
Bill Sempf Avatar asked Apr 24 '12 16:04

Bill Sempf


People also ask

How to call WCF service through URL?

In order to use a WCF service, you will need to create a WCF client proxy. In Visual Studio, you would right-click on the project and pick the "Add Service Reference" from the context menu. Type in the URL you want to connect to, and if that service is running, you should get a client proxy file generated for you.

How to call a WCF service Reference in c#?

With the service running, right click the project that will contain the WCF client proxy and select Add > Service Reference. In the Add Service Reference Dialog, type in the URL to the service you want to call and click the Go button. The dialog will display a list of services available at the address you specify.


1 Answers

It's probably a loopback problem (security issue). See http://support.microsoft.com/kb/896861.

To summarize:

In Registry Editor, locate and then click the following registry key: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa Right-click Lsa, point to New, and then click DWORD Value. Type DisableLoopbackCheck, and then press ENTER. Right-click DisableLoopbackCheck, and then click Modify. In the Value data box, type 1, and then click OK.

Don't follow the step about DisableStrictNameChecking.

You'll also find a lot of reasons not to do this (you are, after all, disabling a security check), followed by a lot of people saying it's the only way to get SharePoint to work properly. In any event, it will help you determine if this is the problem.

like image 192
Greg Smalter Avatar answered Sep 23 '22 08:09

Greg Smalter