WCF Essentials - Applying the ServiceContract attribute
(Page 4 of 4 )
WCF lets you apply the ServiceContract attribute on an interface or on a class. When you apply it on an interface, some class needs to implement the interface. In general, you use plain C# or VB to implement the interface, and nothing in the service class code pertains to it being a WCF service:
[ServiceContract]
interface IMyContract
{
[OperationContract]
string MyMethod();
}
class MyService : IMyContract
{
public string MyMethod()
{
return "Hello WCF";
}
}
You can use implicit or explicit interface implementation:
class MyService : IMyContract
{
string IMyContract.MyMethod()
{
return "Hello WCF";
}
}
A single class can support multiple contracts by deriving and implementing multiple interfaces decorated with theServiceContractattribute.
[ServiceContract]
interface IMyContract
{
[OperationContract]
string MyMethod();
}
[ServiceContract]
interface IMyOtherContract
{
[OperationContract]
void MyOtherMethod();
}
class MyService : IMyContract,IMyOtherContract
{
public string MyMethod()
{...}
public void MyOtherMethod()
{...}
}
There are, however, a few implementation constraints on the service implementation class. You should avoid parameterized constructors because only the default constructor will ever be used by WCF. Also, although the class can use internal properties, indexers, and static members, no WCF client will ever be able to access them.
WCF also lets you apply theServiceContractattribute directly on the service class, without ever defining a separate contract first:
//Avoid
[ServiceContract]
class MyService
{
[OperationContract]
string MyMethod()
{
return "Hello WCF";
}
}
Under the covers, WCF will infer the contract definition. You can apply theOperationContract attribute on any method of the class, be it private or public.
Avoid using theServiceContractattribute directly on the service class. Always define a separate contract, so that you can use it in other contexts.
Names and namespacesYou can and should define a namespace for your contract. The contract namespace serves the same purpose in WCF as it does in .NET programming: to scope a type of contract and reduce the overall chance for a collision. You use the Namespaceproperty of theServiceContractattribute to provide a namespace:
[ServiceContract(Namespace = "MyNamespace")]
interface IMyContract
{...}
Unspecified, the contract namespace defaults to http://tempuri.org. For outward-facing services you would typically use your company’s URL, and for intranet services you can use any meaningful unique name, such asMyApplication.
By default, the exposed name of the contract will be the name of the interface used. However, you could use an alias for a contract to expose a different name to the clients in the metadata using theNameproperty of theServiceContractattribute:
[ServiceContract(Name = "IMyContract")]
interface IMyOtherContract
{...}
In similar manner, the name of the publicly exposed operation defaults to the method name, but you can use theNameproperty of theOperationContractattribute to alias it to a different publicly exposed name:
[ServiceContract]
interface IMyContract
{
[OperationContract(Name = "SomeOperation")]
void MyMethod(string text);
}
You will see a use for these properties in the next chapter.
Please check back next week for the continuation of this article.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter one of Programming WCF Services, written by Juval Lowry (O'Reilly, 2007; ISBN: 0596526997). Check it out today at your favorite bookstore. Buy this book now.
|
|