WCF Restful Services - wcf

I'm developing wcf restful service using WCF 4.0. My question as below :
The post method of the server side as below pic:
The code that made the post request to server as below pic:
But when i debugged the service,i found the id property changed as below pic :
I am confused why the value of ID property changed ?

The case of the model id field is mismatching; you are passing model.ID and you are trying to read model.Id.
Change that JObject in the GetModel() method to be model.Id instead of model.ID.

Related

Facing an issue while using existing Web service as wcf into VS 2012

I have one of webservice which as below method
c.Accountdata ad = new Accountdata();
c.response res = ad.PostData(RequestData);
NOw, i have used above webservice into vs 2012 as add service reference so, it is added as a WCF SERVICE.
Now, i get below code.
c.AccountdataSoapClient ad =new AccountdataSoapClient();
c.response res = new c.response();
ad.PostData(REQUESTHEADER,RequestData,out res).
//in above method, two argumnets are increased (HEADER AND OUT PARAMETER).
I have passed request header instance as it is not actually existed in web service.
NOW, PROBLEM: it throws CLIENT FAULT EXCEPTION.
please suggst me how i could use same web service into this VS 2012 and correct such issue.
not sure, how old webservice schema map into WCF.
Thanks
If your web service is an old-style web-service (not WCF) then you should add a reference to it by this old-services compatibility menu, and use it as you are used it before:

WCF WebAPI client does not know about server types

I am following along in the .6 release of the WCF Web API chm file. I have built my service and everything works fine when I access it via IE. But when I create my console app, I don't understand how the client can know about the "contact" type. Sure I can add a reference, but how would some other client out there in the world know about the types?
List<Contact> contacts = resp.Content.ReadAs<List<Contact>>();
How would a client know about changes to the Contact class?Thanks.
Using the SOAP based WCF bindings, the client would normally generate a client off the WSDL, which would specify these custom types.
However as far as I know, in the REST based world of Web API, there is no facility for doing that. It is expected that the 3rd party customer / programmer making the client is given the data contract in some other form, and makes a compatible class.
In other words, there is not really an automatic way of doing that.
Every property on your client type that matches a property (Name/Type) in the response type is mapped by ReadAs<T>.
If you have a string property "Name" on your response type and your client type, its value is being parsed.
You don't need a reference.
Update: If you don't want to work with a contacts type on the client side you could try something like this:
var json = JsonValue.Parse(response.Content.ReadAsStringAsync().Result);
If your contact type on the server side had a property "Name" you should be able to do the following:
var name = json["Name"];
(Assuming your response was a single contact - in case of List<Contact> "json" would be of type JsonArray - you should get a clue... here is a sample showing usage of JsonValue and JsonArray).
Concerning "changes on contact type" please read this.

WCF 4 REST service can't return a StatusDescription, only StatusCode

I'm currently migrating my WCF RESTful service from .NET 3.5 (Starter Kit) to .NET 4. I started my project using a WCF Rest service template from Visual Studio 2010.
I had to figure out how to keep my authorization scheme (formely done with RequestInterceptor) using ServiceAuthorizationManager. After some work and researching I got it done.
But now I have a collateral problem. My service used to feedback my client of any processing errors using HTTP status code and a brief description. I was using WebOperationContext at many points of my service method to describe to clients what went wrong, like this:
protected void returnCode(HttpStatusCode code, string description)
{
WebOperationContext ctx = WebOperationContext.Current;
ctx.OutgoingResponse.StatusDescription = description;
ctx.OutgoingResponse.StatusCode = code;
}
But in WCF 4, only StatusCode works - StatusDescription silently fails. I can't figure out why.
My only guess is that WebOperationContext doesn't work in this new WCF 4 scenario, and I should be using OperationContext instead, but that also doesn't work. The following method is used in my custom class extending ServiceAuthorizationManager, informing clients a request couldn't be access because auth digest was malformed:
private void GenerateBadDigestMessage(ref OperationContext operationContext)
{
Message reply = Message.CreateMessage(MessageVersion.None, null, null, new DataContractJsonSerializer(typeof(object)));
HttpResponseMessageProperty hrp = new HttpResponseMessageProperty();
hrp.StatusCode = HttpStatusCode.Forbidden;
hrp.StatusDescription = "bad digest";
reply.Properties[HttpResponseMessageProperty.Name] = hrp;
operationContext.RequestContext.Reply(reply);
operationContext.RequestContext = null;
}
Even by using OperationContext direclty here (insted of WebOperationContext), StatusDescription doesn't work.
What I'm missing here? Why such a small thing can break from .NET 3.5 to 4?
I recommend you to use WebFaultException in .NET 4.0. Read for example "Introducing WCF WebHttp Services in .NET 4". Try
throw new WebFaultException<string> ("bad digest", HttpStatusCode.Forbidden);
OK! Here is what I found out. There is nothing wrong with my code. There is nothing wrong with .NET framework 3.5 or 4.0.
The problem is asp.net development server. When you are debugging your service application, it is likely to be hosted on asp.net development server and it completely ignores the status description given by application. Refer this question.
Awarding the bounty to #Oleg who at least tried to help me.
One potential problem is that you are setting the RequestContext to null:
operationContext.RequestContext.Reply(reply);
operationContext.RequestContext = null;
Another possibility is that the parameter "description" is not set.
Also on the client side are you checking:
WebOperationContext.Current.IncomingResponse.StatusDescription
One more possibility, could the values have been overwritten after returnCode was called?
Make sure you return from the Service Method NULL object...so that Status code description is visible in Response Headers, it worked for me.

