Click here to Skip to main content
15,882,163 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have WCF service to store and retrieve messages (in/from a database). I'm calling it from ASP.NET.

When I run my app I face with following problem:

The remote server returned an error: (400) Bad Request.

I don't actually understand how to cope with it.
Here's the class that throws the exception:

C#
public class SendMessageClient
{
    private string BASE_URL = "http://localhost:49350/Service1.svc/";
    public bool sendMessage(Message message)
    {
        try
        {
            DataContractSerializer ser = new DataContractSerializer(typeof(Message));

            MemoryStream mem = new MemoryStream();
            ser.WriteObject(mem, message);
            string data = Encoding.UTF8.GetString(mem.ToArray(), 0, (int)mem.Length);
            WebClient webClient = new WebClient();
            webClient.Headers["Content-Type"] = "application/json";
            webClient.Encoding = Encoding.UTF8;
            webClient.UploadString(BASE_URL + "sendmessage", "POST", data);
            return true;
        }
        catch
        {
            return false;
        }
    }
}



Here's stack trace
C#
at System.Net.HttpWebRequest.GetResponse() at System.Net.WebClient.GetWebResponse(WebRequest request) at System.Net.WebClient.DownloadBits(WebRequest request, Stream writeStream) at System.Net.WebClient.UploadBits(WebRequest request, Stream readStream, Byte[] buffer, Int32 chunkSize, Byte[] header, Byte[] footer) at System.Net.WebClient.UploadDataInternal(Uri address, String method, Byte[] data, WebRequest& request) at System.Net.WebClient.UploadString(Uri address, String method, String data) at System.Net.WebClient.UploadString(String address, String method, String data) at MessageServiceClient.Models.SendMessageClient.sendMessage(Message message) in C:\Users\Asus\source\repos\Web service project\MessageServiceClient\Models\SendMessageClient.cs:line 27


What I have tried:

I checked my WCF Service with reqbin and it works with 200 OK inserting data to my db
Posted
Updated 12-Jul-20 22:40pm

1 solution

Given your content type is JSON, should not this
DataContractSerializer ser = new DataContractSerializer(typeof(Message));
be this
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Message));


It's the only thing that is 'obvious'
 
Share this answer
 
Comments
triyul22 13-Jul-20 5:06am    
Thank you so much!!! It is working now
triyul22 13-Jul-20 6:35am    
Sorry to ask you again, but I have a new problem:
System.BadImageFormatException: 'Could not load file or assembly 'System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context.
May be you can advise something
Garth J Lancaster 13-Jul-20 21:05pm    
well, without having the project in front of me and seeing if 'you' have added a reference to System.Web.Extensions, I can't think of much - I don't work with these components a lot these days, so I'd have to Google it myself. Sorry

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900