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?
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With