views:

756

answers:

2

I have four seperate projects:

-MyUserControl (Needs a reference to a service implementing IMyService)

-MyService (Implements IMyService)

-MySharedInterfaces (IMyUserControl and IMyService)

-MyWebApp

The user control needs to be dynamically loaded at runtime. This implements IMyUserControl and has a property of type IMyService which will be set at runtime.

The trouble is even with the option to reuse types the WebApp isn't reusing the IMyServiceType, it always generates it again from the Service Reference. This wouldn't be an issue if I could cast it to MySharedInterfaces.IMyService (which I can't understand, since it should be exactly the same).

The user control is expecting something of type IMyService, is there anyway to either cast the WebServiceReference.IMyService back to MySharedInterface.IMyServiceReference or force the WebServiceReference to reuse the MySharedInterface.IMyServiceReference?

Thanks

+1  A: 

Have you included a reference to MySharedInterfaces in MyUserControl? Does MySharedInterfaces have any references to other assemblies, which are not referenced in MyUserControl?

The hard way is not using the generated Service Reference and to use ChannelFactory. This gives you always the interface from MySharedInterfaces.

Sven Sönnichsen
Thanks for the quick reply. The MyUserControl project references MySharedInterfaces, and there's nothing referenced by MySharedInterfaces which isn't referenced by MyUserControl. At the moment it's just a proof of concept, so it's pretty empty at the moment.Last week was the first time I've looked at WCF, so was hoping to stick to the easy stuff to start with. I'll have a read up on ChannelFactory though. Just out of curiosity, why can't it reuse the IMyServiceInterface when it can reuse everything else?
Matt
+3  A: 

Matt, you could definitely do the "two-step" process of creating the client-side proxy yourself - it's really not a biggie.

In your client app, reference the MySharedInterfaces assembly. Then, create an instance of the ChannelFactory<T> for your service interface:

ChannelFactory<IMyService> factory = new ChannelFactory<IMyService>();

This basically creates a factory class which is then capable of creating actual communcations channels between your client and your server. Since it requires the service contract, this method only works if you can share the service contract assembly (which you can and want, in your case).

This is a fairly time- and resource-expensive step, so if ever possible, try to stash the factory somewhere and reuse it instead of continuously re-creating it.

Given your channel factory, you can now create your actual communication channels, which is basically equivalent to your proxy client that gets generated by Add Service Reference:

IMyService client = factory.CreateChannel();

This is not a very expensive operation, so you could do this every time before making a service call, and not worry about faulted channels and so forth.

The client also implements the ICommunicationObject interface, so if you need to check something WCF related, like the state of the channel, you can cast your client:

CommunicationState currentState = ((ICommunicationObject)client).State;

So you basically really have all the bits and pieces of your generated client proxy class, but you have more control over what you're doing.

marc_s
Thanks for the code samples, works perfectly.
Matt