9

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?

David Hollowell - MSFT
  • 1,065
  • 2
  • 9
  • 18
  • FYI, WCF service references are one of the few exceptions to the rule about `using` blocks. Don't do that. See http://thorarin.net/blog/post/2010/05/30/Indisposable-WCF-clients.aspx and http://stackoverflow.com/questions/1155815/service-close-vs-service-abort-wcf-example – John Saunders Aug 21 '12 at 14:53

2 Answers2

16

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

Community
  • 1
  • 1
David Hollowell - MSFT
  • 1,065
  • 2
  • 9
  • 18
  • 1
    I had the same issue. What if I want to reuse types? After some investigation I realized that when I uncheck reuse types for assembly System.Web.Http no code is generated. and when I do check it, regardless of any other assemblies to be checked or not, complete code is generated and nothing is reused. by the way I'm using VS 2012 update 1 and MVC 4. – Mahmood Dehghan Dec 15 '12 at 08:39
  • yeah, I'm only having this issue with the MVC 4 templates. It seems to be working elsewhere. I've been able to generate reference.cs without reusing any types, but I've also been able select re-use types for specific assemblies. So far, I've used System & System.Xml.Linq. I'm not sure which specific assembly causes reference.cs to fail to generate entirely or not, but this is still a hassle. – David Hollowell - MSFT Dec 15 '12 at 15:41
  • Just notice Mahmoodvcs pointed out System.Web.Http should not be reused specifically. Good catch. – David Hollowell - MSFT May 28 '13 at 21:56
8

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."

user2429458
  • 81
  • 1
  • 1