David Hollowell - MSFT
David Hollowell - MSFT

Reputation: 1065

Adding a service reference in ASP.NET MVC 4

I have two projects: Mvc3TestSvcRef & Mvc4TestSvcRef. Mvc3TestSvcRef is from the ASP.NET MVC 3 template for an intranet application. Mvc4TestSvcRef is from the ASP.NET MVC 4 template for an intranet application.

I'm trying to add a service reference. In Mvc3TestSvcRef, I right-click the project (or the References folder) and choose Add Service Reference. I point to the URL, click Go. When the reference is resolved, I enter a namespace and click OK. As expected, I see the section added to config with the bindings and client tags completed. I can import: "using Mvc3TestSvcRef.MySvcRef;" And write code like:

        using (var cl = new MyServiceClient())
        {
            cl.DoStuff();
        }

In Mvc4TestSvcRef, I follow the same steps, but there is no system.servicemodel added to config. Additionally the import: "using Mvc4TestSvcRef.MySvcRef;" cannot be resolved. I've tried this for MVC 4 from both Visual Studio 2010 and Visual Studio 2012.

Was there a major change to the process for adding service references in ASP.NET MVC 4 project type, or am I missing something or have corrupt install?

Upvotes: 9

Views: 16887

Answers (2)

David Hollowell - MSFT
David Hollowell - MSFT

Reputation: 1065

There was no code in Reference.cs, just comments:

//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.17929
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

I copied the reference.cs from the project that worked and modified the namespace, then added the section from the working project into the MVC 4 project and was still having an issue.

I tried to build and I got several warnings and an error. Failed to generate code for the service reference 'MySvcRef'. Please check other error and warning messages for details.

That led me to this article: Service Reference Error: Failed to generate code for the service reference

So I unchecked the Reuse types in all referenced assemblies from the Advanced section.

This seems to have generated a good service reference. Although, I should point out that if you have something in say System, like System.TimeSpan for example, that is used as a DataMember in one of your DataContracts, the reference will now have TimeSpan in the reference namespace, not from it's origin. So, the client would see any System.Timespan properties as ReferenceNameSpace.Timespan, which may throw off comparisons and such. The better answer here is to include specific assemblies from the reference and don't check the box for System.Web.Http, as pointed out in the comments below

Upvotes: 16

user2429458
user2429458

Reputation: 81

I don't know if it is too late, but here is the solution "When you add the reference, on advanced setting remove the reuse types checkbox."

Upvotes: 8

Related Questions