Showing posts with label WCF Services. Show all posts
Showing posts with label WCF Services. Show all posts

Monday, July 9, 2018

WCF Error - SOLVED - Service Attribute value could not be found in ServiceHost directive


In this article we take care of the WCF Error  ' Service Attribute value could not be found in ServiceHost directive', after changing a WCF Service properties or configuration.


WCF Error - Service Attribute value could not be found in ServiceHost directive


 WCF Error - Service Attribute value could not be found in ServiceHost directive


The error is originated after you changed some properties on a WCF web service.
For example, you created a web service using a Visual Studio template, which sets a WCF service usually called "Service1".

You customized the service and, after everything seems OK, you realize that the name of the service  is inappropriate, and you change it and the class/interface behind it.
The web service stops working.
Now , the ERROR message says that the TYPE of the web service could not be found.
The clue here is that could not be found in the "ServiceHost" directive.
You take a look at the classes in your project, but can't see the Service Host:

WCF Error - Service Attribute value could not be found in ServiceHost directive

Perform a search inside your project, looking for the "ServiceHost", and add "@" to the search since we're talking about an MVC Directive:

WCF Error - Service Attribute value could not be found in ServiceHost directive
 You'll find a small HTML file with the Directive: this is the markup which enables a hosting of your web service at the development stage:


WCF Error - Service Attribute value could not be found in ServiceHost directive

As you can see, when you renamed your service, Visual Studio updated the change of the name at the CodeBehind attribute, but didn't automatically renamed the TYPE exposed by the web service, in our example "RestService" instead of "Service1":

WCF Error - Service Attribute value could not be found in ServiceHost directive

So, change the Service to the one you wish, and run it:

WCF Error - Service Attribute value could not be found in ServiceHost directive
 Now you get it in JSON WebMessageFormat. If you wanted XML, just don't set any ResponseFormat, and you'll get the XML default format for WCF web services:

WCF Error - Service Attribute value could not be found in ServiceHost directive

WCF Error - Service Attribute value could not be found in ServiceHost directive



That's all!!!!


By Carmel Shvartzman

עריכה: כרמל שוורצמן

Tuesday, June 19, 2018

Make a WCF RESTful Web Service with CRUD operations in 5 minutes

In this Step by step we Make a WCF RESTful Web Service with CRUD operations in 5 minutes 
   we see how to build a WCF REST web service with all CRUD operations to be Ajax called, all this   in just 5 minutes, following this simple steps :

1) Use Visual Studio templates and build an Ajax WCF web service
2) Customize it to support RESTful calls in JSON format (can also be XML)
3) Code all C.R.U.D. (Create Read Update Delete) operations


The whole code for this tutorial , can be downloaded from the following GitHub repository:
https://github.com/CarmelSoftware/WCF_RESTful_WebService
To build this service, we use a demo class which will be exposed by the REST service through HTTP calls, that you can later replace with your own EDM data model classes.
This an HTTP GET response from this RESTful WCF service :

Make a WCF RESTful Web Service with CRUD operations in 5 minutes
And this other is an HTTP PATCH request-response from this WCF REST service:

Make a WCF RESTful Web Service with CRUD operations in 5 minutes



Make a WCF RESTful Web Service with CRUD operations in 5 minutes


The whole process for making this REST service , is as follows :

1) Use Visual Studio templates and build an Ajax WCF web service:

First build an ASP.NET Web Application using this Visual Studio template:


Make a WCF RESTful Web Service with CRUD operations in 5 minutes



Then, add an AJAX enabled WCF web service to the project:


Make a WCF RESTful Web Service with CRUD operations in 5 minutes

You'll get the following usable skeleton for the WCF service:


Make a WCF RESTful Web Service with CRUD operations in 5 minutes


Take a look at the web.config file to see the WCF service configuration:

Make a WCF RESTful Web Service with CRUD operations in 5 minutes


As you can see , there is an endpoint which wears the name of the Web service class.
The "enableWebScript " behavior sets that the service will respond to AJAX calls.


2) Customize it to support RESTful calls in JSON format (can also be XML)

First thing we do, is to change the endpoint behavior to turn it into a REST service, using the "webHttp" directive, as follows:

Make a WCF RESTful Web Service with CRUD operations in 5 minutes

Notice that this directive replaces the "enableWebScript" one.

To make run this service, we design a demo class called "Data", which will be exposed by the REST service through HTTP calls:

Make a WCF RESTful Web Service with CRUD operations in 5 minutes

This data will be decorated with the DataContract directive, and the properties will be DataMembers.
We also add some constructors to create some records and return them by the service.
After you have your WCF RESTful service working, you will replace this demo class with some EDM data model classes of your own.
Remember to declare "serializable" your model.



