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

Track Files in Visual Studio

By default, Visual Studio’s Solution Explorer will update its selected item based on the currently active document. This is extremely annoying as it keeps expanding your projects and folders until your solution explorer becomes unmanageable and unusable, unless you collapse all Projects (Only available if you have Visual Studio Power Commands) installed) and even then, after collapsing the entire solution, the saga returns back. Turn off Active tracking 1. Turn your Active tracking off by going to Tools > Options 2. Select “Projects and Solutions” node 3. Uncheck the “Tack Active Item in Solution Explorer” Add a macro to do on-demand tracking 4. Tools > Marcos > Macro Explorer 5. You will see “MyMacros” Module (Node). Right mouse click on MyMacros and invoke “New Module…” 6. Name it anything you like. I Named it Utilities 7. Right mouse click on Utilizes and invoke “new Macro…” menu. The Macros IDE window opens. 8. Paste in the following into it within the Publ...

Google Instant search

Google Instant Search is the worst feature ever!!!. I could not for the life of me find a PERMANENT way to turn it off. Every time I turned it off using different means, it always came back. This is the only link I found useful so far, but the suggestion is not that great. It's OK. http://www.google.com/support/forum/p/Web%20Search/thread?tid=2a8bc9c3770bc11b&hl=en It is so annoying and so intrusive, every time I type a character it interrupts me and then I end up because of the time lag with a different string in the search box than the one I intended to type. Are they hiring a whole new set of STUPID generation at Google. This does not take a rocket scientist to figure out that this feature is useless. Even if they believe that is has some usefulness, let me (the customer, the user) decide. Anyone knows how to turn this stupid feature off?

Reduce Windows Folder Size

I just realized that my Windows directory has over 34 GB of stuff. What the heck!!! When you have Office, a couple of versions of Visual Studio, and few other Microsoft programs, you basically have already gobbled up about 80 GB of hard disk space. I found this online and found it to be useful and effective. It shaves off about 8 GB of your Windows folder. Still not what I had expected but I can definitely use 8 GB for other important documents. I'm just puzzled at what is Micros soft putting in an Operating system folder? huh? 28 GB of crap? Open a command prompt (make sure you either have Admin privileges). If you do not have Admin privileges, then open the command prompt as Admin. dism /online /cleanup-image /spsuperseded /hidesp