-->

ASP.net re-use types between service references

2019-07-19 04:10发布

问题:

I have two service references that I am importing into my asp.net project by importing a WSDL for each. The first reference is a default reference from my web service (Acumatica) and the second is a custom endpoint service I created in the original web service.

The problem is that both of them use the same "base types" in their object classes so when I include both namespaces to use the objects from both of them in my code, I get an error saying there ambiguous references between the base types in the two namespaces. Makes sense. But how can I set this up so that .net knows to re-use the base types between the two namespaces since they are exactly the same?

To show what I'm talking about:

You can see there are types that are exactly the same between the references. Can I configure these references to re-use those types between the two of them to get rid of my ambiguous reference errors?

回答1:

Unfortunately, there's no way of achieving what you want.

The thing is, the base classes used are not the same (they have different attributes which leads to different XML (de)serialization). To add insult to injury, when we researched this issue, we've found out that WCF client utility which you use to generate client-side code does not support reuse when XMLSerializer is used, so even if Acumatica made the base classes exactly the same, there still wouldn't be any reuse on client.

But why do you need to different endpoints simultaneously? If your own endpoint is only adding functionality to Acumatica's Default, why not consider extending Default? This way you'd need only one endpoint in your code.



回答2:

Create a class library (e.g. "MySharedContracts") where you place all the types you want to use in the web services. You can reference that same class library in your web service projects and in the ASP.net project that consumes the web services.

Then, in the ASP.net project when importing the WDSL definition, click on "Advanced" to open the "Service References Settings" dialog. In here, enable "Reuse types in references assemblies". You can then either let VS search for fitting types by itself by selecting "Reuse types in all references assemblies" or alternatively specify a set of assemblies that you want to use types from for the web services.