I do not know if you need a solution. Below I will do. The trick here is not to use the standard “add service links” as suggested by many blogs, but rather to create your own client proxy using Channel Factory. In this case, you can reuse your interfaces, but redefine specific classes if necessary. We are pleased to specify if you need.
// Service Contract [ServiceContract(name="a", namespace="b")] public interface IFoo { Bar DoesStuff(); } // Interface to share public interface IBar { string Thingo { get; } } // Server Implementation public class Bar : IBar { string Thingo { get; set; } } // Client Proxy reference IBar interface only but redefine concrete class Bar. public class Bar : IBar { public string Thingo { get { return _thingo; } set { _thingo = value; } } string _thingo; } /// Sample channel factory implementation using System; using System.Configuration; using System.ServiceModel; using System.ServiceModel.Channels; public abstract partial class ServiceProxyBase<TServiceContract> : IServiceProxy where TServiceContract : class { protected ServiceProxyBase() : this(null, null) { } protected ServiceProxyBase(string url, Binding binding) { var contractName = typeof(TServiceContract).Name; var urlConfiguration = string.Format("{0}_Url", contractName); var serviceUrl = url ?? ConfigurationManager.AppSettings.ValueOrDefault (urlConfiguration, string.Empty, true); if (serviceUrl.IsNullOrEmptỵ̣()) { throw new Exception(string.Format("Unable to read configuration '{0}'", urlConfiguration)); } var serviceBinding = binding ?? new BasicHttpBinding(); Factory = new ChannelFactory<TServiceContract>(serviceBinding); var serviceUri = new Uri(serviceUrl); var endPoint = new EndpointAddress(serviceUri); Channel = Factory.CreateChannel(endPoint); } public virtual void Abort() { isAborted = true; } public virtual void Close() { if (Channel != null) { ((IClientChannel)Channel).Close(); } if (Factory != null) { Factory.Close(); } } private ChannelFactory<TServiceContract> Factory { get; set; } protected TServiceContract Channel { get; set; } private bool isAborted = false; } public class FooServiceProxy : ServiceProxyBase<IFooServiceProxy>, IFooServiceProxy { public Task<Bar> DoesStuffAsync() { return Channel.DoesStuffAsync(); } } [ServiceContract(name="a", namespace="b")] // The trick when redefine service contract public interface IFooServiceProxy { [OperationContract] Task<Bar> DoesStuffAsync(); }
source share