3) Code all C.R.U.D. (Create Read Update Delete) operations

Then go to the WCF class and replace the "DoWork" method with the HTTP GET method to return ALL  items:


HTTP GET :

Make a WCF RESTful Web Service with CRUD operations in 5 minutes

We specify the handle for HTTP GET requests, by using the attribute "WebGet".
Also, we stipulate the response format as JSON, since the default is XML.
And we set an UriTemplate as "/Get" , which will differentiate this method from the second GET one, which will return only one item according to the ID, which you will code as follows:

Make a WCF RESTful Web Service with CRUD operations in 5 minutes
Notice that the return value is an array of Data objects and a single Data object, accordingly:


Make a WCF RESTful Web Service with CRUD operations in 5 minutes

Make a WCF RESTful Web Service with CRUD operations in 5 minutes


HTTP POST :

The POST HTTP verb is used to CREATE a new item, and we define it using the WebInvoke attribute:

Make a WCF RESTful Web Service with CRUD operations in 5 minutes

We only want to check that our REST service works, so that just return the same Data object from the request (here i'm using POSTMAN to send HTTP requests. You can also use FIDDLER for that):


Make a WCF RESTful Web Service with CRUD operations in 5 minutes

Important: the JSON object in the request's body must hold the field's data with exactly the same field names set in the DataMembers in your service: it is case-sensitive ("Id" means "Id", and no "id", got it?).

HTTP PUT:

The PUT HTTP VERB is used to update the entire item, i.e. to replace it with an updated one, and we do so by using the WebInvoke PUT option as follows:


Make a WCF RESTful Web Service with CRUD operations in 5 minutes
The binder will give you the Date object from the request, if the names of the fields being the same as the class in a case-sensitive way, and also will give you the ID of the item to update. Check that you wrote EXACTLY the same variable name for this : "Put/{id}" == string "id" .

Test this method from POSTMAN sending an PUT request with a JSON object in the body:


Make a WCF RESTful Web Service with CRUD operations in 5 minutes

Important: the JSON object inside the request's body must contain the field's data with exactly the same field names set in the DataMembers in your service: it is case-sensitive ("Id" means "Id", and no "id").


HTTP PATCH :

The HTTP PATCH verb is used in REST for updating just some of the object's fields:


Make a WCF RESTful Web Service with CRUD operations in 5 minutes


Again, test it using this REST call with the HTTP PATCH verb:

Make a WCF RESTful Web Service with CRUD operations in 5 minutes
HTTP DELETE :


Finally, create the method to handle HTTP DELETE requests, as follows:




Make a WCF RESTful Web Service with CRUD operations in 5 minutes




Make a WCF RESTful Web Service with CRUD operations in 5 minutes


THE END


That's all. Now you have a working WCF RESTful web service , and you can replace the Data model with your own.


      by Carmel Schvartzman


כתב: כרמל שוורצמן


Saturday, January 30, 2016

Step by step how to send an HTTP DELETE Request to an RESTful WCF Service using Fiddler

In this tutorial we'll learn how to create an HTTP DELETE Request to an RESTful WCF application using Fiddler
We'll use Fiddler for testing purposes of a RESTful WCF application, sending  HTTP DELETE requests. We'll start with a WCF application,  and we'll remove an entry using Fiddler, showing as follows :





First of all, we have to download the FREE Fiddler tool from this web site :



Now check the settings of the Operation Contract at the WCF Service Contract, in order to provide exactly what it is expected:



The WebInvoke is set to handle the DELETE HTTP method, and the request format is supposed to be JSON, the same as the response format. So, we'll send what the WCF wants.
Open Fiddler and find the "Composer" option:




Then type in the WCF service URL and select "DELETE" HTTP method from the list. Set the request Headers as follows: (the Content-Length  will be filled by Fiddler for you):



Fill the Request Body, and take care to fill the ID of the record to remove. The rest of fields is just optional.

Press the "EXECUTE" button  to send the request. If you set a breakpoint inside the WCF operation, you'll see the following in action :


As you see, WCF succeeded at identifying inside the JSON the ID to erase.
Then the response is sent to Fiddler, with a "true" value, because everything went fine:


And the database shows the deleted value:



If you want to see the RESTful WCF that we're using here, see this tutorial.

That's all.... 
In this tutorial we've seen how to send an HTTP DELETE Request to an RESTful WCF service using Fiddler. 
Happy programming.....
      By Carmel Shvartzman
כתב: כרמל שוורצמן








Saturday, November 29, 2014

Step by step how to send an HTTP PUT Request to a RESTful WCF Service using Fiddler

In this post we'll learn how to send an HTTP PUT Request to a RESTful WCF application using Fiddler
We'll use Fiddler to test an RESTful WCF application, sending an HTTP PUT request. We'll start with a working WCF application,  and we'll update an entry using Fiddler, showing as follows :



First of all, we have to download the FREE Fiddler tool from this web site :



After installing it , study the settings of the Operation Contract at the WCF Service Contract, in order to fill exactly what it is expecting as a request:
http put request to restful wcf using fiddler

The WebInvoke handles the PUT HTTP verb, and the request format must be JSON, and so the response format. Therefore, we'll send exactly what the WCF wants.
Open Fiddler and click the "Composer" option:




Then type in the URL and select "PUT" HTTP request method from the list. Also, set the request Headers as follows: (don't worry about the Content-Length, because Fiddler will fill  it for you):



Also, fill the Request Body :  be careful to fill adequately the field names of the Entity you want to update, most of all, the ID of the record to change.

Press the "EXECUTE" button at the Fiddler Composer, to send the request. If you set a breakpoint inside the WCF HTTP PUT handler, you'll see the bindings in action :


As you see, WCF succeeded at binding the JSON it got with the Blog object expected.
The response is then sent to Fiddler, with an "true" value:


And the database shows the updated values:



If you want to take a look at the RESTful WCF which we're using here, see this tutorial.

That's all... 
In this tutorial we've learned how to send an HTTP PUT Request to a RESTful WCF application using Fiddler. 
Happy programming.....
      By Carmel Shvartzman
כתב: כרמל שוורצמן










Monday, November 24, 2014

How to fix the WCF - Web API Error 'contains cycles and cannot be serialized if reference tracking is disabled'


In this article we see How to fix the WCF - Web API Error 'contains cycles and cannot be serialized if reference tracking is disabled'.
How to fix the WCF - Web API Error 'contains cycles and cannot be serialized if reference tracking is disabled'




 How to fix the WCF - Web API Error 'contains cycles and cannot be serialized if reference tracking is disabled'


This serialization error is originated when your Data Model contains an Entity with a property which references an object of the same type of the container class, or when you have many entities with relationships between them, and several of them are referenced by others in such a way that creates a circular self reference.
Let's say we have the following data Model , in which a "Blog" contains a reference to its "Blogger" , but this later owns many "Blogs":

How to fix the WCF - Web API Error 'contains cycles and cannot be serialized if reference tracking is disabled' 1

That means, if you open some Blog , and open its Blogger property, you will then see the Blogs that the Blogger owns, and , between them , the original Blog that you opened earlier, and that is a circular reference.
This can happen for instance, when you have a WCF service or a Web API which use eager loading:


How to fix the WCF - Web API Error 'contains cycles and cannot be serialized if reference tracking is disabled' 2

The MSDN web page for this "contains cycles and cannot be serialized if reference tracking is disabled" error  addresses only the first study case, in which an object reference itself: for example, a "Person" class contains a property referencing some other "Person":

How to fix the WCF - Web API Error 'contains cycles and cannot be serialized if reference tracking is disabled' 3

As it states, the solution is to add to the DataContract Attribute the "IsReference" boolean value set to "true". However , in most cases, you're prone to find the second XML-JSON serialization problem, in which several entities are related by ONE-TO-MANY relationships:

How to fix the WCF - Web API Error 'contains cycles and cannot be serialized if reference tracking is disabled' 4

In this case, just make a list of the classes that appear REFERENCED by other classes, that means, the entities which are at the " 0 .. 1 " side of any one-to-many relationship. In our example, there are ONLY two classes like that: Blogger and Blog, because "Comment" is not referenced by any other.
Therefore, open the classes files and add to the DataContract attribute the "IsReference" set to true:

How to fix the WCF - Web API Error 'contains cycles and cannot be serialized if reference tracking is disabled' 5

How to fix the WCF - Web API Error 'contains cycles and cannot be serialized if reference tracking is disabled' 6

This way, the reference tracking will be enabled and the circular references will be fixed.
If you like, it's possible to define "Metadata classes" and set the Attributes there: continuing with our example, that will be like this:
How to fix the WCF - Web API Error 'contains cycles and cannot be serialized if reference tracking is disabled' 7



After compilation-run, the WCF or WebAPI will render the data avoiding cycles, as follows:

How to fix the WCF - Web API Error 'contains cycles and cannot be serialized if reference tracking is disabled' 8


Here you can see that a "Blog" contains a "Blogger" property, and when it tries to reference the containing object, it will be use the reference " z:Id=i1 " . 
Same thing for the "Comments", while trying to reference the containing "Blog".

That's all!!!!


By Carmel Shvartzman

עריכה: כרמל שוורצמן