Adding Routing to eliminate .SVC in REST service isn't working - c#

I am building a dummy WCF REST service just for purposes of learning how it works (preparing for real service build). I have the REST service working and responding with both JSON and POX formatting. However, I cannot get the routing solution to work in order to eliminate the ".svc" file. I am using VS 2010, WCF 4.0 and IIS 7.5 on Win Server 2008 R2.
Right now the URL works as: "/api/rest/rest.svc/json/myMethod" but I want to just have "/api/rest/json/myMethod". I have found numerous articles here on SO and elsewhere which claims to remove the ".svc" file. I believe I have it setup as instructed, but the project will not build because of an error in the Global.asax file.
It says to add the following to the Application_Start function:
RouteTable.Routes.Add(new ServiceRoute("", new WebServiceHostFactory(),
typeof(RestService)));
I also added the following to the web.config:
<modules runAllManagedModulesForAllRequests="true">
<add name="UrlRoutingModule"
type="System.Web.Routing.UrlRoutingModule,
System.Web.Routing, Version=4.0.0.0,
Culture=neutral,
PublicKeyToken=31BF3856AD364E35" />
</modules>
<handlers>
<add name="UrlRoutingHandler"
preCondition="integratedMode"
verb="*" path="UrlRouting.axd"
type="System.Web.HttpForbiddenHandler,
System.Web, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a" />
</handlers>
I also added the aspNetCompatibility lines to the web.config and above the class in the svc.cs file.
Th issue is that I can't even get the project to build. When I add the RouteTable.Routes.Add line to the global.asax and build it, I get the following errors:
The type
'System.ServiceModel.Activation.ServiceHostFactory'
is defined in an assembly that is not
referenced. You must add a reference
to assembly
'System.ServiceModel.Activation,
Version=4.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35'.
The type or namespace name
'ServiceRoute' could not be found (are
you missing a using directive or an
assembly reference?)
Any ideas why this is failing?

make sure that "System.ServiceModel.Activation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" is referenced in the project, if it is make sure to add this to the web.config file as well. – Joakim Mar 25 at 19:03

Related

asmx on an existing site gives me namespace not found

Been looking around but cannot find an answer. I wrote an asmx on VS2012 locally works good. Now deploying to the remote target site it is becoming an issue. On that site there are other asmx services and in their global web.config they have this section
...
<system.webServer>
...
<handlers>
...
<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=123456789" />
...
</handlers>
...
</system.webServer>
I placed my asmx on a subfolder defining an Application on IIS8 and I'm trying to overwrite that global setting with my local web.config, tried different permutations on the local web.config with location to avoid inheritance but so far it always gives me the lovely
Server error in '/my_svc' Application
CS0246: The type or namespace name 'ForeignNS' could not be found
Is it possible to override it by only changing my local web.config? If so how? If at all possible? Target site admin will not allow to change global web.config unless it is absolutely necessary.

Visual Studio Page Inspector Assembly not found

I have a ASP MVC Project which I didn't make but I need to upload it on a IIS server. This works but when I upload it and visit the website I get this error:
Could not load file or assembly 'Microsoft.VisualStudio.Web.PageInspector.Loader, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
Everything works locally but not on the server. Things I tried already:
Repairing .NET Framework
Trying to remove 2 lines from .NET Framework webconfig (Located in: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config) file but can't because file is in use. Can't stop process because of Access Denied error
I found that removing these 2 lines might be a solution but I can't remove them:
<remove assembly="Microsoft.VisualStudio.Web.PageInspector.Loader, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<add assembly="Microsoft.VisualStudio.Web.PageInspector.Loader, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
Is there something I am missing? Any help is much appreciated! Thx
Try adding just this line to your site web.config. The config files are inherited from the .NET base up to you site and then folders in your site. So Your site will override the .NET framework config.
<remove assembly="Microsoft.VisualStudio.Web.PageInspector.Loader, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
EDIT
The line should go here...
<configuration>
<system.web>
<compilation>
<assemblies>
<remove assembly="Microsoft.VisualStudio.Web.PageInspector.Loader, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</assemblies>
</compilation>
</system.web>
</configuration>
It should be noted that all that junk in the assembly attribute more than likely needs to be identical to the one that adds it in the .NET framework config.

Razor intellisense does not work in vs2013

