WCF with no service xml or endpoints? What is this magic?

I had to write a WCF service for a project and I hadn’t created a WCF service for a while.

I was trying to write mine to fit in with the rest of the projects but I was totally confused because I was looking at the other WCF services in the project and looking to copy the shell and modify (like all good developers do:-))

I could see there was a SVC file, I could see the code but in the web.config there was no service xml section for the WCF service.

I trying to look at the url for the SVC to see if I could see the wsdl but when I did I got the message no endpoint found.

no endpoint found

 

WHAT WAS GOING ON.

Sometimes as a developer, when you haven’t worked on an area for a while, you have a horrible feeling they have changed the way it worked to some completely new method and framework a bit like the way web services were replaced with WCF.   but this wasn’t the case, it was CRM 2011 project (not that makes any difference calling a WCF service).

The most puzzling aspect was the WCF code was being called (I called it myself to make sure it worked) from a Javascript form.

I asked a developer and he said it must be a factory service, I must have had a completely blank look on my face because he then offered to show me.  A good tip is if you don’t understand something ask someone to point you in the right direction because you can save yourself days of puzzling.

He said right click on the SVC file in Visual Studio and choose View Markup
 
You will see this
mine was like this
<%@ ServiceHost Language=”C#” Debug =”true” Service=”Hosk.HoskRibbonService” CodeBehind=”HoskRibbonService.svc.cs %>
he said change it to this
<%@ ServiceHost Language=”C#” Debug =”true” Service=”Hosk.HoskRibbonService”  Factory=”System.ServiceModel.Activation.WebServiceHostFactory” %>

I removed the service xml section in the web.config and then as if by magic it worked and I was able to call the wcf service using jsonp call (cross domain) in javascript.

Not only had I never heard of using WebServiceHostFactory to create a WCF but I had never come across it in any of the code I have worked with.

I was interested to learn a bit more about it,  It’s a way to dynamically create services and it automatically creates the endpoints for the service and was added in .NET 3.5

 

Here is MSDN class

http://msdn.microsoft.com/en-us/library/system.servicemodel.activation.webservicehostfactory(v=vs.110).aspx

 

this flashcard gives a good quick explanation

http://www.brainthud.com/cards/5218/25206/what-is-the-purpose-of-the-webservicehostfactory-class-and-how-is-it-used

Question

What is the purpose of the WebServiceHostFactory class and how is it used?
Answer

When hosting a web service in IIS, the WebServiceHostFactory class can be used to automatically configure the endpoints for the service. Endpoints will be configured to use WebHttpBinding and WebHttpBehavior. The WebServiceHostFactory class will automatically create a WebServiceHost class when the web service is activated. The following example demonstrates how to reference the WebServiceHostFactory in a .svc file:

<%@ ServiceHost 
    Service="OrderService" 
    Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

 

 

Here are some simple tutorials if you want to learn more

http://dotnet.dzone.com/articles/creating-restful-wcf-service

http://agilewarrior.wordpress.com/2010/12/19/how-to-create-a-simple-wcf-rest-service/

http://www.c-sharpcorner.com/uploadfile/dhananjaycoder/rest-eanbaled-service-in-wcf-tutorial/

 

Picture from http://daarken.com/blog/2012/08/11/lotr-magic-and-other-news/

Advertisement

2 thoughts on “WCF with no service xml or endpoints? What is this magic?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.