WCF Service Reference generates its own contract interface, won't reuse mine

假如想象 提交于 2019-11-27 11:40:20

"Reuse types in referenced assemblies" only allows you to reuse Data Contracts, not Service Contracts. If you want to share Service Contracts, you don't need to use "Add Service Reference" at all. You can just use ChannelFactory directly.

// Supply the binding and address in code
Binding binding = new BasicHttpBinding();
EndpointAddress address = new EndpointAddress("http://tempuri.org/address");
IServiceContract channel = ChannelFactory<IServiceContract>.CreateChannel(binding, address);

// Or read them from the config file
ChannelFactory<IServiceContract> channelFactory = new ChannelFactory<IServiceContract>();
IServiceContract channel = channelFactory.CreateChannel();

The channel object will also implement ICommunicationObject, so you can cast it if you need to call methods like Open() or Close().

When you create the service reference, there is a box you can tick to make it reuse the shared definitions. Make sure the client project is already referencing the shared assembly, add the service reference again, and check all the options carefully.

If it still doesn't work, check the binding you use. I have a vague recollection that basic HTTP binding won't support re-using of types?

Visual Studio does not support reusing you existing interface when generating the proxy classes for you. Reuse types will not reuse the contract interface as Quartermeister pointed out.

We have solved it with inheritance. Quite similar to the partial class idea above suggested by Jester Software.

This is how we solved it:

In the project of your client just create a service reference as you would have done. Then add a class that serves as the replacement for the client:

internal class MyServiceProxy : MyServiceClient, MyLogicNamespace.IMyService
{}

This class inherits from the generated MyServiceClient but states that that client does implement the original interface.

(I suggest you put them in a folder named "ServiceProxies")

If the MyServiceClient class contains any methods that do not match with the original interface then you can add them in that proxy and do the conversion in code.

After this, just use the MyServiceProxy where you would have used MyServiceClient.

There is another good option, if you want to continue to use the proxy generator for it's limited-but-somewhat-useful functionality... Use a partial class:

namespace <same namespace as generated proxy>
{
    public partial class MyClient : <namespace of "real" service contract>.IServiceContract
    {
    }
}

Ensure that the proxy is generating code the same way your Service Contract is defining it, ie, if it's using 'List', use that option in Configure Service References as well. In other words, make sure your generated Service Interface is exactly equal to your real Service Interface and the above code should work, and to update the reference you use right-click instead of writing code.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!