I have an MVC project to which I have added a folder in the root directory called WCF. Within this folder I have created a WCF service entitled CustomFunctions
. When I attempt to start the service I receive the following error:
Error: Cannot obtain Metadata from
http://localhost/Viper/WCF/CustomFunctions.svc
... Metadata contains a reference that cannot be resolved:
With an additional description:
The type 'Viper.WCF.CustomFunctions', provided as the Service attribute value in the ServiceHost directive, or provided in the configuration element system.serviceModel/serviceHostingEnvironment/serviceActivations could not be found.
I was getting this error yesterday and spent some time scouring the internet for an answer. This led me to make a lot of changes to my Web.config as well as my Global.asax.cs. At one point yesterday, it started working and I stopped. However, when I came back this morning it wasn't working all over again. Nothing new was added and no code was changed between that time.
I have added the following to my Web.config:
<system.serviceModel>
<services>
<service behaviorConfiguration="WCFBehavior" name="Viper.WCF.CustomFunctions">
<endpoint address="" binding="wsHttpBinding" contract="Viper.WCF.ICustomFunctions">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost/Viper/WCF/CustomFunctions/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WCFBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" httpGetUrl=""/>
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
And this to my Global.asax.cs
:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.svc/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional
}, // Parameter defaults
new { controller = "^(?!CustomFunctions).*" }
);
routes.Add(new ServiceRoute("CustomFunctions", new ServiceHostFactory(),
typeof(CustomFunctions)));
}
Can anyone help me? I'm all out of ideas here.
I've figured out the problem. First of all, I was missing a part of the path to the function that registers my routes. After fixing that path I was able to get my wsdl showing up in my hosting environment. However, this messed up the default routing for my areas. So for anyone who has this problem in the future, here is my solution:
I specified my custom route primarily so that when I navigated to the specified url then it would show my .svc file. I call this method from my ApplicationStart method in Global.asax.cs. I also had to create a seperate controller and view for my CustomFunctions within my Home Area so that it could differentiate between my default route and my CustomFunctions, and specified that in my route map as seen above. So when I go to localhost\Viper it will find the route specified in my default map and when I go to localhost\Viper\CustomFunctions it will find the route to my .svc file. The IgnoreRoute basically makes it so you don't have to put the file extension at the end of the url when calling your page. So rather than CustomFunctions.svc I only specify CustomFunctions. Make sure you add the System.ServiceModel.Activation assembly and using statement to your project when doing this.
Thanks everyone for your help. Hope this helps some other poor lost soul.