Tuesday, April 10, 2012

Error because of Adding Service Reference - SVCUTIL re uses all referenced libraries in the Project

Its common to come across the below error when adding WCF service reference,

Warning 1 Custom tool warning: Cannot import wsdl:portType Detail: An exception was thrown while running a WSDL import extension: System.ServiceModel.Description.DataContractSerializerMessageContractImporter Error: List of referenced types contains more than one type with data contract name '' in namespace 'http://'. Need to exclude all but one of the following types. Only matching types can be valid references: "Company.Depart.Proj.dll", Company.Depart.Proj, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" (not matching) "

XPath to Error Source: //wsdl:definitions[@targetNamespace='http://schemas..org/2012/04/service/user']/wsdl:portType[@name='IUserService'].

This happens because ServiceUtil is by default re uses all referenced assemblies in the project. And if any of the referenced assembly is not serializable or unable to reference then it will throw the above error.

To avoid this we can follow one of the below approaches,

Approach 1

Make sure that when using a service reference of a web service to a web application/any application , you follow below steps,

  1. After adding the service reference OR while adding the service reference , In Service Reference Settings Dialogue , check the Data Type section,

To Go to Service Reference Settings Dialog, Right click the service , Select "Configure Service Reference"

Machine generated alternative text: I i J App_WebReferences j  ExPxService eference.svcmap D Open Open With... Update Service Reference Configure Service Reference... Remove Service Reference Check In.. isePrescription_1.pdf View Pending Changes es ontrolToolkit.dll Undo Pending Changes... bContrib.Data.OdpNet.dll Shelve Pending Changes... net.dll 7E:. Hit’:r osoft.Practices.EnterpriseLibrary.Common.diI - --- osoft.Pradices.EnterpriseLibra.Data.dll - osoft.Pradices.EnterpriseLibra.Logging.dll osoft. Practices.ObjectBuilder2.dll Exclude From Project le.DataAccess.dll Cut Ctrl+X uonet.dll trois Copy Ctrl÷C e Delete Del Rename sax I ‘ !Ei Logln.aspx

That will open the Service Reference Settings Dialog,

Machine generated alternative text: ExPxService - Service Reference Settings L4J  Ï Client Address: http://localhost:49465/ExPxService.svc Access level for generated classes: L?h11k L] Generate asynchronous operations Data Type JI Always generate message contracts Collection type: [System.CollectionsGeneric.List Dictionary collection type: System.Collections.Generic.Dictionary Reuse types in referenced assemblies Reuse types in all referenced assemblies o Reuse types in specified referenced assemblies: J OAjaxControlToolkit -lJEmerio.ClientScriptManager Emerio.DataAccess QEmerio.Shared I!1 —tJEntLibContrib.Data.OdpNet —tHPBExPx.BusinessLayer.Mapper iJHPB.ExPx.BusinessLayer.Service aHPB.ExPx.Utility D OMicrosoft.Build.Framework D  Microsoft.Practices.EnterpriseLibrary.Common H 4jMirrncnft Prartirac Fntprnricpl ibran, flM  ___________________ OK j [ Cancel j

In this, Make sure that you have selected the option "Reuse types in referenced assemblies" -> "Reuse types in specified referenced assemblies".

By Default it will be "Reuse types in all referenced assemblies". It will look for all references you have added in the project, this is what causing error.

Approach 2

Another approach is to uncheck "Reuse types in all referenced assemblies" but when you are un checking this option, you may get compilation error as you might have references some dll's in Project. So before doing this you

Should be extra careful.

Machine generated alternative text: Client Address: http://localhost:49465/ExPxSer.ice.sc Access level for generated classes: [Tublic ri Generate asynchronous operations Data Type L Always generate message contracts Collection type: [system.Collections.Generic.List Dictionary collection type: [system.collections.Generic.Dictionary L Reuse types in referenced assemblies .‘ Reuse - ri C System.Web O System.Web.Abstractions ri  System.Web.DynamicData j C System.Web.Extensions ri O System.Web.Mobile ri O System.Web.Routing j -O System.Web.Services rio System.WorkflowServices Fi -OSystem.Xml Fi o System.Xml.Linq [ 0K ] [ Cancel j