I have four separate projects:
MyUserControl - Needs a reference to a service implementing
IMyService
MyService - Implements
IMyService
MySharedInterfaces - Contains
IMyUserControl
andIMyService
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, MyWeb开发者_开发知识库App isn't reusing the IMyService
interface. 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.IMyService
or force the WebServiceReference
to reuse the MySharedInterface.IMyService
?
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.
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.
精彩评论