Difference between IOperationBehavior and IContractBehavior? - wcf

I want to know what is difference between IOperationBehavior and IContractBehavior and what is special case when we can see difference between this two in development.

A WCF service can has multiple contracts.
IServiceBehavior - applies to all contracts and can be applied thru the config file or the attribute
IContractBehavior - applies only to one contract and can be applied only thru the attribute

Related

Utilizing multiple service contracts over the same WCF channel or session

I'm in the process of writing a duplex WCF service using NetTcpBinding, and I've run into an architecture question that I think I know the answer to, but hope that I'm wrong.
Our service is stateful, and we've selected NetTcpBinding with PerSession InstanceContextMode. For various reasons, this is something that we require. I'm trying to break up our larger interface (where large blocks of the operations would not apply to many clients) into multiple smaller interfaces with the operations logically grouped. While it's simple enough to have a single service implementation implement all of the contracts, I'm not sure if it's possible to have multiple service contracts share a single channel (or, more to my requirement, a single session), and I'd definitely need to be able to do that in order to make this work.
I could, of course, include everything on one contract and throw FaultExceptions when an invalid operation is performed, but I'd really like to be able to break these up and not even add an endpoint for inapplicable contracts. Is what I'm looking for possible?
TL;DR Version:
I need to be able to do this:
[ServiceContract]
public interface IServiceA
{
[OperationContract]
void Foo();
}
[ServiceContract]
public interface IServiceB
{
[OperationContract]
void Bar();
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class Service : IServiceA, IServiceB
{
...
}
And be able to establish one session from the client to the service but use both IServiceA and IServiceB.
The default instance provider over a sessionful channel will give you an instance per connection in your case. You can however extend the instance provider to pick up an existing object from your own cache and return the same object.
How you correlate instances will be upto you using some special message header etc. The underlying channel/Connection will be different for each proxy and also use differnt buffers / concurrency models but you can allow service model to use the same instance.
http://msdn.microsoft.com/en-us/magazine/cc163590.aspx

What is the difference between [DataContractAttribute(IsReference=true)] and [DataContract]

I'm trying to write a WCF Web service that will return my data as JSON so I can call it from some client script.
I know I need to decorate any classes I want to return from the Web Methods in a [DataContract] attribute and then any Members in a [DataMember]. That in mind I want to return entity types so I went to the Entity ObjectContext classes.
However when I look at the .edmx file I can see that the classes have been decorated like so...
[EdmEntityTypeAttribute(NamespaceName="PteDotNetModel", Name="AssocFile")]
[Serializable()]
[DataContractAttribute(IsReference=true)]
public partial class AssocFile : EntityObject
When I then try and add [DataContract] I get an error saying I can't have duplicated attributes. I'm confused whilst they are similar they are clearly different no?
The second part of my question is how I return entity types over a WCF service?
The two attributes are the same; the trailing "Attribute" can be excluded. From MSDN:
By convention, all attribute names end with the word "Attribute" to distinguish them from other items in the .NET Framework. However, you do not need to specify the attribute suffix when using attributes in code.

WCF namespace usage

Ok - straight forward and simple question (2 part) - looking for a simple answer.
In a WCF Service contract you can provide a namespace on your contract...
Question is this:
First - Why (for real...need solid details here) is the http:// model chosen for the contract namespace naming convention
Second - Once set, how do I leverage...like so?
this is in my host -
<endpoint ... contract="http://helloworld.com/example"></endpoint>
The namespace absolutely does not have to be an http:// URL, it only needs to be any valid URN. It's just about being a uniquely identifiable name. Check out this great article on the differences between URI, URN, URL if you're not familiar.
As for using it on the WCF client endpoint, you don't. That namespace just scopes the name of the interface in the WSDL. The address at which the service is ultimately hosted can (should) be completely different from the namespaces used when defining the contracts. In fact there could be several endpoints for the exact same contract.
In WCF client endpoint configuration you use the contract attribute which points to a .NET interface which can either be the same interface as your service, this assumes you're sharing your contract library between client and service, or a "replica" interface that is marked up with a ServiceContract that has the matching values from your service definition (this is what Add Service Reference/svcutil do).

WCF Web Service parameter

I have a WCF web service that implements a parameter List<ICustomObject>. In the generated client code, the parameter is List<Object>?!? How can I make it accept the List<ICustomObject>, as an interface, or do I have to use a concrete class? I have the concrete class marked as [KnowType] and [Serializable]
You should use concrete class, because you should set DataMember and DataContract for it to serilize it, and with interface you can't do this.
WCF works with anything that is expressible in XML schema - interfaces per se are not.
You need to define your lists to be lists of concrete types - otherwise the client will not be able to know what to do with the list and will fall back to a List<Object>.

how do you specify which concrete class is consumed by WCF service

My endpoints specify which interface is used to govern the WCF service. Because it's an interface, I can have multiple different concrete classes implement the interface's functionality. How do I specify which concrete class should be used for a given WCF service's endpoint?
How do you say for this endpoint, use this concrete class and for that endpoint, use that concrete class when both endpoints use the same interface?
The contract implementation type to use is defined when the ServiceHost instance is created. If you have multiple endpoints served by the same ServiceHost, it will create an instance of the same type to respond to requests to these endpoints.
If you want two endpoints for the same contract to use different implementations, you have two choices:
expose the two endpoints on two separate service hosts. (Either through creating two ServiceHost instances or specifying two <service> configuration elements);
have a composite implementation for the class that has two separate implementations of the same interface (through any OOP approach you can think of) and dispatches the calls to these based on the endpoint address.
I personally would stick with the first approach. If you go for the second approach, you would be essentially reimplementing ServiceHost yourself.