Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 370
David Hollowell - MSFT Avatar asked Aug 20 '12 22:08

David Hollowell - MSFT


People also ask

How can add service in ASP NET MVC?

Right-click on the created ASP.NET MVC Application and click Add Service Reference, as shown below. Now, after clicking on the preceding Service reference option, it will show the Window given below. Now, click on advanced button.

How do I add an existing service reference?

In Solution Explorer, right-click the name of the project to which you want to add the service, and then click Add Service Reference. The Add Service Reference dialog box appears. In the Address box, enter the URL for the service, and then click Go to search for the service.


1 Answers

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

like image 184
David Hollowell - MSFT Avatar answered Oct 13 '22 00:10

David Hollowell - MSFT