2

I have two WCF services on my project. Services are sharing one type, but each service generating its own type. Is it possible to generate one class for both services?

Sample code for server side:

[DataContract]
class MyClass { /*some properties*/ } 

[ServiceContract]
public interface IService1 
{
    [OperationContract]
    MyClass GetSomeValue();
}

public class Service1 : IService1
{
   public MyClass GetSomeValue() { /*some logic*/ }
}

[ServiceContract]
public interface IService2 
{
    [OperationContract]
    MyClass GetSomeOtherValue();
}

public class Service2 : IService2
{
   public MyClass GetSomeOtherValue() { /*some logic*/ }
}

On client side two "MyClass" are generating for each service:

namespace Services.Service1Reference {
[System.SerializableAttribute()]
public partial class RSTRole : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged 
{
}

and

namespace Services.Service2Reference {
[System.SerializableAttribute()]
public partial class RSTRole : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged 
{
}
Samvel Siradeghyan
  • 3,523
  • 3
  • 30
  • 49
  • Just to confirm - you are consuming two different services from a single client. The services themselves have a common assembly and you would like this to be shared across all 3 systems? – StuartLC Dec 22 '14 at 15:04
  • How are you generating your proxy classes? Visual studio or svcutil? – KornMuffin Dec 22 '14 at 15:15
  • Just to make sure you know: On the client side, only a stub is used - methods and stuff are not "transferred" to the client. Of course you will get two stubs containing the same properties for `MyClass`. I suggest to use `StuartLC's` solution of re-using types. – Thorsten Dittmar Dec 22 '14 at 15:27
  • @KornMuffin I am generating proxy with Visual studio – Samvel Siradeghyan Dec 22 '14 at 16:05
  • Issue I had was because of wrong architecture (for our application). I changed services so there is no more need to have same type with two services on one client. – Samvel Siradeghyan Dec 23 '14 at 14:11

1 Answers1

1

This requires a few changes

  1. You'll need to reference same actual assembly used to contain the Shared Entities (DTO's) on the two services directly in the Client assembly (i.e. add this assembly as a reference the same project to which you are adding the WCF Service References). As per Dimitri's comment, it makes sense to refactor the shared entities into a small assembly just containing the DTO's, if you haven't done so already.

  2. On the client, you'll need to select Advanced when adding the service reference, and then select Reuse Types in Referenced Assemblies, as per the following:

Service reference with Shared Type

Community
  • 1
  • 1
StuartLC
  • 104,537
  • 17
  • 209
  • 285