Search code examples
c#wcfrestproxywebhttpbinding

WCF Service on remote server returns 400, works fine locally


I made a small console application that hosts a WCF Service, this application is also the client and implements the Service.

It works fine on my local machine and returns the data in JSON to the webbrowser. Im using the Chrome browser with Advanced Rest CLient, but also used a javascript client and succesfully read the response.

I deployed the executable on the server and changed the endpoint adres to the domainname. Visiting the service on the domain on the right port also shows the Service is activated, i can also acces the wsdl remotely.

When i try to Post i get a http 400 bad request error. I have not been able to figure out why this works fine locally but not on a remote server.

[ServiceContract]
    public interface IService
    {
        [WebInvoke(UriTemplate = "/getRecipes/",
            Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
        [OperationContract]
        object[][] getRecipes();
    }

public class Service : IService
{
    private static bool IgnoreCertificateErrorHandler(object Sender,
                                            System.Security.Cryptography.X509Certificates.X509Certificate Cert,
                                            System.Security.Cryptography.X509Certificates.X509Chain Ch,
                                            System.Net.Security.SslPolicyErrors PolErr)
    {
        return true;
    }

    public object[][] getRecipes()
    {

        //this is the place where implement a client implementation of a webservice and it full the object array beneath, this returns more then 200 rows.
        object[][] QueryResult; 
        return QueryResult;
    }
}

App.config:

<system.serviceModel>
    <services>
      <service name="CS.Service" behaviorConfiguration="PageBehave">
        <host>
          <baseAddresses>
            <add baseAddress="http://domain.com:9001/Service"/>
          </baseAddresses>
        </host>
        <endpoint address="http://domain.com:9001/Service" binding="webHttpBinding" behaviorConfiguration="webBehavior" contract="CS.IService"/>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="webBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="PageBehave">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

UPDATED:

This application is a bridge to communicate with a remote webservice. Currently the error im getting is: The remote name could not be resolved:

. The exception stack trace is:
System.Net.HttpWebRequest.GetRequestStream(TransportContext&amp; context)
       at System.Net.HttpWebRequest.GetRequestStream()
       at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
   `at ConsoleApplication1.ABC.ABC.select(String sql, Object[] params) in` 

c:\Users\---\Desktop\--\--\Web References\ABC\Reference.cs:line 81
       at CS.Service.getRecipeDetails(String number) in c:\Users\--\Desktop\---\ConsoleApplication1\Program.cs:line 134
       at SyncInvokegetRecipeDetails(Object , Object[] , Object[] )
       at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]&amp; outputs)
       at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc)


 at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

This bridge application works perfect on any local machine i tried. Just the server is giving problems.


Solution

  • Solved this issue. The host file on the server had to include the remote server i was trying to acces from the wcf service.