Restlet basic HTTP Authentication : Help required

I am running Restful web service on 8182 port using restlet framework. I am trying to authenticate user to hit the service. i.g.
I have a query string like this http://localhost:8182/api/service/customers/?key="XXXXXXXXX"
My doubts are:
How to get value of parameter key in Resource class/Application class, so i can authenticate user upon key through my custom database.
I don't have any client code for my restful service, since i want to invoke all call from browser itself. so please tell me,how to send post data from browser itself. since i want to use post/put method to add new customer data.
I am using restlet framework 1.1.
Thanks in advance.
Karunjay Anand
You can use:
getRequest().getResourceRef().getQueryAsForm()
This will return a Form instance from which you can get the value of your query parameters (getFirstValue("key"), for example).
As Bruno pointed out, you can obtain a Form instance to access the request's query parameters:
Form form = getRequest().getResourceRef().getQueryAsForm();
for (Parameter p : form) {
System.out.println("Name: " + p.getName());
System.out.println("Value: " + p.getValue());
}
If you want to use the POST method from the browser itself, I would recommend you use one of the following add-on/extensions:
Firefox - REST Client
Google Chrome - Simple REST Client

Type 'System.Web.HttpRequest' cannot be serialized

I am trying to design an Picture Upload feature into a web site.
I am using ASP.NET 3.5, C#, and WCF.
I have been asked to accomplish the following:
1) Make the Uploader a Web Service
2) Return progress updates to the user as files are uploaded.
3) Log other relevant user-selected options in the database.
So, I have started off by creating a WCF web client with the
below service contract:
IService.UploadPictures(HttpRequest request);
private UploadServiceClient upload;
protected void Page_Load(object sender, EventArgs e)
{
upload = new UploadServiceClient();
upload.UploadPictures(Request.Files);
}
When I compile, I get the below error:
Type 'System.Web.HttpRequest' cannot
be serialized. Consider marking it
with the DataContractAttribute, and
marking all of its members you want
serialized with the
DataMemberAttribute attribute.
So, I went back into my service contract and
changed [OperationContract] to [DataContract]
but the change produced the same error.
Can somebody kindly tell me what I am doing wrong
and provide examples as to how to best move forward?
Thanks for your time.
You cannot use something like a HttpRequest as a WCF parameter. The error messages says it all - the HttpRequest is not serializable, and in order to work with WCF, types have to be serializable.
Also, you need to remember: you're not just passing an object instance to a method here - what you're really doing is having the WCF runtime serialize your request (the method name to call plus all the parameters passed in) into a message (think: e-mail or xml file), sending it to the server, deserialising there and building up a new copy of the given datatype (as defined in your DataContract), and doing something with it.
Your WCF service could well be self-hosted, e.g. running in a NT Service or console app - no HttpRequest available in those circumstances!
You need to definitely rearchitect your solution - you need to either check into WCF streaming to upload files to WCF (google for it - you'll find plenty of hits) or you'll need to find another way to pass the relevant info (e.g. list of filenames) to the WCF service without use of a HttpRequest object.
Marc
You are submitting a request as a parameter to a request. This is not what you want to do. I'm assuming that "Request.Files" is an array of files. This is what you want to upload. So something like:
IService.UploadPictures(List<SomeFileType> request);