The Razor intellisense stopped working in one MVC 4 project, and one project only on my pc. I use Visual Studio 2013 Professional. I know this is an issue that has happened to a lot of people before me. I've tried many possible solutions, but the one that works for me has not been there yet...
A list of what I tried so far:
Delete the temporary .suo file
In tools->options check Auto list members
Remove the project from the solution and add it back
Create a new Visual Studio MVC project and copy the web.config file
Delete old Visual Studio 2010 settings in AppData/Roaming/Windows/Visual Studio
Change MVC version in web.config file in Views folder
Clean the solution, exit VS, reopen it and rebuild entire solution
Unload and reload the project within the solution
and probably I tried more which I can't remember right now. Does anyone have another solution/tip to solve this?
Suggested answers for this post which didn't work either:
Repair Visual Studio and reinstall ASP .NET MVC
I've faced same kind of problem with different causes and solved as follow:
web.config in all views folders: I got a correct version of web.config creating a new Area in my project and using it in other view folders
Feature … cannot be used because it is not part of the ISO-2 C# language specification generally these kind of errors does not block compilation, but could be very annoying. To solve this error I've added this segment in web.config (or in app.config if your project is not a web project)
<system.web>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<!-- ASP.NET 4.0 Assemblies -->
<add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
<add assembly="System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
<add assembly="System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
<add assembly="System.Data.DataSetExtensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
<add assembly="System.Web.Extensions.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.WebPages, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
</compilation>
</system.web>
local assemblies: sometimes, when I've used external assemblies (for helpers e.g.) I've had to check for Copy Local = true in Project references.
In all cases It could be needed to close/reopen VS to get the modification applied.
hope it helps

why I got this in my web.config

I got these dlls in gac then why I got them in web.config as well,
<compilation debug="true">
<assemblies>
<add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=removedForPrivacyMyself"/>
<add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=removedForPrivacyMyself"/>
<add assembly="System.Web.Extensions.Design, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=removedForPrivacyMyself"/>
<add assembly="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=removedForPrivacyMyself"/>
</assemblies>
</compilation>
My application seem to work fine when i remove this section
These are just stored in the config file by default. It also means that you do not need to provide a
using System.Web.Extensions;
etc. in every source file.
During compilation, the compiler will use the <assemblies> section to figure out which assemblies to add into the compilation process. If you do put usings everywhere then indeed this section adds nothing.
Your web application project properly using those assemblies in the reference definition.
You can remove them, but remember if you going to use one of those assemblies your web application won't work.
Usually the visual studio add them automatically your web.config

System.DirectoryServices is not recognised in the namespace 'System'

I'm trying to use System.DirectoryServices in a web site project and I'm getting this error:
The type or namespace name 'DirectoryServices' does not exist in the namespace 'System' (are you missing an assembly reference?)
My project has a reference to System.DirectoryServices in web.config:
<add assembly="System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
And I do have using System.DirectoryServices in the files where I want to use it.
Does anybody have a clue where to look for the problem?
Right click on References under your solution.
Select Add Reference.
The reference can be found under the Framework Assemblies list.
Select System.DirectoryServices and click Add.
I think you should install Directory Services Package.
Install-Package System.DirectoryServices -Version 4.0.0
Directory Services Package
Shot in the dark: have you tried adding to the web.config:
<compilation debug="true">
<assemblies>
<add assembly="System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
</assemblies>
</compilation>
Is the web-server (IIS or whatever) configured to run the folder as an application (i.e. shows as a cog), and is it using the correct version of ASP.NET? If it is running as 1.1, bits of it might work - but it would fail to find that 2.0 assembly in the 1.1 GAC.
This is a very old thread but just to provide a complete answer for the sake of posterity ;)
This issue occurs if the project is missing a reference to the .Net Component System.DirectoryServices
Adding this reference in the usual manner prefered by you will resolve the issue.
Is this a web site project, or a web application project. With the latter, references are handled via the .csproj - i.e. via the "References" node in Solution Explorer.
On Solution Explorer right-click your project, then from the resulting menu, click on Add Reference, then under the .NET tab navigate to DirectoryServices.AccountManagement
These problems occur when you are working with older .net version and trying to build with the latest IDE
It depends on which version of IDE you are using and also the current code version.
Check the web config,
In my case, I was using the Latest version i.e 4.7 and directoryService assembly are still referring to C#4.0.
Add below if you are using Latest version of id i.e 4.7
<system.web>
<location>
<compilation debug="false" numRecompilesBeforeAppRestart="100" targetFramework="4.7">
<assemblies>
<add assembly="System.DirectoryServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
<add assembly="System.DirectoryServices.AccountManagement, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
</assemblies>
</compilation>
</system.web>
</location>
I had the same problem when I tried to convert website to web-app.
It looks like vs failing to load the assembly should be related to versioning.
switch to web.config and add the assembly to it as bellow. make sure the DLL version is matching your application targeted .NET version.
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.DirectoryServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</assemblies>
</compilation>
</system.web>
</configuration>
for getting a public key you need to launch Developer Command Prompt for VS. Change to GAC directory related framework on above ex C:\Windows\Microsoft.NET\Framework\v4.0.30319
and call
sn -T System.DirectoryServices.dll

Categories