I developed a WCF Service with the following post method:
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "/InsertBearingData")]
bool InsertBearingData(String JSONString);
I am using Fiddler to formulate an HTTP POST Request for this method but, it is returning Status Code - 400 Bad Request. This is the request formulated:
Request Header:
Host: localhost:21468
Content-Length: 96
Content-Type: application/json
Request Body:
[{"start time":"29-03-2013 11:20:11.340","direction":"SW","end time":"29-03-2013 11:20:14.770"}]
Can you please tell me how to formulate a good request in order to get a succesful response?
There are a few issues in your code:
The data type of the parameter is string, but you're passing a JSON array to it; a string parameter requires a JSON string to be passed.
The body style of the operation is set to Wrapped, which means that the parameter should be wrapped in an object whose key is the parameter name, something like {"JSONString":<the actual parameter value>}
To receive a request like the one you're sending, you need to have an operation like the following:
[ServiceContract]
public interface ITest
{
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "/InsertBearingData")]
bool InsertBearingData(MyType[] param);
}
[DataContract]
public class MyType
{
[DataMember(Name = "start time")]
public string StartTime { get; set; }
[DataMember(Name = "end time")]
public string EndTime { get; set; }
[DataMember(Name = "direction")]
public string Direction { get; set; }
}
Related
I have a restful service based on WCF like below:
(The FeedbackInfo class has only one enum member - ServiceCode.)
[OperationContract]
[WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public List<FeedbackInfo> GetFeedbackInfoList(ServiceCode code)
{
return ALLDAO.GetFeedbackInfoList(code);
}
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public int? CreateFeedback(FeedbackInfo feedback)
{
return ALLDAO.CreateFeedback(feedback);
}
I will use jquery ajax to invoke these two method like below:
$.ajax({
type: "GET",
url: "/Service/ALL.svc/GetFeedbackInfoList",
datatype: "text/json",
data: { "code": "Info"},
});
var feedbackInfo = { feedback: {
ServiceCode: "Info"
}};
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "/Service/ALL.svc/CreateFeedback",
datatype: "text/json",
data: JSON.stringify(feedbackInfo),
});
The first calling will be excuted succesfully, whereas the second one give me an error: The value "Info" cannot be parsed as the type 'Int64'. I'm wondering why the same one enum can not be parsed in the second calling? Just because of the enum type being used as a member of class?
EDIT:
The FeedbackInfo and ServiceCode are looked like below:
public class FeedbackInfo
{
public int ID { get; set; }
public string Title { get; set; }
public ServiceCode ServiceCode { get; set; }
}
[DataContract]
public enum ServiceCode
{
[EnumMember]
Info,
[EnumMember]
Error
}
I have put together a better solution that uses the Newtonsoft.Json library. It fixes the enum issue and also makes the error handling much better. It's quite a lot of code, so you can find it on GitHub here: https://github.com/jongrant/wcfjsonserializer/blob/master/NewtonsoftJsonFormatter.cs
You have to add some entries to your Web.config to get it to work, you can see an example file here:
https://github.com/jongrant/wcfjsonserializer/blob/master/Web.config
Enums are serialized as integers, so you'd need to use ServiceCode: 1 (or whatever), or alternatively, add a custom property in the FeedbackInfo class to deserialize the enum from a given string value. Ie, something like this:
public string ServiceCode {
get {
return ServiceCodeEnum.ToString();
}
set {
MyEnum enumVal;
if (Enum.TryParse<MyEnum>(value, out enumVal))
ServiceCodeEnum = enumVal;
else
ServiceCodeEnum = default(MyEnum);
}
}
private MyEnum ServiceCodeEnum { get; set; }
I am trying to pass a List in WCF Post call as parameter. Below is my code.
TrackingNumbers.cs
[KnownType(typeof(List<string>))]
[DataContract]
public class TrackingNumbers
{
[DataMember]
public List<object> TrackingNumberList { get; set; }
}
IService.cs
[OperationContract]
[WebInvoke(Method = "POST",
RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "v1/xml/GetMultipleShipmentDetails/")]
Shipments[] XMLMultipleData(TrackingNumbers trackingNumbers);
Now When I am passing the following XML from fiddler
<TrackingNumbers xmlns="http://schemas.datacontract.org/2004/07/Chhotu.Web.Partner.API">
<TrackingNumberList>10000008871</TrackingNumberList>
<TrackingNumberList>10000008864</TrackingNumberList>
<TrackingNumberList>10000008858</TrackingNumberList>
</TrackingNumbers>
the XMLMultipleData method is not getting called from debugger. Please suggest what i am doing wrong.
I see some issues with our code:
No need for [KnownType(typeof(List))], the DataContractSerializer knows this type already.
List - is not a good approach. If you use numbers why not make it an int?
Are you sure you are attached? You can try to enforce the attaching of a debugger with Debugger.Break(); at a good position in your code.
I will suggest you try change this line
Shipments[] XMLMultipleData(TrackingNumbers trackingNumbers);
with
Shipments[] XMLMultipleData(TrackingNumbers[] trackingNumbers);
And your [DataContract] as
[DataContract]
public class TrackingNumbers
{
[DataMember]
public int TrackingNumber{ get; set; }
}
Also! Try removing KnownType(typeof(List<string>))]
Thanks!
I have a WCF REST service that needs to communicate with another WCF REST service.
There are three websites:
Default Web Site
Website1
Website2
If I set up both services in Default Web Site and connect to the other (using HttpClient) using the URI http://localhost/service then everything is okay.
The desired set-up is to move these two services to separate websites and rather than using the URI http://localhost/service, accessing the service via http://website1.domain.com/service still using HttpClient.
I received the exception:
System.ArgumentOutOfRangeException: Unauthorized (401) is not one of
the following: OK (200), Created (201), Accepted (202),
NonAuthoritativeInformation (203), NoContent (204), ResetContent
(205), PartialContent (206)
I can see this is a 401, but what is going on here?
Thanks
I think this is related to your setup for webservice. It is best if you just create GET,POST,Put,DELETE heartbeat calls for new services and then check those from fiddler. If you get 401, it may mean your app pool identity could not access something.
Steps to fix that:
Give user read/write/modify/execute/..similar rights at your WCF publish folder
Create app pool for this site in .net 4 integrated
Set this user to application pool identity, enable anonymous mode
Enable PUt,Delete verbs as well
Part of a heartbeat class in your service to test calls:
[DataContract]
public class StatusInfo
{
[DataMember]
public string MachineName { get; set; }
[DataMember]
public string IpAddress{ get; set; }
[DataMember]
public string Methodname { get; set; }
public override string ToString()
{
return "Machinename:" + MachineName + " ;IP:" + IpAddress + "; Method:" + Methodname;
}
}
private void ResolveStatus(StatusInfo statusInfo,string methodname)
{
try
{
var context = System.ServiceModel.OperationContext.Current;
RemoteEndpointMessageProperty property =
(RemoteEndpointMessageProperty)
context.IncomingMessageProperties[RemoteEndpointMessageProperty.Name];
statusInfo.IpAddress = property.Address;
statusInfo.MachineName = Environment.MachineName;
statusInfo.Methodname = methodname;
}catch(Exception ex)
{
}
}
/// <summary>
/// create task
/// </summary>
/// <param name="taskwrapped"></param>
[WebInvoke(Method = "POST", UriTemplate = "", RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
public StatusInfo postcall()
{
StatusInfo statusInfo = new StatusInfo();
logger.Trace(Tagname + "postcall");
ResolveStatus(statusInfo, "POST");
return statusInfo;
}
/// <summary>
/// edit task
/// </summary>
[WebInvoke(Method = "PUT", UriTemplate = "", RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
public StatusInfo Edit()
{
StatusInfo statusInfo = new StatusInfo();
logger.Trace(Tagname + "Edit");
ResolveStatus(statusInfo, "PUT");
return statusInfo;
}
//delete request with taskid
[WebInvoke(Method = "DELETE", UriTemplate = "", RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
public StatusInfo DeleteCall()
{
StatusInfo statusInfo = new StatusInfo();
logger.Trace(Tagname + "Edit");
ResolveStatus(statusInfo, "DELETE");
return statusInfo;
}
//delete request with taskid
[WebInvoke(Method = "DELETE", UriTemplate = "/{recordid}", RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
public StatusInfo DeleteCallWithParam(string recordid)
{
StatusInfo statusInfo = new StatusInfo();
logger.Trace(Tagname + "Edit");
ResolveStatus(statusInfo, "DELETE/"+recordid);
return statusInfo;
}
enter code here
I received the exception:
Who is "I"? One of the web services or some other client?
If I'm understanding things correctly, it's the receiving end that seems to be expecting a range of responses, 401 not being one of them. It maybe some error checking code that expects "this range" of responses and does X (and 401 isn't one of these, or there is no "default" method to account for x response?).
That said, 401, is an authorization error so check on possible ServiceAuthorizationManager and/or similar settings in place that isn't being met by "I" causing the 401 response in the first place....
Hth...
My guess is you are missing authorizaton headers or credentials.
Check this out :
Consume RESt API from .NET
How to authenticate with Rest-client based on HttpClient and .net4
Suppose I have a web service which accept email id and password as parameter in url . I have to authenticate user by email id and password. using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Serialization;using System.ServiceModel;using System.Text;using System.ServiceModel.Web;namespace WebApp.Services{[ServiceContract]public interface IService { [WebInvoke(Method = "GET", UriTemplate = "/authenticate/{emailId}/{password}", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] [OperationContract] Boolean Authenticate(string emailId, string password); }}
we call web service like it :
http://localhost:14176/Services/Service.svc/Authenticate/sushant.bhatnagar#greatdevelopers.com/123
because email contain '.' which is not encoded by the web browser , so web service function is not called .
There is any solution to pass email id in url other than query string .
If You could use POST:
[ServiceContract]
public interface IMyService
{
[OperationContract]
bool Authenticate(EmailCredential request);
}
[DataContract]
public class EmailCredential
{
[DataMember]
public string EmailId {get; set;}
[DataMember]
public string Password {get; set;}
}
and call service using WebClient or WebHttpRequest with that xml (i don't know now how json looks like for this so xml)
<EmailCredential >
<EmailId >sushant.bhatnagar#greatdevelopers.com</EmailId >
<Password >123</Password >
</EmailCredential >
My service interface is:
[ServiceContract]
public interface IMyService
{
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "HelloJSON/{name}")]
string HelloJSON(string name);
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "GetEmployees")]
List<Employee> GetEmployees();
}
My implementation is:
public class MyService : IMyService
{
public string HelloJSON(string name)
{
return string.Format("Hello {0} in JSON", name);
}
public List<Employee> GetEmployees()
{
using (DBEntities ctx = new DBEntities())
{
List<Employee> emp = new List<Employee>();
emp = (from e in ctx.Employee select e).ToList();
return emp;
}
}
}
When I call the first method I get something like "Hello pepe in JSON", that's ok.
When I call the second method and set a breakpoint on line "return emp;" I get the list of the employees(there are 6 records from the database), but in IE I get this:
Internet Explorer cannot display the webpage
and testing in Firefox all I get is a blank page with a blank body, no HTML, no data and no errors.
I think WCF can't serialize my default EF4 entities.
EDIT:
My final solution was something(not exactly) like this:
static string SerializeJSON<T>(T obj) {
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(obj); }
EF entities cannot be serialized by default you must add code generation to them.
Refer to this article on how to create Serializable entities.
called Self Tracking entities
The best way would be to enable RIA Services and expose JSON Endpoint, it does everything correctly.
http://blogs.msdn.com/b/davrous/archive/2010/12/14/how-to-open-a-wcf-ria-services-application-to-other-type-of-clients-the-json-endpoint-4-5.aspx
http://channel9.msdn.com/Shows/SilverlightTV/Silverlight-TV-26-Exposing-SOAP-OData-and-JSON-Endpoints-for-RIA-Services