Skip to main content

WCF Dos and Don'ts

Do not inherit interfaces from other interfaces.

Interfaces are contracts. Contracts must be explicit, otherwise the flexibility and maintainability of your design would run into issues. An interface is a pure contract definition and should remain explicit and pure.

Consider the following example:
[ServiceContract]
public interface IMyService1
{

[OperationContract]

void Test1();

}

[ServiceContract]
public interface IMyService2 : IMyService1

{

[OperationContract]

void Test2();

}

[ServiceBehavior(…)]
class MyService : IMyService2, IMyService1

{

#region IMyService2 Members
public void Test2(){

}
#endregion
#region IMyService1 Members

public void Test1()

{

}
#endregion

}
What happens when you Publish your Service and open its communication channels. If you do the following:
ServiceHost serviceHost = new ServiceHost(typeof(MyService, …)ServiceEndpoint ep = serviceHost.AddServiceEndpoint(typeof(IMyService1), typeof(IMyService2));
You will get a nasty ambiguous message from WCF since you are trying to register the same contract twice; once implicitly with IMyService2 wince it inherits from IService1 and another time explicitly using IService1
In this case, you need to either do #1 (Still the wrong way) or #2(the right way)
#1: remove the IMyService1 from both the Service inheritance and implicitly implement IMyService1’s operation contracts and remove the IMyService1 from your service endpoints registration with WCF.

[ServiceBehavior(…)]
class MyService : IMyService2
{

#region IMyService2 Members
public void Test2()
{
}
#endregion
#region IMyService1 Members

void IMyService1.Test1()
{

}

#endregion

}

ServiceHost serviceHost = new ServiceHost(typeof(MyService, …)
ServiceEndpoint ep = serviceHost.AddServiceEndpoint(typeof(IMyService2));

In this case it is very ambiguous to anyone reading the code to know at a glance that your service publishes an IMyService1 as a service contract


#2 (the right way)

Just remove the inheritance of IMyService1 from iMyService2’s interface. That’s it.[ServiceContract]

public interface IMyService2

{

[OperationContract]

void Test2();

}


Comments

Popular posts from this blog

Visual Studio 2010 was released yesterday

Unfortunately, I have just checked the MSDNAA site and they still do not have the release version of Visual Studio 2010. They only have an old Beta2. I would recommend that if you do not have access to Visual Studio 2010 through other means, that you keep an eye on the MSDNAA web site and see if VS2010 becomes available. If and when it becomes available, I would not recommend that you install it while you are finishing CS 503. Download it and save it somewhere and when the course is done, you can knock yourself out :)

Wave Undo

To implement Undo, the simplest way is to use a MemoryStream object in your WaveDisplayForm (MDI Child) and simply serialize the Wave (which should be marked as [Serializable] to the stream and hold on to the stream object. private MemoryStream _undoStream = new MemoryStream(); private bool _canUndo = false; public void SaveForUndo() {      BinaryFormatter bf = new BinaryFormatter();      bf.Serialize(_undoStream, wave);      _undoStream.Flush();      // This rewinds the stream so that when it is used for deserialization, it's ready to use,     // otherwise, the deserialization will start deserializing from the end of the stream and would fail.      _undoStream.Position = 0;      _canUndo = true; }   public void Undo() {      BinaryFormatter bf = new BinaryFormatter();      ...

My Plans for this Blog

I intend to post new things I learn or run into or that come to mind on at least a weekly basis. Sometimes may be daily. I'm planning on starting after the course ends so I do disturb the peace of the final project. You have options to unsubscribe yourself from this Blog site completely and you are welcome to stay as you fell like it. It's all up to you. I also would be open and willing to respond to those tough questions you may run into assuming the volume of questions would be manageable...... Knowing you guys, I think it will be very quite in this neighborhood.