none
wcf bad request error RRS feed

  • Question

  • Hi everyone,

    i am quite new in WCF world and I have create a service (very simple one)

    here is the contract

    [ServiceContract]
    public interface IBrokerTransferService
    {

    [OperationContract]
    void PutMessageIntoTable(BrokerMessage message);
    }

    This is the implementation of the contract
    public class BrokerTransferService: IBrokerTransferService
    {

    public void PutMessageIntoTable(BrokerMessage message)

    {
    // Add Message to the table

    }

    This is my Data contract class

    [DataContract]
        public class BrokerMessage
        {
            [DataMember]
           public string TransactionID;
            [DataMember]
           public string MessageID;
            [DataMember]
           public DateTime Timestamp;
            [DataMember]
           public SenderInfo SenderInfo;
            [DataMember]
           public string PacketID;
           [DataMember]
           public string PacketCount;
            [DataMember]
           public string MessagePayload;
        }
    Here is the config file

    <system.serviceModel>
        <services>
          <service name="Mary.BrokerTransferService.BrokerTransferService">
            <host>
              <baseAddresses>
                <add baseAddress = "http://localhost/BrokerTransferService/" />
              </baseAddresses>
            </host>
            <!-- Service Endpoints -->
            <!-- Unless fully qualified, address is relative to base address supplied above -->
            <endpoint address ="basic"
                      binding="basicHttpBinding"
                      contract="Mary.BrokerTransferService.IBrokerTransferService">           
            </endpoint>
           
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior>
              <!-- To avoid disclosing metadata information,
              set the value below to false and remove the metadata endpoint above before deployment -->
              <serviceMetadata httpGetEnabled="True"/>
              <!-- To receive exception details in faults for debugging purposes,
              set the value below to true.  Set to false before deployment
              to avoid disclosing exception information -->
              <serviceDebug includeExceptionDetailInFaults="False" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>

    then I create a console app to Host this service.

    class Program
        {
            static void Main(string[] args)
            {
                ServiceHost host = new ServiceHost(typeof(BrokerTransferService));
                try
                {
                    host.Open();
                    Console.WriteLine("Up and running");
                    PrintServiceInfo(host);
                    Console.Read();
                    host.Close();
                }
                catch(Exception ex)
                {
                    Console.WriteLine(ex);
                    Console.Read();
                    host.Abort();
                }
            }
            static void PrintServiceInfo(ServiceHost host)
            {
                foreach (var ep in host.Description.Endpoints)
                {
                    Console.WriteLine(ep.Address);
                }
            }
        }

    If I run the Host app, it shows that the service is Up and running and display all the endpoints from the config file. 

    Here is the config file for the Host console App

    <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior name="ServiceBehavior">
              <serviceMetadata />
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <services>
          <service name="Mary.BrokerTransferService.BrokerTransferService" behaviorConfiguration = "ServiceBehavior">
            <endpoint address="http://localhost/BrokerTransferService/basic"
              binding="basicHttpBinding" contract="Mary.BrokerTransferService.IBrokerTransferService"></endpoint>
            <endpoint address="http://localhost:8081/BrokerTransferService/MEX" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
          </service>
        </services>
      </system.serviceModel>

    Until this point everything looks that is working fine.

    But if I want to create a client application to consume my service (after run the Host app) adding reference to it

    I always got

    "There was an error downloading 'http://localhost:8081/BrokerTransferService/MEX'.
    Unable to connect to the remote server
    A socket operation was attempted to an unreachable network [::1]:8081
    Metadata contains a reference that cannot be resolved: 'http://localhost:8081/BrokerTransferService/MEX'.
    There was no endpoint listening at http://localhost:8081/BrokerTransferService/MEX that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
    Unable to connect to the remote server
    A socket operation was attempted to an unreachable network [::1]:8081
    If the service is defined in the current solution, try building the solution and adding the service reference again.
    "

    If I try to request it from the browser I got this (Http 400 bad request )

    Could someone help me to figure out what is wrong here,

    Thanks in advance

    PS: I have read a lot trying to find why and people with similar issue, but non of the solutions works for me.

    Peter

    Tuesday, January 29, 2013 4:37 PM

All replies

  • Is there any reason why you don't host the MEX endpoint in the same domain as the rest of the service? If not, try changing the host app and its config to the following, and it should work:

    Host app:

    class Program
    {
        static void Main(string[] args)
        {
            ServiceHost host = new ServiceHost(typeof(BrokerTransferService));
            try
            {
                host.Open();
                Console.WriteLine("Up and running");
                PrintServiceInfo(host);
                Console.Read();
                host.Close();
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex);
                Console.Read();
                host.Abort();
            }
        }
        static void PrintServiceInfo(ServiceHost host)
        {
            foreach (var ep in host.Description.Endpoints)
            {
                Console.WriteLine(ep.Address);
            }
        }
    }

    Config:

    <system.serviceModel>
      <behaviors>
        <serviceBehaviors>
          <behavior name="ServiceBehavior">
            <serviceMetadata />
            <serviceMetadata httpGetEnabled="true"/>
          </behavior>
        </serviceBehaviors>
      </behaviors>
      <services>
        <service name="Mary.BrokerTransferService.BrokerTransferService" behaviorConfiguration = "ServiceBehavior">
          <host>
            <baseAddresses>
              <add baseAddress = "http://localhost/BrokerTransferService/" />
            </baseAddresses>
          </host>
          <endpoint address="basic"
            binding="basicHttpBinding" contract="Mary.BrokerTransferService.IBrokerTransferService"/>
          <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        </service>
      </services>
    </system.serviceModel>


    Carlos Figueira

    Tuesday, January 29, 2013 10:22 PM
    Moderator
  • Peter, did you try the changes I mentioned? Did they work?

    Carlos Figueira

    Monday, February 25, 2013 8:12 PM
    Moderator