问题
Visual Studio and SvcUtil can be used to generate proxy code to integrate with a service. In VS 2010 and prior there were issues if you wanted to maintain a cross tier reference library of shared types. This forced several workarounds to address the issue of equivalence in the data contract types and the inability to properly use the local types.
URL Reference to Issue: WCF Client Code Generation - Issue with "Reuse types from referenced assemblies"
I am using Visual Studio 2012, ASP.NET 4.5, C# code
My Question: "Has the reuse of types across assemblies been fixed in VS 2012?" I am porting over some code now but am also concerned that this bug may rear its head. I can run test cases, but it would be faster if someone had an answer already. It has been my experience that if you can not seem to find the answer online (googled it and keep getting 2011 - problem still exists) that the a fix may not be in place.
My Goal: Allow my future development team to reuse the same types library across the corporate .Net application tiers and layers [Presentation (Website, Mobile App layer - server side, ...), Domain (Services, Business Logic Layer, Data Access Layer)]. I would like to ensure some uniformity and code reuse. Code will be as "loosely coupled" as possible in each layer, but types will be ensured via the reference assembly. Equally, I want the code to support external integration for third parties down the road. Thus my need to construct all proxy types from DataContractAttribute decorated types for outward facing services and maintain referenced types for my server side applications.
Am I going to run into any quagmires here? Is the issue in the link above been addressed? Please advise.
回答1:
The bug which you report as existing in Reuse types from referenced assemblies option happens because by specifying re-use VS calls svcutil.exe under the hood with the /r flag.
However, svcutil.exe uses DataContractSerializer
to help generate code and unfortunately this has a rather strict set of rules when it comes to parsing your service contracts.
So unless you service XSDs adhere to this set of rules svcutil.exe will switch to use the XmlSerializer
, which doesn't support the /r flag (or re-use). Hence you will not be able to re-use types.
If you can reference the actual service contract types (via binary reference) this is a much better solution as you can do away with service references all together. You can also use WSCF.blue to generate your service contracts, as this has it's own custom serializer and supports re-use of types.
回答2:
In my situation both WebService and WebApp referenced the same assembly containing domain entities. Naturally, each entity was decorated with DataContractAttribute but when generating a ServiceReference in WebApp using an endpoint exposed by the WebService the Reuse Type in Referenced Assemblies
had been seemingly ignored by VS2012, which resulted in additional copies of the types in the local assembly. Then (after several hours of trial and error) I added a Namespace parameter to the ServiceContractAttribute of my interface in WebService. Once added, the reparsed ServiceReference started to reference my shared
DataContract types as desired.
[ServiceContract(Namespace="http://www.example.com/Demo.WebService/")]
public interface IConfigurationService { ..methods here.. }
来源:https://stackoverflow.com/questions/13974470/wcf-and-shared-reference-library-between-client-service