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.
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. Igood 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
this flashcard gives a good quick explanation
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
Filed under: CRM 2011, visual studio, WCF, Web Applications Development with Microsoft .NET 4.
