Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF Service Reference - proxy not being generated when reusing types

Tags:

c#

.net

wcf

I have two C# (.NET v4.0) companion projects, a ASP.NET website and a "self-hosted" WCF Console application. The website code calls the WCF service for various things; both were written a couple of years ago, and have been working fine.

Both the console app and web site share a common class library between them (called MyLibrary in this example); this is added as a reference to both projects.

The console app is pretty simple - for example, I have IService.cs:

[ServiceContract]
public interface IService
{               
    [OperationContract]
    List<MyLibrary.MyClass> DoSomething(int ID);
}

and then Service.cs:

public class Service : IService
{
    public List<MyLibrary.MyClass> DoSomething(int ID)
    {
        // etc
        return result;
    }
}

... and all of this works. However, today I've needed to add something trivial to both sides - one of the methods needs to have another parameter added. I've updated the WCF app, built it, ran it, and then gone to the website and tried to "Update Service Reference".

At this point, the site broke: generating the service reference is no longer creating a proxy. However, if I deselect the option to "Reuse all references", the proxy is created: this breaks my code massively though, I need to share the references to MyLibrary all through the code.

After doing some digging, I've attempted to create the reference using svcutil:

svcutil /t:code http://localhost/MyService /r MyLibrary.dll

which throws the error:

Attempting to download metadata from 'http://localhost/MyService' using WS-Metadata Exchange or DISCO. Error: Cannot import wsdl:portType Detail: An exception was thrown while running a WSDL import extension: System.ServiceModel.Description.DataContractSerializerMessageContractImporter    
Error: Referenced type 'MyLibrary.MyClass, MyLibrary, Version=2.0.53
33.26816, Culture=neutral, PublicKeyToken=null' with data contract name 'MyLibrary.MyClass' in namespace 'http://schemas.datacontract.org/2004/07/MyService' cannot be used since it does not match imported DataContract. Need to exclude this type from referenced types.    
XPath to Error Source: //wsdl:definitions[@targetNamespace='http://tempuri.org/']/wsdl:portType[@name='IService']

I cannot figure out what's gone wrong; I've seen other posts about this error, but they all mention making sure the referenced class is the same - the WCF app is using the same code in MyLibrary as I'm calling with svcutil above; both are being compiled at the same time. Also, I'm not doing anything differently that I haven't done dozens of times before.

Can anyone suggest where to start troubleshooting this issue? Apologies in advance if I've mangled any of the syntax in my code examples during obfuscation :)

like image 772
KenD Avatar asked Aug 13 '26 16:08

KenD


1 Answers

I've inadvertently stumbled over the answer: if I uncheck the "Always generate message contracts" option, and then "Update Service Reference", everything goes back to normal.

image

I'm certain I should really be using a DataContract (as mentioned in the comments on my OP), but I'll take the quick win and hide my ignorance for another day!

like image 57
KenD Avatar answered Aug 15 '26 05:08

KenD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!