简体   繁体   中英

Sharing Interfaces Between a WCF Service and Client (marked w/ ServiceContract)

I'm using VS2010 and .NET 4.0.

I've checked here ( Sharing Interfaces that are implemented in WCF Service ) but it differs in that what I'm trying to accomplish involves an interface that is marked with ServiceContract.

Ie in the service, I have an interface A (marked as a ServiceContract) and an extending interface B (also marked as a ServiceContract). B is implemented by the WCF service.

The exact extension goes along the lines of this:

public interface A<T>

public interface B : A<SpecificType>

("SpecificType" is marked as a DataContract.)

Obviously, B is exposed to the WCF client in the proxy-generating process; however, I need to expose interface A, as well (I'm implementing a semi-generic pub/sub system and the "publisher" needs to be able to check/rely on interface A).

The first way I tried to solve this was by creating a separate, "shared" assembly that contained the interface and could be used by both the service and the client; however, that ended up not working so well because, in the publisher, it needs to ensure that an instance of B is, in fact, properly extended from A. This implicit conversion fails, likely because the service reference doesn't seem to entirely jive with the "shared" assembly.

To get around this, I manually edited the Reference.cs file and it ended up working (I added the definition of interface A and made sure interface B referred to it properly). But this poses a large problem in that every time I update the service reference, this code will get wiped out.

Having looked at other WCF responses on this site and others, I can't quite seem to find an exact answer (perhaps I just didn't get through all of them and their responses).

If someone could point me in the right direction, I'd appreciate it.

Thanks.

Provide access to 'raw' service contracts interfaces to client (assembly or link to .cs file). Then you have two options:

1) Wrap autogenerated proxy calls into your own class method calls (implement interfaces as you want). Service reference updating won't hurt you.

2) Don't use autogenerated proxy. Create your own proxy (implement your interfaces):

var baseAddress = "net.tcp://localhost:4503/MyService"; 
var channelFactory = new DuplexChannelFactory<IMyService>(new InstanceContext(new MyNotificationReceiver()), //MyNotificationReceiver - WCF callback implementation class
    myNetTcpBinding,
    new EndpointAddress(baseAddress));

var proxy = channelFactory.CreateChannel();
proxy.MyMethod();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM